1. 问题描述
centos7系统默认的git安装版本是1.8,但是在项目构建中发现git版本过低,于是用源码编译的方式进行升级.同时该文章也适用于安装新的git,相信大家对git都有一定的了解了,在文章过程中有的步骤也就不细细讲了.
操作环境:centos7.0及以上
2. 升级安装流程
2.1. 第一步卸载原有的git
rpm -e --nodeps git 仅删除该git包
# yum remove git 采用yum删除git,同时也将删除其依赖包,例如go,建议不采用该命令
2.2. 安装相关依赖
yum install -y curl-devel expat-devel openssl-devel zlib-devel asciidoc
yum install -y gcc perl-ExtUtils-MakeMaker
2.3. 安装git
- 下载解压
#下载git从https://github.com/git/git/releases
wget https://github.com/git/git/archive/v2.27.0.tar.gz -O git.tar.gz
tar -zxvf git.tar.gz
cd git-2.27.0
#将压缩包解压到/usr/local/src目录
#tar -C /usr/local/src -zxvf git.tar.gz
- 编译安装
# 编译并设置安装路径
make prefix=/usr/local/bin/git all
# 安装
make install
- 执行
# 写入到环境变量中,但重启则失效(方法一)
export PATH=$PATH:/usr/local/bin/git/bin
# 写入到环境变量中(方法二)
echo "export PATH=$PATH:/usr/local/bin/git/bin" >> /etc/profile && source /etc/profile
# 制作软链接(方法三)
rm -f /usr/bin/git && ln -s /usr/local/bin/git /usr/bin/git
# 查看是否已安装和版本号
git --version
3. 编译中问题解决
正常的流程就是按照上面的流程进行安装即可,下面总结一些在安装过程中遇到的几个问题.
- make prefix=/usr/local/git all进行编译的时候提示如下错误
LINK git-credential-storelibgit.a(utf8.o): In function `reencode_string_iconv':/usr/src/git-2.8.3/utf8.c:463: undefined reference to `libiconv'libgit.a(utf8.o): In function `reencode_string_len':/usr/src/git-2.8.3/utf8.c:502: undefined reference to `libiconv_open'/usr/src/git-2.8.3/utf8.c:521: undefined reference to `libiconv_close'/usr/src/git-2.8.3/utf8.c:515: undefined reference to `libiconv_open'collect2: ld returned 1 exit statusmake: *** [git-credential-store] Error 1
这个问题主要是系统缺少libiconv库导致的。根据上面提供的链接,下载libiconv即可。
cd /usr/local/src
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar -zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv //配置
make //编译
make install //安装
ln -s /usr/local/lib/libiconv.so /usr/lib //建立软连接
这时候还libiconv库已经安装完成,下面进入我们的git安装目录,按照下面的方式进行安装
make configure
./configure --prefix=/usr/local --with-iconv=/usr/local/libiconv //编译
make //安装
make install //加入环境变量
export PATH=$PATH:/usr/local/bin/git检测版本号git --version
- 在安装libiconv时会遇到./stdio.h:1010:1: error: 'gets' undeclared here (not in a function)的错误提示,进行下面的操作即可解决.
进入错误文件路径cd libiconv-1.14/srclib编辑文件stdio.in.h找到698行的样子,内容是_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");将这一行注释掉(注意注释一定要用/**/来进行注释),替换为下面的内容
#if defined(__GLIBC__) && !defined(__UCLIBC__) && !__GLIBC_PREREQ(2, 16)_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");#endif