Git on CentOS

How to install GIT on CentOS

Today I would like to show you how I install GIT on CentOS servers. It might be super easy or slightly harder. It depends if you want to have the latest version (2.14.0) or you don't care:)

Easy way to install git on CentOS with yum

The easiest way is to use yum of course. Simply type

sudo yum install git -y

and it will install git on your server. That's it 😀 However if you will check your git version:

git --version

you will note that it will install version (or higher). But wait! Isn't there git v2.0? Yes, there is. And I will show you how to install it.

How to compile git from source on CentOS?

There are 5 mandatory and 2 optional steps to have latest Git version on CentOS.

First you need to install some tools that are required to compile git. You will need some libraries and compiler. Install them by executing following command:

sudo yum install autoconf libcurl-devel expat-devel gcc gettext-devel kernel-headers openssl-devel perl-devel zlib-devel -y

Here is the fun part. Even if you don't have git installed, after executing command above you will actually have git on your server. It is because one of the packages has git as a dependency. But you shouldn't worry about that too much now.

Step two is to download git source code. The easiest option is to download it from github. Visit git release page - and pick desired version. I'm usually using latest, non-rc version. In my case it's 2.14.0. Right click on tar.gz icon and copy the link.

Now execute following command on your server (link you see is copied from GitHub):

curl -O -L

It will download tarball with git source files to your server. -O flag will save the file instead of redirecting curl output to stdout. -L will follow redirects if any (usually there is one when using GitHub releases).

Step three is obviously unpacking downloaded source code:

tar -zxvf v2.10.2.tar.gz

Now is the fun part - compiling. Very important is to know, that you should never ever use root account for compiling. This might lead to serious security issues. Imagine that you will compile code that contains malicious parts as root. This code can modify your server and cut off your root access. Google about it, there were such cases in the past. So if you use root account, think about creating separate account to manage your server. Read this tutorial to learn how to setup additional account with root privileges. I'm not saying that you can't compile it as root, because obviously you can. But you just shouldn't do that.

Execute following command as non-root account:

cd git-v2.14.0
make clean
make configure
./configure --prefix=/usr/local

After these steps git will be compiled and will be ready to install. Note parameter --prefix in commands above. It will prevent overriding Git installed via yum. It will also help with overriding compiled version with packages installed from yum. For installing you need to have root account or be in sudoers group. So last step is installation:

sudo make install

Now you have latest version of git in your system!

Optionally you can remove downloaded package and uncompressed directory. To cleanup execute:

cd ..
rm -rf git-v2.14.0 v2.14.0.tar.gz

Adding GIT to $PATH

Here is the important part of this tutorial. To force using GIT that was just installed we need to add it to $PATH. It will prevent running git installed with yum.

Create file in this location:

sudo vi /etc/profile.d/

and paste there following contents:

pathmunge /usr/local/bin

After exiting and logging again to your SSH session you will always call GIT compiled from source:)

How to update GIT to newer version?

When new version of GIT will be released, simply repeat the steps in this tutorial, but with newer version. You don't need to remove GIT that is currently installed. New build will override the one that you already have.

Drawbacks of compiling git from source

You must know that there are certain drawbacks of compiling git from source. There are not really painful to be honest.

  1. You must manually check if there is new version of git. When you try to do yum update, you won't see it on the list
  2. Theoretically you will have two GITs installed - one via yum (old version), and latest version compiled from source. In practice, you will only use the one that was compiled.

If you don't want to install git manually you can use our Lamp On Steroids project based on Ansible. We have git role that automates the whole process.


    Great tutorial! Thanks to your blog, I have installed Git 2.12.2 on the CentOS 7 computer.

    Currently, the latest version of Git is 2.13.0.

    Then do I have to delete Git 2.12.2 and

    curl -O -L

    and try the above process, again?

    Also, now I cannot use $sudo yum -y remove git
    what’s the proper way to remove Git after following your tutorial?

    -Best Regards

  • Rich Kucera
    • Krystian

      Hi there! Thank you for your comment. That’s true, you can install any of the packages via external repository (GIT, PHP, etc). But I personally prefer to install it from source that are downloaded from reliable website (like instead of downloading compiled version from source that is maintained by someone else.
      With external repositories there is always slight chance that you can install some additional things, that you don’t want to have on your server:) I’m not saying that this WANdisco repo has some malicious code, because it’s probably not, but you know what I mean.
      All our servers are provisioned by Ansible, so compiling stuff from source code is not that complicated:) It might be bit harder to maintain if you do everything manually. If you don’t care about security that much, using WANdisco repo is way easier:)