CentOS7 安装最新版Git(源码安装)

本文详细介绍了在CentOS环境下从源码编译安装Git 2.19.1的步骤,包括下载、解压、配置、编译、安装及环境变量设置,同时解决了编译过程中遇到的依赖问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、下载最新版Git

a)查看最新版

https://mirrors.edge.kernel.org/pub/software/scm/git/
或者
https://github.com/git/git/releases

b)下载最新版

wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.19.1.tar.gz

或在本地下载完成后上传到CentOS虚拟机(建议迅雷下载,wget有点慢)

2、解压

[root@localhost download]# pwd
/usr/local/src/download
[root@localhost download]# ll
total 49276
-rw-r--r--. 1 root root    35676 Oct  4 13:34 container-selinux-2.68-1.el7.noarch.rpm
-rw-r--r--. 1 root root 42712724 Oct  4 13:26 docker-ce-18.06.1.ce-3.el7.x86_64.rpm
-rw-r--r--. 1 root root  7708143 Dec  1 10:02 git-2.19.1.tar.gz
[root@localhost src]# tar -zxvf /usr/local/src/download/git-2.19.1.tar.gz -C /usr/local/src/
[root@localhost src]# pwd
/usr/local/src
[root@localhost src]# ll
total 16
drwxr-xr-x.  3 root root    19 Oct 27 22:23 config
drwxr-xr-x.  2 root root   123 Dec  1 10:03 download
drwxrwxr-x. 25 root root 12288 Sep 28 04:46 git-2.19.1
drwxr-xr-x.  2 root root     6 Nov 30 21:52 workspace

3、编译

a)卸载旧版本

[root@localhost git-2.19.1]# yum remove git

b)make configure出错

[root@localhost git-2.19.1]# make configure
GIT_VERSION = 2.19.1
    GEN configure
/bin/sh: autoconf: command not found
make: *** [configure] Error 127

解决

[root@localhost git-2.19.1]# yum install  autoconf automake libtool
[root@localhost git-2.19.1]# make configure                        
    GEN configure

c)yum install libcurl-devel

[root@localhost git-2.19.1]# yum install libcurl-devel

d)./configure

[root@localhost git-2.19.1]# ./configure --prefix=/usr/local/git --with-iconv --with-curl --with-expat=/usr/local/lib
configure: Setting lib to 'lib' (the default)
configure: Will try -pthread then -lpthread to enable POSIX Threads.
configure: CHECKS for site configuration
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for size_t... yes
checking for working alloca.h... yes
checking for alloca... yes
configure: Setting EXPATDIR to /usr/local/lib
configure: CHECKS for programs
checking whether we are using the GNU C compiler... (cached) yes
checking whether gcc accepts -g... (cached) yes
checking for gcc option to accept ISO C89... (cached) none needed
checking for inline... inline
checking if linker supports -R... no
checking if linker supports -Wl,-rpath,... yes
checking for gar... no
checking for ar... ar
checking for gtar... gtar
checking for gnudiff... no
checking for gdiff... no
checking for diff... diff
checking for asciidoc... no
Using 'grep -a' for sane_grep
configure: CHECKS for libraries
checking for SHA1_Init in -lcrypto... no
checking for SHA1_Init in -lssl... no
checking for curl_global_init in -lcurl... yes
checking for curl-config... curl-config
checking for XML_ParserCreate in -lexpat... no
checking for iconv in -lc... yes
checking for deflateBound in -lz... no
checking for socket in -lc... yes
checking for inet_ntop... yes
checking for inet_pton... yes
checking for hstrerror... yes
checking for basename in -lc... yes
checking for gettext in -lc... yes
checking libintl.h usability... yes
checking libintl.h presence... yes
checking for libintl.h... yes
configure: CHECKS for header files
checking sys/select.h usability... yes
checking sys/select.h presence... yes
checking for sys/select.h... yes
checking sys/poll.h usability... yes
checking sys/poll.h presence... yes
checking for sys/poll.h... yes
checking for inttypes.h... (cached) yes
checking for old iconv()... no
configure: CHECKS for typedefs, structures, and compiler characteristics
checking for socklen_t... yes
checking for struct itimerval... yes
checking for struct stat.st_mtimespec.tv_nsec... no
checking for struct stat.st_mtim.tv_nsec... yes
checking for struct dirent.d_type... yes
checking for struct passwd.pw_gecos... yes
checking for struct sockaddr_storage... yes
checking for struct addrinfo... yes
checking for getaddrinfo... yes
checking for library containing getaddrinfo... none required
checking whether the platform regex supports REG_STARTEND... yes
checking whether system succeeds to read fopen'ed directory... yes
checking whether snprintf() and/or vsnprintf() return bogus value... no
checking whether the platform uses typical file type bits... yes
configure: CHECKS for library functions
checking libgen.h usability... yes
checking libgen.h presence... yes
checking for libgen.h... yes
checking paths.h usability... yes
checking paths.h presence... yes
checking for paths.h... yes
checking libcharset.h usability... no
checking libcharset.h presence... no
checking for libcharset.h... no
checking for strings.h... (cached) yes
checking for locale_charset in -liconv... no
checking for locale_charset in -lcharset... no
checking for clock_gettime... yes
checking for library containing clock_gettime... none required
checking for CLOCK_MONOTONIC... yes
checking for setitimer... yes
checking for library containing setitimer... none required
checking for strcasestr... yes
checking for library containing strcasestr... none required
checking for memmem... yes
checking for library containing memmem... none required
checking for strlcpy... no
checking for uintmax_t... yes
checking for strtoumax... yes
checking for library containing strtoumax... none required
checking for setenv... yes
checking for library containing setenv... none required
checking for unsetenv... yes
checking for library containing unsetenv... none required
checking for mkdtemp... yes
checking for library containing mkdtemp... none required
checking for initgroups... yes
checking for library containing initgroups... none required
checking for getdelim... yes
checking for library containing getdelim... none required
checking for BSD sysctl... no
checking for POSIX Threads with ''... no
checking for POSIX Threads with '-mt'... no
checking for POSIX Threads with '-pthread'... yes
configure: creating ./config.status
config.status: creating config.mak.autogen
config.status: executing config.mak.autogen commands

