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.16.3) 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?

I show you two approaches. The only difference is with using OpenSSL version. By default CentOS is shipped with older version. I like to have most recent version so I compile OpenSSL from source. It is not mandatory, since you can use default version.  If you prefer to upgrade it  follow this tutorial first and get back here once you will have latest version. If not just follow along.

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.

If you didn't compile latest OpenSSL like I did you must install one more package:

sudo yum install openssl-devel -y

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.16.3. 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.16.3.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.16.3
make clean
make configure

Depending on which OpenSSL you are using...

If you compiled latest OpenSSL use following command:

./configure CFLAGS='-I/usr/local/openssl/include' LDFLAGS='-L/usr/local/openssl/lib' --prefix=/usr/local/git --with-openssl=/usr/local/openssl

If you are using regular version:

./configure --prefix=/usr/local/git

Note parameter --prefix in configure command above. It will prevent overriding Git installed via yum. It will also help with overriding compiled version with packages installed from yum.  Once package is configure run:


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.16.3 v2.16.3.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/git/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:)

  • Ed Russell

    This is an extremely clear and well written cookbook tutorial. I followed it with zero problems.

  • Dale Pres

    This buildsi in the same directory as the source so it isn’t a clean install. How do I know what to preserve so I can delete the source? Maybe just get rid of .h and .c?

    • Krystian

      Hi there,

      It depends where you extract the downloaded source code. You can use your home directory or /usr/src for instance. Make sure that when you use ./configure command to specify –prefix=/usr/local/git or any other location where you want to have git installed. If you extract the files to /usr/local/git it won’t be clean install as source code and compiled files will be in the same directory. If you extract files to different directory and install into another dir, you will have clean install.

  • Bahata Ansumali

    Very helpful.

  • zauxst

    Thanks. Solved me a lot of headaches I had with CentOS!

  • roso


    Thank you for your very clear post.

    What if I just need to run git from my local home folder? ie /home/roso/git ?

    and git available in my local path and not the entire system $path?

    I use a university machine and want the latest git be available only to me..
    The global git is version 1.7.1 where as the one i installed is 2.18.0. I apparently do not have sudo privileges. I managed to install the extra packages with admin. But he refused to have 2 variants of git, and adviced me to install the latest git on my local home directory.

    Problem is now I dont know whether there is a local path for jsut the user. I use CentOS 6.10. I cannot find any .bashrc or .profile

  • Raymon Guzm√°n Dom√≠nguez

    It’s doesn’t works for me. Still i have the git 1.8… when i execute “$ git –version” command line.

    • Krystian

      Are you sure that you added your installation to $PATH correctly? If you didn’t and you have git installed from yum, by default it call yum version, not your compiled version.