如何在CENTOS 5.5上安装git?

72

我该如何在CENTOS 5.5机器上安装Git?我尝试使用yum安装,但出现以下消息。

root@host [~]# sudo yum install git
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * addons: centos.mirrors.tds.net
 * base: mirror.ubiquityservers.com
 * extras: mirrors.serveraxis.net
 * updates: pubmirrors.reflected.net
addons                                                   |  951 B     00:00
base                                                     | 2.1 kB     00:00
extras                                                   | 2.1 kB     00:00
updates                                                  | 1.9 kB     00:00
Excluding Packages in global exclude list
Finished
Setting up Install Process
No package git available.
Nothing to do
root@host [~]#

1
在较新版本的CentOS上,使用标准的“yum install git”似乎完全可以正常工作。 - James John McGuire 'Jahmic'
yum安装git-core也可以。 - Alexander Bird
14个回答

135

如果您使用的是CentOS,则内置的yum软件库似乎没有包含git,因此您需要向系统添加额外的软件库。对于我的服务器,我发现Webtatic软件库似乎是比较新的,安装git的方法如下:

# Add the repository
rpm -Uvh http://repo.webtatic.com/yum/centos/5/latest.rpm

# Install the latest version of git
yum install --enablerepo=webtatic git-all

解决 Missing Dependency: perl(Git) 错误:

yum install --enablerepo=webtatic --disableexcludes=main  git-all

1
这真的很好,但对于某些人来说,dl.fedoraproject.org版本可能是更好的选择(对我来说,我在使用此软件包时遇到了一个奇怪的冲突,与perl SVN集成工具有关,所以不得不使用Fedora版本)。 - Iain Collins
6
这也包括图形用户界面(GUI)工具和许多依赖项。您可以使用yum install git仅获取基本安装。 - pfrenssen
2
我并不想承诺添加新的仓库,所以我只是通过浏览器访问了http://repo.webtatic.com/yum/centos/5/并挑选我需要的RPM文件。 - MarkHu
1
只是一条评论,表达感谢你的出色答案 - 这真的很有帮助。 - Simon Courtenage
5
我只需要最后一行。在“yum install git”无法工作的情况下,“yum install --disableexcludes=main git-all”可行。谢谢。 - djskinner
显示剩余8条评论

42

从哪里获得?从版本库获取?最简单的方法是使用版本库:sudo yum install git即可。如果git不由主要版本库提供,则可能首先需要设置其他版本库,如EPEL

如果您想从源代码安装,您可以尝试这些说明。如果已安装yum-utils,那么实际上比这更容易 ** :

sudo yum build-dep git
wget http://kernel.org/pub/software/scm/git/<latest-git-source>.tar.gz
tar -xvjf <latest-git>.tar.gz
cd <git>
make (possibly a ./configure before this)
sudo make install

将被<>包围的部分替换为所需的路径。具体步骤可能会略有不同,因为我个人没有从源代码编译git(例如可能会有一个配置脚本)。如果你不知道我在说什么,那么你可能希望按照我的第一个建议从存储库安装。


11
我尝试通过yum安装,但是它显示“没有可用的git包”。 - Prakash
9
@Prakash:看起来git不在标准的CentOS仓库中。你可以选择从源代码安装或在你的机器上设置EPEL仓库:https://fedoraproject.org/wiki/EPEL。如果需要更多帮助,你也可以考虑在Superuser/Serverfault上提出这个问题。 - eldarerathis
1
@Rimian:我知道这个链接可以安装git和gitweb,但它的重点是gitosis,而gitosis已经不再维护,完全被gitolite取代。 - Cascabel
9
sudo yum install epel-releasesudo yum install git 在 Red Hat (RHEL) 上也能很好地工作。 - Jess
1
@Jess 非常有用,简单明了的回答。谢谢。建议您将此作为答案发布,因为它直接而不复杂。 - octopusgrabbus
显示剩余3条评论

34
只需:
sudo rpm -Uvh https://archives.fedoraproject.org/pub/archive/epel/5/i386/epel-release-5-4.noarch.rpm
sudo yum install git-core

7
请注意,"download.fedora.redhat.com"现在是"dl.fedoraproject.org",但仍然可用。 - Iain Collins
1
这个答案比从源代码构建要好得多。使用yum在持续的基础上管理软件包要容易得多。 - Joshua Davis
工作正常(Centos 5.9) - chrisjlee
除了上述两个命令外,我还需要执行“sudo yum install git”来获取git二进制文件。我使用的是CentOS 5.9。 - user674669

25

我尝试过这个问题中的几种方法,但它们都在我的CentOs上失败了,要么是因为错误的存储库,要么是缺少文件。