4、make && make install

[root@localhost git-2.19.1]# make && make install
    * new build flags
    CC credential-store.o
In file included from credential-store.c:1:0:
cache.h:20:18: fatal error: zlib.h: No such file or directory
 #include <zlib.h>
                  ^
compilation terminated.
make: *** [credential-store.o] Error 1

解决

[root@localhost git-2.19.1]# yum install zlib zlib-devel
[root@localhost git-2.19.1]# make && make install

5、配置环境变量

[root@localhost git-2.19.1]#echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrc
[root@localhost git-2.19.1]#source /etc/bashrc

6、检查版本

[root@localhost git-2.19.1]# git --version
git version 2.19.1

7、github帐号添加SSH keys

[root@localhost git-2.19.1]# ssh-keygen -t rsa -C"你的Git账号"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #回车
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): #回车或输入密码
Enter same passphrase again: #在输一次
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:dbLagqimFQMLcl0oH+wH14TKyQ+k2L5BotlbrZPMNFM cashzhang27@qq.com
The key's randomart image is:
+---[RSA 2048]----+
|   . ..+.        |
|  ..*.o .        |
|+o.O.*    o .    |
|=++ O E  . +     |
|o*o  *  S .      |
|o +o=.o. o       |
|  .O.=. o .      |
| .+.*    .       |
|.o.  .           |
+----[SHA256]-----+
[root@localhost git-2.19.1]# cat  ~/.ssh/id_rsa.pub
# 再把公钥粘到GitHub或Gitee上就ok了

 

### 安装最新版本的 Git 为了在 CentOS 7安装最新的 Git 版本,建议采用编译源码的方式进行安装。这能确保获取到最新功能并解决可能存在的兼容性问题。 #### 移除旧版 Git 如果系统中已存在旧版本的 Git,则需先将其卸载: ```bash sudo yum -y remove git sudo yum -y remove git-* ``` 此操作可防止不同版本之间的冲突[^2]。 #### 配置基础环境 更新系统的 DNS 设置以提高网络连接稳定性,并替换默认的 YUM 源为阿里云镜像站来加速下载速度: ```bash echo "nameserver 8.8.8.8" >> /etc/resolv.conf curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo ``` 接着安装必要的开发工具和库文件用于后续编译过程: ```bash yum groupinstall 'Development Tools' -y yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker -y ``` 上述命令将准备所需的构建环境[^3]。 #### 下载与编译 Git 源代码 前往官方 GitHub 页面查找最新发布的稳定版标签页链接地址,假设当前最新版本号为 `v2.x.y`(具体数值请自行确认),执行如下指令完成下载、解压及编译工作: ```bash cd /usr/src/ wget https://github.com/git/git/archive/v2.x.y.tar.gz tar zxvf v2.x.y.tar.gz cd git-v2.x.y/ make prefix=/usr/local all sudo make prefix=/usr/local install ``` 通过这种方式可以直接获得最前沿的功能特性支持[^4]。 #### 验证安装成果 最后验证新安装Git 是否生效及其版本信息: ```bash git --version ``` 应当显示刚刚所安装的具体版本号而非之前的较低版本。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值