Git is the world's most popular distributed version control system, used by many open source and commercial projects. It lets you collaborate on projects with other developers, track 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.
Install Git with Apt
This is the most convenient and fastest way to install Git on Ubuntu.
Git comes pre-installed 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 above command will tell you that the git
command was not found. Otherwise, it will print the installed Git version.
git version 2.34.1
If you don't have Git on your instance, you can install it from the default Ubuntu repositories using apt
package manager.
Start by running the following command as a user with sudo privileges to update the local package index:
sudo apt update
Once the upgrade is complete, install Git:
sudo apt install git
You can verify your installation by checking your Git version:
git --version
As of this writing, 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 can start configuring it.
When a new version of Git is released, you can upgrade packages using the standard sudo apt update && sudo apt upgrade
procedure.
Install Git from source
The main advantage of installing Git from source is that you can compile any version of Git you want and customize the build options. However, you cannot maintain your Git installation via the apt
package manager.
Start by installing the dependencies needed to build Git on your Ubuntu system:
sudo apt update
sudo 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.
As of this writing, the latest stable version of Git 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.
Download the latest version and extract the Git source to 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, go to the source directory and run the following commands to compile and install Git:
cd /usr/src/git-*
sudo make prefix=/usr/local all
sudo make prefix=/usr/local install
It may take some time to compile, depending on your system.
For the changes to take effect on the current shell, you can log out and log in or run the following source
command:
source /etc/environment
Verify the installation by typing:
git --version
git version 2.43.0
Next, if you want to upgrade to a newer version of Git, use the same process.
Configure Git
After installing Git on your system, you'll want to set up your Git username and email address. Git associates your identity with every commit you make.
To set the global commit name and email address, run the following commands:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Test your configuration changes by typing:
git config --list
The output should look like this:
user.name=Your Name
[email protected]
Configuration settings are stored in the ~/.gitconfig
file:
[user]
name = Your Name
email = [email protected]
If you want to make additional changes to your Git configuration, you can use the git config
command (recommended) or manually edit the ~/.gitconfig
file.
Conclusion
Installing Git on Ubuntu is a matter of running a single apt
command. If you want to use the latest version of Git, you can compile it from source.
To learn more about Git, visit the Pro Git book website.