How To Install Git on Ubuntu 18.04

Updated 

4 min read

Photo by varianto25

Git is a de-facto standard for distributed version control systems and is used by the majority of developers nowadays. It allows you to keep track of your code changes, revert to previous stages, create branches, and to collaborate with your fellow developers.

Git is originally developed by Linus Torvalds, the creator of the Linux kernel.

This tutorial will guide you through the steps required to install Git on Ubuntu 18.04. The same instructions apply for Ubuntu 16.04 and any other Ubuntu-based distribution, including Kubuntu, Linux Mint and Elementary OS.

Prerequisites

Before continuing with this tutorial, make sure you are logged in as root or a user with sudo privileges.

Installing Git with Apt

The easiest and the recommended way to install Git is to install it using the apt package management tool from Ubuntu’s default repositories. If you want to install the latest stable version of Git from source, move on to the Installing Git from the Source section of this tutorial.

Follow these steps to install Git on your Ubuntu system:

  1. Start by updating the package index:

    sudo apt update
  2. Run the following command to install Git:

    sudo apt install git
  3. Verify the installation by typing the following command which will print the Git version:

    git --version

    At the time of writing this article, the current version of Git available in the Ubuntu 18.04 repositories is 2.17.1.

    git version 2.17.1

That’s it, you have successfully installed Git on your Ubuntu and you can start using it.

Installing Git from the Source

Another option is to compile Git from the source, which will allow you to install the latest Git version and to customize the build options. However, you will not able be to maintain your Git installation through the apt package manager.

First, install the dependencies necessary to build Git on your Ubuntu system:

sudo apt update
sudo apt install make libssl-dev libghc-zlib-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip

Once the installation is complete open your browser, visit the Git project’s mirror on GitHub and copy the latest release link URL that ends in .tar.gz:

At the time of writing this article, the latest stable Git version is 2.23.0.

We are going to download Git source in the /usr/src directory which is the common location to place source files, change to the directory with:

cd /usr/src/

Download the file as git.tar.gz using the link you copied earlier:

sudo wget https://github.com/git/git/archive/v2.23.0.tar.gz -O git.tar.gz

Next, extract the tar.gz file and change to the Git source directory by typing:

sudo tar -xf git.tar.gz
cd git-*

Run the following two commands to compile and install Git on your Ubuntu system:

sudo make prefix=/usr/local all
sudo make prefix=/usr/local install

To verify the installation type the following command which will print the installed Git version:

git --version
git version 2.23.0

If you want to upgrade to a newer version, you will need to repeat the installation process.

Configuring Git

Now that you have git installed, it is a good idea to set up your personal information that will be used when you commit your code.

The following commands will set your git commit username and email address:

git config --global user.name "Your Name"
git config --global user.email "youremail@yourdomain.com"

To verify the configuration changes, type:

git config --list

The output should look something like this:

user.name=Your Name
user.email=youremail@yourdomain.com

The configuration settings are stored in the ~/.gitconfig file:

~/.gitconfig
[user]
    name = Your Name
    email = youremail@yourdomain.com

If you want to make further changes to your Git configuration, you can either use the git config command or edit the ~/.gitconfig file by hand.

Conclusion

Installing Git on Ubuntu is a matter of running a single apt command. If you want to use the latest Git release, you’ll need to compile it from the source.

You should now check the Pro Git book and learn more about how to use Git.

If you hit a problem or have feedback, leave a comment below.