这是对我有效的方法(在安装版本1.7.8时):

    yum -y install zlib-devel openssl-devel cpio expat-devel gettext-devel
    wget http://git-core.googlecode.com/files/git-1.7.8.tar.gz
    tar -xzvf ./git-1.7.8.tar.gz
    cd ./git-1.7.8
    ./configure
    make
    make install 
您可能希望从这里下载不同版本的Git: http://code.google.com/p/git-core/downloads/list

有没有一种可靠的方法将源代码配置为软件包? - Henrik
你的意思是创建一个新的软件包吗?我想你总是可以这样做的,是的:https://dev59.com/y3NA5IYBdhLWcg3wpfmu - Datageek
1
我也遇到了同样的问题,不得不采用这种方法。为了便于搜索,以下是我在尝试通过webtatic或EPEL软件包安装时遇到的一些错误:“缺少依赖项:软件包git-1.7.7.3-1.w5.x86_64(webtatic)需要perl(Error)”,“缺少依赖项:软件包git-1.7.7.3-1.w5.x86_64(webtatic)需要perl-Git = 1.7.7.3-1.w5”。按照此答案中指定的方法安装时没有错误。 - jlpp
我使用下载版本1.7.12进行操作,但它无法克隆https URL,并提示不支持https。根据用户SCL在评论中提供的另一个答案,我尝试使用dl.fedoraproject.org,结果运行良好。 - simbo1905

14

Git 1.8有点老了。下面的脚本提供2.12版本,它是目前最新的版本。 - jww

11

我相信随着RHEL 5接近生命周期的结束,这个问题很快就会被淘汰,但是答案现在似乎变得简单多了:

sudo yum install epel-release
sudo yum install git

在我安装 CentOS 5.11 的新环境中,这个方法对我有效。


6
yum -y install zlib-devel openssl-devel cpio expat-devel gettext-devel gcc
wget http://git-core.googlecode.com/files/git-1.7.11.4.tar.gz
tar zxvf git-1.7.11.4.tar.gz 
cd git-1.7.11.4
./configure 
make
make install

1
看起来 CentOS 5 的存储库正在消失。这个问题中提到的大多数存储库已不再在线,似乎没有 Git,或者有一个非常老的版本的 Git。下面是我使用的脚本,用于从源代码构建 OpenSSL、IDN2、PCRE、cURL 和 Git。git://https:// 协议都可用于克隆。
随着时间的推移,档案名称需要更新。例如,截至本文撰写时,openssl-1.0.2k.tar.gz 是 1.0.2 系列中最新的可用版本。
Dale Anderson 的答案目前看起来使用 RHEL 存储库很好,但它是一个相当旧的版本。红帽提供 Git 版本 1.8,而下面的脚本从源代码构建 2.12。
#!/usr/bin/env bash

# OpenSSL installs into lib64/, while cURL installs into lib/
INSTALL_ROOT=/usr/local
INSTALL_LIB32="$INSTALL_ROOT/lib"
INSTALL_LIB64="$INSTALL_ROOT/lib64"

OPENSSL_TAR=openssl-1.0.2k.tar.gz
OPENSSL_DIR=openssl-1.0.2k

ZLIB_TAR=zlib-1.2.11.tar.gz
ZLIB_DIR=zlib-1.2.11

UNISTR_TAR=libunistring-0.9.7.tar.gz
UNISTR_DIR=libunistring-0.9.7

IDN2_TAR=libidn2-0.16.tar.gz
IDN2_DIR=libidn2-0.16

PCRE_TAR=pcre2-10.23.tar.gz
PCRE_DIR=pcre2-10.23

CURL_TAR=curl-7.53.1.tar.gz
CURL_DIR=curl-7.53.1

GIT_TAR=v2.12.2.tar.gz
GIT_DIR=git-2.12.2

###############################################################################

# I don't like doing this, but...
read -s -p "Please enter password for sudo: " SUDO_PASSWWORD

###############################################################################

echo
echo "********** zLib **********"

wget "http://www.zlib.net/$ZLIB_TAR" -O "$ZLIB_TAR"

if [[ "$?" -ne "0" ]]; then
    echo "Failed to download zLib"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

rm -rf "$ZLIB_DIR" &>/dev/null
tar -xzf "$ZLIB_TAR"
cd "$ZLIB_DIR"

LIBS="-ldl -lpthread" ./configure --enable-shared --libdir="$INSTALL_LIB64"

if [[ "$?" -ne "0" ]]; then
    echo "Failed to configure zLib"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

make -j 4

if [ "$?" -ne "0" ]; then
    echo "Failed to build zLib"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

echo "$SUDO_PASSWWORD" | sudo -S make install

cd ..

###############################################################################

echo
echo "********** Unistring **********"

# https://savannah.gnu.org/bugs/?func=detailitem&item_id=26786
wget "https://ftp.gnu.org/gnu/libunistring/$UNISTR_TAR" --no-check-certificate -O "$UNISTR_TAR"

