Git Installation

Version control systems are essential tools in modern software development. A version control system allows you to track changes to your code, revert to previous stages, and create alternate versions of files and directories. Git is one of the most popular distributed version control systems, allowing many software projects to maintain their files in Git repositories. Platforms like GitHub, GitLab, and Bitbucket use Git to help facilitate software development project sharing and collaboration.

In this tutorial, you will install and configure Git on an Ubuntu server. We will cover two different methods to install the software: using the built-in apt package manager and installing from the source code. Each approach has distinct benefits that may be better for your specific setup. After installing, you will also complete the initial configuration to start using the software. 

You will need an Ubuntu 22.04 server with a non-root superuser account.

With your server and user set up, you are ready to begin.

The option of installing with default packages is best if you want to get up and running quickly with Git, if you prefer a widely-used stable version, or if you are not looking for the newest available functionalities. If you are looking for the most recent release, you should jump to the section on installing from source. 

				
					git --version
				
			

If you receive output similar to the following, then Git is already installed.

				
					Output
git version 2.34.1

				
			

If this is the case for you, then you can move onto setting up Git, or you can read the next section on how to install from source if you need a more up-to-date version.

However, if you did not get the output of a Git version number, you can install it with the Ubuntu default package manager APT.

First, use the apt package management tools to update your local package index

				
					sudo apt update
				
			

With the update complete, you can install Git:

				
					sudo apt install git
				
			
				
					Output
git version 2.34.1
				
			

With Git successfully installed, you can now move on to the Setting Up Git section of this tutorial to complete your setup.

If you’re looking for a more flexible method of installing Git, you may want to compile the software from source. This takes longer and will not be maintained through your package manager, but it will allow you to download the latest release and will give you greater control over the options you include if you wish to make customizations.

 

Verify the version of Git currently installed on the server:

				
					git --version
				
			

If Git is installed, you’ll receive output similar to the following:

				
					Output
git version 2.34.1

				
			

Before you begin, you need to install the software that Git depends on. This is all available in the default repositories, so you can update your local package index and then install the relevant packages.

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

After you have installed the necessary dependencies, create a temporary directory:

				
					mkdir tmp
				
			

Move into your tmp directory where you will download your Git tarball:

				
					cd /tmp
				
			

From the Git project website, navigate to the tarball list available at https://mirrors.edge.kernel.org/pub/software/scm/git/ and download the version you want. At the time of writing, the most recent version is 2.38.1. You will download the latest version for demonstration purposes. Use curl and output the file you download to git.tar.gz.

				
					curl -o git.tar.gz https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.38.1.tar.gz
				
			

Unpack the compressed tarball file:

				
					tar -zxf git.tar.gz
				
			

Next, move into the new Git directory:

				
					cd git-*
				
			

Now, you can make the package with this command:

				
					make prefix=/usr/local all
				
			

This process could take some time to create. When it’s finished, install Git by typing the following command:

				
					sudo make prefix=/usr/local install
				
			

Now, replace the shell process so that the version of Git you just installed will be used:

				
					exec bash
				
			

With this complete, you can be sure that your install was successful by checking the version.

				
					Output
git version 2.38.1

				
			

With Git successfully installed, you can now complete your setup.

After you are satisfied with your Git version, you should configure Git so that the generated commit messages you make will contain your correct information and support you as you build your software project.

Configuration can be achieved by using the git config command. Specifically, you need to provide your name and email address because Git embeds this information into each commit you do. You can add this information by typing:

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

You can display all the configuration items that have been set by typing:

				
					git config --list
				
			
				
					Output
user.name=
Your Name
user.email=youremail@domain.com
...
				
			

The information you enter is stored in your Git configuration file, which you can optionally edit by hand with a text editor of your choice. This example uses nano:

				
					nano ~/.gitconfig
				
			
				
					~/.gitconfig contents

[user]
  name = Your Name
  email = youremail@domain.com
				
			

Press CTRL and X, then Y then ENTER to exit the nano text editor.

There are many other options that you can set, but these are the two essential ones needed. If you skip this step, you’ll likely see warnings when you commit to Git. This makes more work for you because you will then have to revise the commits you have done with the correct information.

You should now have Git installed and ready to use on your system.