Read in 3 minutes

last updated 

How To Install Git on Debian 9

This tutorial will show you how to install and configure Git on Debian 9.

Git is the world’s most popular distributed version control system used by many open source and commercial projects. 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 was tested on Debian 9 but it should also work with any previous Debian version.

Prerequisites

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

Installing Git with Apt

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

The following steps will show you how to install Git on your Debian system:

  1. Update package index.

    Before installing new packages you should always update the apt package index:

    sudo apt update
  2. Install Git.

    Once the list is updated issue the following command to install Git:

    sudo apt install git
  3. Verify Git installation.

    To verify the installation type the following command to print the Git version:

    git --version
    git version 2.11.0

As you can see from the output above, you have successfully installed Git version 2.11.0. You can now move on to the Configuring Git section of this tutorial to complete your setup.

Installing Git from Source

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

Before continuing with the next steps, first you need to install the packages necessary to build Git on your Debian system:

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

Once the dependencies are installed open your browser, go to Git project’s mirror on GitHub and copy the latest release link address that ends in .tar.gz:

At the time of writing this article, the latest stable Git version is 2.18.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/

Use the wget command to download the archive file as git.tar.gz:

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

Once the download is completed extract the file that you downloaded and switch to the git source directory by typing:

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

Now, you can compile and install Git by typing these two commands:

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

Once the installation is completed verify it by typing the following command which will print the installed Git version:

git --version
git version 2.18.0

Later, 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 recommended to set your Git commit email and username:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

You can verify the changes with the following command:

git config --list
user.name=Your Name
[email protected]

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

~/.gitconfig
[user]
    name = Your Name
    email = [email protected]

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

Conclusion

You have learned how to install Git on your Debian system. You should now check the Pro Git book and learn more about how to use Git.

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