How to Install Git on Ubuntu 22.04

Published on

4 min read

Install Git on Ubuntu

Git is the world’s most popular distributed version control system used by many open-source and commercial projects. It allows you to collaborate on projects with fellow developers, keep track of your code changes, revert to previous stages, create branches , and more.

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

In this guide, we will describe how to install and configure Git on Ubuntu 22.04. We will cover two methods: installing the package from the Ubuntu repositories and building Git from the source code. Choose the method that meets your needs.

Installing Git with Apt

This is the most convenient and quickest way to install Git on Ubuntu. If you have some special requirements and want to build Git from the source, move on to the Installing Git from the Source section of this tutorial.

Git comes preinstalled with most Ubuntu systems. Before installing Git, you can check if it is already installed on your system by typing:

git --version

If Git is not installed on your system, the output of the command above will tell you that the command git is not found. Otherwise, it will print the installed Git version.

git version 2.34.1

In case you don’t have Git on your instance, you can install it from Ubuntu’s default repositories using the apt package manager.

Start by executing the following command as a user with sudo privileges to update the local package index:

sudo apt update

Once the update is complete, install Git:

sudo apt install git

You can verify the installation by checking the Git version:

git --version

At the time of writing this article, the current version of Git available in the Ubuntu 22.04 repositories is 2.34.1:

git version 2.34.1

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

When a new version of Git is released, you can update the packages using the standard sudo apt update && sudo apt upgrade procedure.

Installing Git from the Source

The main benefit of installing Git from source is that you can compile any Git version you want and customize the build options. However, you cannot maintain your Git installation through the apt package manager.

Start by installing the dependencies necessary to build Git on your Ubuntu system:

sudo apt updatesudo apt install libcurl4-gnutls-dev libexpat1-dev cmake gettext libz-dev libssl-dev gcc wget

Next, open your browser and visit the Git download page . Here, you can find the latest version of Git.

Installing Git from Source

At the time of writing this article, the latest stable Git version is “2.43.0”:

If you want to install another version of Git, click here , where you can find a list of tarballs and download the version you need.

We’re going to download the latest version and extract the Git source in the /usr/src directory, which is the common location to place source files:

wget -c https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.43.0.tar.gz -O - | sudo tar -xz -C /usr/src

Once the download is complete, navigate to the source directory and execute the following commands to compile and install Git:

cd /usr/src/git-*sudo make prefix=/usr/local allsudo make prefix=/usr/local install

The compilation may take some time, depending on your system.

For changes to take effect on your current shell, you can either log out and log in or run the following [source] command(https://linuxize.com/post/bash-source-command/):

source /etc/environment

Verify the installation by typing:

git --version
git version 2.43.0

Later, if you want to upgrade to a newer version of Git, use the same process.

Configuring Git

After installing Git on your system, it is recommended that you configure your Git username and email address. Git associates your identity with every commit you make.

To set your global commit name and email address, run the following commands:

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

Verify the configuration changes by typing:

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 (recommended) 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 can compile it from the source.

To learn more about Git, Visit the Pro Git book website.

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