if [[ "$?" -ne "0" ]]; then
    echo "Failed to download IDN"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

rm -rf "$UNISTR_DIR" &>/dev/null
tar -xzf "$UNISTR_TAR"
cd "$UNISTR_DIR"

LIBS="-ldl -lpthread" ./configure --enable-shared --libdir="$INSTALL_LIB64"

if [[ "$?" -ne "0" ]]; then
    echo "Failed to configure IDN"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

make -j 4

if [ "$?" -ne "0" ]; then
    echo "Failed to build IDN"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

echo "$SUDO_PASSWWORD" | sudo -S make install

cd ..

###############################################################################

echo
echo "********** IDN **********"

# https://savannah.gnu.org/bugs/?func=detailitem&item_id=26786
wget "https://alpha.gnu.org/gnu/libidn/$IDN2_TAR" --no-check-certificate -O "$IDN2_TAR"

if [[ "$?" -ne "0" ]]; then
    echo "Failed to download IDN"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

rm -rf "$IDN2_DIR" &>/dev/null
tar -xzf "$IDN2_TAR"
cd "$IDN2_DIR"

LIBS="-ldl -lpthread" ./configure --enable-shared --libdir="$INSTALL_LIB64"

if [[ "$?" -ne "0" ]]; then
    echo "Failed to configure IDN"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

make -j 4

if [ "$?" -ne "0" ]; then
    echo "Failed to build IDN"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

echo "$SUDO_PASSWWORD" | sudo -S make install

cd ..

###############################################################################

echo
echo "********** OpenSSL **********"

wget "https://www.openssl.org/source/$OPENSSL_TAR" -O "$OPENSSL_TAR"

if [[ "$?" -ne "0" ]]; then
    echo "Failed to download OpenSSL"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

rm -rf "$OPENSSL_DIR" &>/dev/null
tar -xzf "$OPENSSL_TAR"
cd "$OPENSSL_DIR"

# OpenSSL and enable-ec_nistp_64_gcc_12 option
IS_X86_64=$(uname -m 2>&1 | egrep -i -c "(amd64|x86_64)")

CONFIG=./config
CONFIG_FLAGS=(no-ssl2 no-ssl3 no-comp shared "-Wl,-rpath,$INSTALL_LIB64" --prefix="$INSTALL_ROOT" --openssldir="$INSTALL_ROOT")
if [[ "$IS_X86_64" -eq "1" ]]; then
    CONFIG_FLAGS+=("enable-ec_nistp_64_gcc_128")
fi

"$CONFIG" "${CONFIG_FLAGS[@]}"

if [[ "$?" -ne "0" ]]; then
    echo "Failed to configure OpenSSL"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

make depend
make -j 4

if [ "$?" -ne "0" ]; then
    echo "Failed to build OpenSSL"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

echo "$SUDO_PASSWWORD" | sudo -S make install_sw

cd ..

###############################################################################

echo
echo "********** PCRE **********"

# https://savannah.gnu.org/bugs/?func=detailitem&item_id=26786
wget "https://ftp.pcre.org/pub/pcre//$PCRE_TAR" --no-check-certificate -O "$PCRE_TAR"

if [[ "$?" -ne "0" ]]; then
    echo "Failed to download PCRE"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

rm -rf "$PCRE_DIR" &>/dev/null
tar -xzf "$PCRE_TAR"
cd "$PCRE_DIR"

make configure
CPPFLAGS="-I$INSTALL_ROOT/include" LDFLAGS="-Wl,-rpath,$INSTALL_LIB64 -L$INSTALL_LIB64" \
    LIBS="-lidn2 -lz -ldl -lpthread" ./configure --enable-shared --enable-pcre2-8 --enable-pcre2-16 --enable-pcre2-32 \
    --enable-unicode-properties --enable-pcregrep-libz --prefix="$INSTALL_ROOT" --libdir="$INSTALL_LIB64"

if [[ "$?" -ne "0" ]]; then
    echo "Failed to configure PCRE"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

make all -j 4

if [ "$?" -ne "0" ]; then
    echo "Failed to build PCRE"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

echo "$SUDO_PASSWWORD" | sudo -S make install

cd ..

###############################################################################

echo
echo "********** cURL **********"

# https://savannah.gnu.org/bugs/?func=detailitem&item_id=26786
wget "https://curl.haxx.se/download/$CURL_TAR" --no-check-certificate -O "$CURL_TAR"

if [[ "$?" -ne "0" ]]; then
    echo "Failed to download cURL"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

rm -rf "$CURL_DIR" &>/dev/null
tar -xzf "$CURL_TAR"
cd "$CURL_DIR"

