安装最新版本的Git

使用yum安装不到最新版本,根据Git官网的提示,得知CentOS需要下载源码编译安装:

Red Hat Enterprise Linux, Oracle Linux, CentOS, Scientific Linux, et al.

RHEL and derivatives typically ship older versions of git. You can download a tarball and build from source, or use a 3rd-party repository such as the IUS Community Project to obtain a more recent version of git.

本文记录时最新版本为2.19.1

  1. 安装依赖软件
[root@doracoin ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel asciidoc
[root@doracoin ~]# yum install  gcc perl-ExtUtils-MakeMaker
  1. 卸载系统自带的低版本git(1.8.5.1)
[root@doracoin ~]# git --version
git version 1.8.5.1
[root@doracoin ~]# yum remove git
  1. 编译安装最新的git版本
[root@doracoin ~]# cd /usr/local/src/
[root@doracoin src]# wget https://www.kernel.org/pub/software/scm/git/git-2.19.1.tar.xz
[root@doracoin src]# tar -vxf git-2.19.1.tar.xz
[root@doracoin src]# cd git-2.19.1
[root@doracoin git-2.19.1]# make prefix=/usr/local/git all
[root@doracoin git-2.19.1]# make prefix=/usr/local/git install
[root@doracoin git-2.19.1]# echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/profile
[root@doracoin git-2.19.1]# source /etc/profile
  1. 查看新的版本
[root@doracoin ~]# git --version
git version 2.19.1

注:如果是非root用户使用git,则需要配置下该用户下的环境变量

# 切换到该用户,执行以下命令
[git@doracoin ~]$ echo "export PATH=$PATH:/usr/local/git/bin" >> ~/.bashrc
[git@doracoin ~]$ source ~/.bashrc
[git@doracoin ~]$ git --version
git version 2.19.1

问题记录

编译过程中,在执行make命令时:

make prefix=/usr/local/git all

可能会遇到以下错误:

LINK git-credential-store
libgit.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 status
make: *** [git-credential-store] Error 1

去官网下载安装最新版本的libiconv即可,本文操作时最新版为1.15

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz
tar -zxvf libiconv-1.15.tar.gz
cd libiconv-1.15
./configure --prefix=/usr/local/libiconv
make
make install

创建软链接到/usr/lib

ln -s /usr/local/lib/libiconv.so /usr/lib
ln -s /usr/local/lib/libiconv.so.2 /usr/lib

然后回到git目录继续编译

./configure --prefix=/usr/local --with-iconv=/usr/local/libiconv
make
make install

接下来git就可以顺利安装了,笔者暂未遇到其它问题


参考链接: