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:
you will note that it will install
18.104.22.168 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 - https://github.com/git/git/releases 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 https://github.com/git/git/archive/v2.16.3.tar.gz
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:
--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.
git.sh in this location:
sudo vi /etc/profile.d/git.sh
and paste there following contents:
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.
- 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
- 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.