CPPFLAGS="-I$INSTALL_ROOT/include" LDFLAGS="-Wl,-rpath,$INSTALL_LIB64 -L$INSTALL_LIB64" \
    LIBS="-lidn2 -lssl -lcrypto -lz -ldl -lpthread" \
    ./configure --enable-shared --with-ssl="$INSTALL_ROOT" --with-libidn2="$INSTALL_ROOT" --libdir="$INSTALL_LIB64"

if [[ "$?" -ne "0" ]]; then
    echo "Failed to configure cURL"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

make -j 4

if [ "$?" -ne "0" ]; then
    echo "Failed to build cURL"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

echo "$SUDO_PASSWWORD" | sudo -S make install

cd ..

###############################################################################

echo
echo "********** Git **********"

wget "https://github.com/git/git/archive/$GIT_TAR" -O "$GIT_TAR"

if [[ "$?" -ne "0" ]]; then
    echo "Failed to download Git"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

rm -rf "$GIT_DIR" &>/dev/null
tar -xzf "$GIT_TAR"
cd "$GIT_DIR"

make configure
CPPFLAGS="-I$INSTALL_ROOT/include" LDFLAGS="-Wl,-rpath,$INSTALL_LIB64,-rpath,$INSTALL_LIB32 -L$INSTALL_LIB64 -L$INSTALL_LIB32" \
    LIBS="-lidn2 -lssl -lcrypto -lz -ldl -lpthread" ./configure --with-openssl --with-curl --with-libpcre --prefix="$INSTALL_ROOT"

if [[ "$?" -ne "0" ]]; then
    echo "Failed to configure Git"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

make all -j 4

if [ "$?" -ne "0" ]; then
    echo "Failed to build Git"
    [[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi

MAKE=make
MAKE_FLAGS=(install)
if [[ ! -z `which asciidoc 2>/dev/null | grep -v 'no asciidoc'` ]]; then
    if [[ ! -z `which xmlto 2>/dev/null | grep -v 'no xmlto'` ]]; then
        MAKE_FLAGS+=("install-doc" "install-html" "install-info")
    fi
fi

echo "$SUDO_PASSWWORD" | sudo -S "$MAKE" "${MAKE_FLAGS[@]}"

cd ..

###############################################################################

echo
echo "********** Cleanup **********"

rm -rf "$OPENSSL_TAR  $OPENSSL_DIR  $UNISTR_TAR  $UNISTR_DIR  $CURL_TAR  $CURL_DIR"
rm -rf "$PCRE_TAR $PCRE_DIR $ZLIB_TAR $ZLIB_DIR $IDN2_TAR $IDN2_DIR $GIT_TAR $GIT_DIR"

[[ "$0" = "$BASH_SOURCE" ]] && exit 0 || return 0

1

OK, there is more to it than that, you need zlib. zlib is part of CentOS, but you need the development form to get zlib.h ... notice that the yum name of zlib development is quite different possibly than for apt-get on ubuntu/debian, what follows actually works with my CentOS version
in particular, you do ./configure on git, then try make, and the first build fails with missing zlib.h

I used a two-step procedure to resolve this a) Got RPMFORGE for my version of CentOS

See: www.centos.org/modules/newbb/viewtopic.php?topic_id=18506&forum=38 and this: wiki.centos.org/AdditionalResources/Repositories/RPMForge

In my case [as root, or with sudo]

$ wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
$ rpm -K rpmforge-release-0.5.2-2.el5.rf.*.rpm
$ rpm -i rpmforge-release-0.5.2-2.el5.rf.*.rpm
## Note: the RPM for rpmforge is small (like 12.3K) but don't let that fool
## you; it augments yum the next time you use yum
## [this is the name that YUM found] (still root or sudo)
$ yum install zlib-devel.x86_64
## and finally in the source directory for git (still root or sudo):
$ ./configure (this worked before, but I ran it again to be sure)
$ make
$ make install

(this install put it by default in /usr/local/bin/git ... not my favorite choice, but OK for the default)... and git works fine!


你可以使用Markdown格式来回答问题,而不是直接使用HTML。这样更容易让其他人进行编辑。 - Noufal Ibrahim
2
git在rpmforge中,所以你只需要将rpmforge添加到你的仓库列表中并安装:http://dag.wieers.com/rpm/FAQ.php#B => 添加rpmforge => yum install git-all(就这样) - michael
这似乎是迄今为止最好的解决方案。 - Caterham

1

仅针对2017年及以后的更新,因为CentOS 5已经到达EOL,而EPEL的URL已更改:

sudo rpm -Uvh http://archives.fedoraproject.org/pub/archive/epel/5/x86_64/epel-release-5-4.noarch.rpm
sudo yum install git

这将为您获取git 1.8.2.3


1
这是在2019年12月最终对我起作用的那个。 - MikeC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接