How to Install Git on Ubuntu?

Photo of author

By admin

For every developer and programmer, source code is like life. They spend most of their time writing it, testing it, debugging it, etc. Therefore, they must be aware of any changes made in it. When you are working on a program all by yourself, keeping track of the changes in source code is no big deal.

However, the problem arises when you are working in a team, and the changes in source code are infinite. In such a case, if a problem arises, you need to know the reason behind it. It can be due to changes made in the source code, and to track the changes; you need Git. Git has become the synonym of the version control system and is used widely by DevOps teams around the world.

In the tech world, Git is a technology everyone knows about and uses. Still, the question as to how to install Git on Ubuntu is very frequently asked. This guide is going to answer this question of yours. Here you will learn about Git and the methods to install Git on Ubuntu. The guide also includes processes to install Git on Ubuntu 18.04 and Ubuntu 20.04.

What is Git?

Being a programmer, developer, or a part of a development team, you must be aware of Git, but a little introduction for the beginners will not hurt.

It is a version control system. But what does it do? Git records changes in your source code. It is a tool that manages a repository that records changes made to the source code over some time. Every modification can be tracked with Git, and developers can go back to the older version to check the mistakes made if any.

What makes Git a favorite among the users is that it works well with several operating systems and Integrated Development Environments. Plus, it allows Distributed version control, i.e., every developer can have a working copy of code. It is secure, its performance is unmatched, and it is designed keeping in mind the flexibility.

How to Install Git on Ubuntu?

Now that you have learned about Git and what it does for you, it is time to learn how to install Git on Ubuntu. As said earlier, this guide talks about installing Git on Ubuntu 18.04 and Ubuntu 20.04. You can install Git in two ways:

  • Install with Apt
  • Install from Source

You will learn about installing Git for both versions in both ways.

Let’s talk about installing Git on Ubuntu 18.04 first.

How to Install Git on Ubuntu 18.04?

What you need to install Git on Ubuntu 18.04 are the Ubuntu server and a non-root user with sudo privileges.

Installing with Apt

The easiest way to install Git is to install it with default packages with apt. So, if you want to install Git quickly and easily, it is recommended to install Git with apt. Before going further, check if Git is installed on your Ubuntu system already.

Use the command:

git – – version

The result will look something like this:

git version 2.20.5

It will return the version installed on your system if any. Else, you can move on and follow the steps:

Update the package with:

sudo apt update

Run the following command to install Git:

sudo apt install git

Check the installation with:

git – – version

The output will reflect the latest Git version installed, e.g.

git version 2.17.1

With this the installation of Git on your Ubuntu 18.04 server is complete.

Installing from the Source

It is the second option to install Git from the source which installs the latest version of Git for you. If you install Git this way, you can’t use the apt package manager.

Start by installing the dependencies required:

sudo apt update

sudo apt install make libssl-dev libghc-zlib-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip

Open the browser window after the installation completes and go to GitHub, to Git Project’s mirror. Here you will see a list of the Git releases and their release links. The files end with tar.gz. Choose the version you want to install. For example, you can choose Git 2.23.0.

Change the directory with /usr/src as all the source files are commonly placed here:

cd /usr/src/

Download the selected version of Git with:

sudo wget https://github.com/git/git/archive/v2.23.0.tar.gz -O git.tar.gz

Extract this file in the source directory with:

sudo tar -xf git.tar.gz

cd git-*

Execute these two commands to install Git:

sudo make prefix=/usr/local all

sudo make prefix=/usr/local install

Check the Git version installed on your Ubuntu 18.04 server by running.

git –version

It will return the output as:

git version 2.23.0

Done. You have learned to install git with two methods. Let’s move ahead.

Configuring Git on Ubuntu 18.04

Git is installed on your system, and now you must configure it with your information so that it starts working.

Set your username and email address with:

git config –global user.name “Your Name”

git config –global user.email “youremail@yourdomain.com”

Verify the changes with:

git config –list

It will return your details:

user.name=Your Name

user.email=youremail@yourdomain.com

~/ . gitconfig file stores the config settings which look like:

[user]

name = Your Name

email = youremail@yourdomain.com

It is also possible to make changes to the config file manually or by using the git config command. That is all for Ubuntu 18.04.

How to Install Git on Ubuntu 20.04?

Once again, these are the same two methods to install Git on Ubuntu 20.04. First, check if you are already running a Git version on your system, as you did in the previous section. If it is not, use any of the following methods to install Git on Ubuntu 20.04.

You need an Ubuntu 20.04 server with a sudo privileged non-root user.

The two methods to install Git on Ubuntu 20.04 are:

  • Installing Git with Apt
  • Installing Git from Source

Installing with Apt

Let’s start with the easiest way first.

Update your package with:

sudo apt update

Install Git after updation:

sudo apt install git

Check that you have correctly installed Git with:

git –version

It will show the result with whatever version you have installed e.g. 2.25.1:

git version 2.25.1

This is it.

Installing Git from Source

If you want greater control over options, want to customize Git, or get the latest release, you should choose to install Git from the source. Apt package manager can’t help you here to manage it.

Start with installing the Git dependencies:

sudo apt update

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

You need a temporary directory to download Git which can be created with:

mkdir tmp

cd /tmp

Now go to the Git website and navigate to the section to find the download versions of Git. Choose the latest one, such as 2.31.1, and download it with curl in the following manner:

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

Unpack the tarball file with:

tar -zxf git.tar.gz

Move to the directory you created earlier:

cd git-*

With the following two commands, install Git:

make prefix=/usr/local all

sudo make prefix=/usr/local install

To use the latest installed version, replace the shell process with:

exec bash

Check the version with:

git –version

It will reflect the result as:

git version 2.31.1

The installation is complete.

Configuring Git on Ubuntu 20.04

After installation, you must configure Git to get started. Git config command is used for configuration. Your name and email address will be required. You can configure Git with this command:

git config –global user.name “Your Name”

git config –global user.email “youremail@domain.com”

You can check it with:

git config –list

It will show the result as:

user.name=Your Name

user.email=youremail@domain.com

The configuration is now complete, and you are all set to use Git.

Conclusion

The small tools in the tech world can prove to be very helpful. Git is one of those tools. Keeping track of the changes in the source code is crucial in the present times when technology plays a vital role in allowing shared working. Git allows you to turn the clock back and go to the previous versions of the code and debug the code, check the changes made and by whom, etc.

Today, DevOps find it extremely crucial to use Git. This version control system has become the synonym of performance, security, and flexibility. Installing it on your system is very easy, provided that you have a non-root user with sudo privileges. This guide has covered two different methods for each of the Ubuntu server versions — version 18.04 and version 20.04. Along with learning how to install Git on Ubuntu, you can also learn how to configure it with your personal information. The quick and easy steps are not hard to follow, and you will not face any difficulty installing Git on Ubuntu.

Leave a Comment