Install Git featured image

How to Install and Configure Git on Ubuntu 18.04

Introduction

In software development, versioning is an incredibly powerful tool. It allows the developer(s) to keep track of the software at the source level. Versioning helps to keep track of changes, reverting to previous stages, and branching out to create alternate versions of files and directories. Among all the version control systems, Git is probably the most popular one. In the case of the Git system, the project files are maintained in Git repositories. Sites like GitHub, GitLab, and Bitbucket offer to host free/paid Git repositories for all sorts of purposes. Git also makes project sharing and collaboration more efficient.

In this guide, check out how to install Git on Ubuntu 18.04.

Prerequisites

Before we start, you need to make sure that you have a non-root user with sudo privileges on an Ubuntu 18.04 server. You can set up your Ubuntu server by following our step-by-step tutorial. You can follow this tutorial to make sure you have the right non-root user with sudo privileges. Now, let’s start!

Installing Git on Ubuntu

On Ubuntu, there are multiple methods to install Git. The software is directly available from the official Ubuntu repositories. Alternatively, Git can also be built and installed from the source. This guide demonstrates both methods.

Install Git from Ubuntu repo

First, launch a terminal and update the APT local package index:

The following APT command will grab and install Git from the repos:

Verify that Git was installed successfully.

Installing Git from source

Git is an open-source project. It’s possible to download and build the tool manually. While it offers more flexibility, it’s a more complex process. When Git receives an update, this entire process has to be performed again. For general use, it’s recommended to avoid this method.

Prerequisite software

First, we need the system to have all the necessary tools and dependencies required to build Git. All these packages are available directly from the Ubuntu repositories:

Downloading Git source code

The next step is to download the source code. If Git wasn’t previously installed, then the source code has to be downloaded manually. If Git was previously installed, then we can use Git to download the Git source code.

First, check out how to manually download the latest Git source code. Go to the official Git page on GitHub:

From the drop-down menu, select “master” for the branch. As for the tag, select the target version of Git. It’s recommended to use the latest stable version. At the time of writing this article, it’s v2.30.0. Avoid the release candidate (marked as “rc”) versions as those may be unstable:

Next, click the “Code” icon. Select “Download ZIP”. In this example, this will download the source code of Git v2.30.0 in a zipped archive:

Alternatively, the source archive is directly available in the following URL format:

To download Git v2.30.0 source archive using wget, run the following command:

Another alternative is to use Git to clone the Git source repository. If there’s already Git installed, the process is much simpler. Just run the following command:

Git 1

Compiling Git source code

In this section, assume we’re using the Git source code obtained as the zip archive. Extract the archive:

Change the current directory:

Start the compilation process. Using the “-j” flag, “make” will use multiple CPU cores to speed up the process:

Installing Git

The following command will install the freshly built program:

After that, verify if the installation went successfully:

Git configuration

Now you should have Git successfully installed. In order to use it properly, Git requires some information to generate proper commit messages. Git embeds the name and email address into each commit. Add the info using the following commands:

Next, verify if the configurations were set successfully:

Git

This information is stored locally in the Git configuration file. The file is available at the following location:

Git supports lots of additional options. However, these two are the absolutely necessary ones. If skipped, Git may warn when trying to commit to any Git repositories. It increases the workload as each commit has to be revised with the corrected information.

Conclusion

Git is a powerful version controlling system. This guide demonstrated how to install and configure Git on Ubuntu.

Happy computing!