Skip to content

Instantly share code, notes, and snippets.

@maximd
Last active August 29, 2015 14:22
Show Gist options
  • Save maximd/eb3778e6328c40ff6fe2 to your computer and use it in GitHub Desktop.
Save maximd/eb3778e6328c40ff6fe2 to your computer and use it in GitHub Desktop.
Install git from source on RHEL and its derivative (Centos, ...)
#!/usr/bin/env bash
#
# Install git from source on RHEL and its derivative (Centos, ...)
#
# The idea is to not touch the system wide git (installed via yum). That's why
# we install it in the home directory.
#
# Also setup the man pages so that "${INSTALL_DIR}/bin/git commit --help" (for
# example) gives the $GIT_VERSION man page, not the original system man page.
# But see below for more explanation.
#
# Inspired by http://tecadmin.net/install-git-2-0-on-centos-rhel-fedora
# and http://tecadmin.net/install-git-2-0-on-centos-rhel-fedora/
#
# Author: Maxim Doucet <maxim.doucet@gmail.com>
# If needed, install the dependencies as root with:
#
# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker
GIT_VERSION=2.4.2
BUILD_DIR=/tmp/install_git
INSTALL_DIR=~/local/git
if ! mkdir -p "${BUILD_DIR}" ; then
echo "Error: cannot create ${BUILD_DIR}" >&2
exit 100
fi
pushd "${BUILD_DIR}"
wget "https://www.kernel.org/pub/software/scm/git/git-${GIT_VERSION}.tar.gz"
wget "https://www.kernel.org/pub/software/scm/git/git-manpages-${GIT_VERSION}.tar.gz"
tar zxvf "git-${GIT_VERSION}.tar.gz"
cd "git-${GIT_VERSION}"
make -j "$(grep -E -c '^processor' /proc/cpuinfo)" prefix="${INSTALL_DIR}" all
make prefix="${INSTALL_DIR}" install
tar zxvf "${BUILD_DIR}/git-manpages-${GIT_VERSION}.tar.gz" -C "${INSTALL_DIR}/share/man"
# Restore the destination directory permissions because tar changes it
# (someone has the same problem at http://stackoverflow.com/a/29254273/576371),
# even with "tar --no-same-permissions"
chmod o+rx "${INSTALL_DIR}/share/man"
popd
rm -rf "${BUILD_DIR}"
cat << EOF
Now add the following line to your ~/.bashrc:
PATH="${INSTALL_DIR}/bin:\${PATH}"
Note that we put the newest git as the first element of the PATH on purpose: we
want to use it even if a system-wide version is present.
Also check the comments at the end of this script to have more information
regarding the git man pages.
EOF
# By default, git invoked with "--help", like in "git commit --help", will use
# a relative search of its man page. It means that:
#
# ${INSTALL_DIR}/bin/git commit --help
#
# (for example) gives the ${INSTALL_DIR}/share/man man page, not the original
# system man page.
#
# It also means that "man git" will give the original system man page.
# If we want "man git" to give the $GIT_VERSION man page, then add the
# following line to /etc/man.config (replace ${INSTALL_DIR} with its actual
# value):
#
# MANPATH_MAP ${INSTALL_DIR}/bin ${INSTALL_DIR}/share/man
#
# Note that this will give the $GIT_VERSION man page only for the users who
# have ${INSTALL_DIR}/bin in their PATH. This is the preferred behaviour: only
# those users who explicitely add ${INSTALL_DIR}/bin to their PATH will use the
# newest git version and its manpage.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment