How To Install Git on Ubuntu 20.04 – DigitalOcean

Version

control systems like Git are essential to modern software development best practices. Version control allows you to track your software at the source level. You can track changes, revert to previous stages, and branch to create alternate versions of files and directories.

Many software project files are maintained in Git repositories, and platforms such as GitHub, GitLab, and Bitbucket help facilitate the sharing and collaboration of software development projects.

In this guide, we will see how to install and configure Git on an Ubuntu 20.04 server. We’ll cover how to install the software in two different ways: through the built-in package manager and through the source. Each of these approaches comes with its own benefits depending on your specific needs.

Prerequisites

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

To set this up, you can follow our Initial Server Setup Guide for Ubuntu 20.04.

With the server and user set up, you’re ready to go.

Installing Git with default packages The option to install 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’re not looking for the newest functionalities available. If you are looking for the latest version, you should skip to the section about installing from source.

It is likely that Git is already installed on your Ubuntu 20.04 server. You can confirm that this is the case on your server with the following command:

  1. git -version

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

Outputgit version 2.25.1 If this is the case for you, you can move on to

configuring Git, or you can read the next section on installing from source if you need a more up-to-date version

.

However, if you didn’t get the output of a Git version number, you can install it with Ubuntu’s default package manager APT.

First, use the APT package management tools to update the local package index.

sudo apt update With the

update complete

, you can install Git:

  1. sudo apt

install git You can confirm that you have successfully installed Git

by running the following command and verifying that you receive the relevant result

.

  1. git -version

Outputgit version 2.25.1 With Git

installed successfully, you can now move to the Git Configuration section of this tutorial to complete your setup. Installing

Git from the

source

If you’re looking for a more flexible method of installing

  1. Git

,

you may want to build the software from source, which we’ll go over in this section. This takes more time and won’t be maintained through your package manager, but it will allow you to download the latest version and give you more control over the options it includes if you want to make customizations.

Check the version of Git currently installed on the server:

  1. git –

version If Git is

installed

, you

receive output similar to the following:

Outputgit version 2.25.1 Before you

begin, you must install the software that Git depends on. All of this is available in the default repositories, so we can update our local package index and then install the relevant packages.

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

After you have installed the required dependencies, create a temporary directory and move to it. This is where we will download our Git tarball.

  1. mkdir tmp
  2. cd /tmp

From the Git project website, we can navigate to the tarball list available on https://mirrors.edge.kernel.org/pub/software/scm/git/ and download the version we want. At the time of writing, the most recent version is 2.26.2, so we will download it for demonstration purposes. We will use curl and generate the file that we downloaded on git.tar.gz.

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

Unzip the compressed tarball file

: tar –

  1. zxf

git.tar.gz

Next, go to the new Git directory

:

  1. cd git-*

Now, you can create the package and install it by typing these two commands:

make prefix=/usr/local

    all

  1. sudo make prefix=/usr/local install

Now, replace the shell process so

that the version of Git we just installed is used:

  1. exec bash

With

this complete, you can be sure that your installation was successful by verifying the

version.

  1. git -version

Outputgit version 2.26.2 With Git

installed successfully, you can now complete your setup

. Configuring Git

Once you are satisfied with your version of Git, you should configure Git so that the generated confirmation messages you make contain your correct information and support you while you create your software project.

Configuration can be achieved using the git config command. Specifically, we need to provide our name and email address because Git incorporates this information into every engagement we make. We can go ahead and add this information by

typing: git config -global user.name “Your Name” git config -global user.email “youremail@domain.com”

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

  1. git config -list

Outputuser.name=Your

  1. 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 like this one (we’ll use nano):

  1. nano ~/.gitconfig

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

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

There are many other options you can configure, but these are the two necessary essentials. If you skip this step, you’ll likely see warnings when you commit to Git. This does more work for you because you will then have to review the confirmations you have made with the corrected information.

Conclusion

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

For more information about using Git, see these articles and series:

How to use Git

  • effectively How to use
  • Git branches
  • An introduction to open source