How to Install and Configure Git on Ubuntu 22.04?


Git is a free-to-use distributed version-controlled system used extensively by the software developer community to track modifications in the code and revert to an error-free version of the code. Git enables collaboration enabling software developers to contribute to a project from different parts of the world. This article will demonstrate different methods of installing Git along with the configuration of Git on Ubuntu 22.04 LTS. This article is organized as follows:

  • How to Install Git on Linux/Ubuntu?
  • How to Configure Git on Linux/Ubuntu?
  • How to Uninstall Git From Linux/Ubuntu?

How to Install Git on Linux/Ubuntu?

Ubuntu is a frequently operated feature-rich, free, secure, and resource-friendly Linux distribution. It is an open-source distribution that enables its users to install any particular software of their choice without any hurdles.

To install Git on Ubuntu 22.04, users can use the following methods:

  • Method 1: How to Install Git via Ubuntu Repository?
  • Method 2: How to Install Git via PPA?
  • Method 3: How to Install Git via Source

These methods are discussed in the following sections

Method 1: How to Install Git via Ubuntu Repository?

Ubuntu repository contains the latest and most stable version of Git. To install Git via the Ubuntu repository, the following steps are performed:

Step 1: Update System Repositories

Launch the terminal via keyboard with a combination of [Ctrl+Alt+T] keys:

Then, update the system repositories by running the following command:


Step2: Install Git

Next, git can be installed by the default package manager, i.e., apt (Advance Packet Tool) by running the following command:


The user will be prompted during installation. Press ‘Y’ to continue with the installation.

The above indicates the successful installation of Git.

Step 3: Verify Installation

To verify Git installation, execute the following command:

From the above figure, it can be verified that Git version 2.34.1 is installed successfully.

Method 2: How to Install Git via PPA?

The latest version of the software is stored in the PPA (Personal Package Archive) repository. Git is installed via the PPA repository by executing the following steps:

Step 1: Update System Repositories

Update the system repositories by running the following command:


Step 2: Adding PPA Repository

The Git package is available in git-core and can be added via the PPA repository by executing the following command:

$ sudo add-apt-repository ppa:git-core/ppa

Press “Enter” at the prompt to continue:

The above figure indicates the successful addition of the git package (git-core) via PPA in the system repository list.

Step 3: Update System Repositories

Next, update the “apt” repository by running the following command:


Step 4: Install Git

Install the latest version of git added to system repositories via PPA can be installed by executing the following command:


Press “Y” to continue with the installation process.

The installation of git will conclude in a few seconds. The above output indicates the successful installation of git.

Step 5: Verify Installation

To verify the git installation, execute the following command:


From the above image, it can be seen that the latest version, i.e., 2.42.0 is installed successfully.

Method 3: How to Install Git via Source?

Another option to install Git is to build from the source. Software is usually available from the software developer’s website in the form of compressed archives, among which tarball is one of the popular archive formats. All previous versions of git can be installed via tarball. The following steps can be followed to install “git” by building from the source:

Step 1: Update System Repositories

Update the “apt” repository by running the following command:


Step 2: Download Tarball

To download the tarball of Git, access the Git Official Website and then press the “Download for Linux” button:

This will redirect to the “Download for Linux and Unix” page, where click the latest version, i.e., 2.42.0 as shown in the figure below:

This will start to download the tarball, i.e., git-2.42.0.tar.gz which will take a few seconds and the archive will be downloaded in the “Downloads” directory. Alternatively, check the latest version from Git’s website and use the wget command to download the tarball:

$ wget https://www.kernel.org/pub/software/scm/git/git-2.42.0.tar.gz

The archive, i.e., git-2.42.0.tar.gz is saved in downloads. Next, navigate to the “Downloads” directory by using cd command as follows:

Step 2: Install Additional Packages

Additional packages, i.e., tar extraction and compiling tools can be installed via “apt” by executing the below-mentioned command:

$ sudo apt-get install libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev build-essential

Press “Y” to continue with the installation process.

Step 3: Extract the Contents of Tarball

After tar extraction and compiling tools are installed, extract the compressed archive via the tar command:

$ tar -zxf git-2.42.0.tar.gz

The above output indicates that “git-2.42.0.tar.gz” is successfully extracted to “git-2.42.0”.

Step 4: Run Configuration Script

Next, navigate inside the “git-2.42.0” directory via the “cd” command:


After that, verify if an executable file “configure” exists in the extracted archive by listing the contents of git-2.42.0 of the directory via the “ls” command:

From the below image, it can be seen that an executable file “configure” exists in the git-2.42.0 directory.

Next, run the configuration script by the below command:


The configuration step will take a few seconds to complete.

Step 5: Build and Compile Git’s Source Code

To build git, run the following command:


The building process will run for a few minutes depending on the system hardware and the speed of the internet.

Finally, install git by running the following command:


The installation process will take a few seconds to complete.

Step 6: Verify Installation

To verify the git installation, run the following command:


The above output indicates that the latest version, i.e., 2.42.0 is installed successfully.

How to Configure Git on Linux/Ubuntu?

After Git installation is completed, the subsequent step is to configure git. In this section, basic steps to configure Git are discussed:

How to Check Git Configuration?

To check Git configurations run the following command:


From the above image, it can be seen that the command didn’t return any output which indicates that the git repository is not configured yet.

How to Configure a User in Git?

In order to ensure that the commit messages contain the correct information, git needs to be configured using the “git config” command by the following steps:

Step 1: How to Specify a User Name

Specify the GitHub username by the following command:

$ git config –global user.name “linuxuser”

In the above command, “linuxuser” is the username.

Step 2: Specify the Email Address

Specify the email address by the following command:

$ git config –global user.email “[email protected]

In the above command, [email protected]is the email address.

The changes made in the above steps can be verified by the following command:


How to Initialize Git?

Git is initialized by first navigating to the directory with the “cd” command and then by using the “git init” command, which creates a hidden git directory. The history, configurations, etc are stored in the created hidden git directory:

$ cd cppProject
$ git init

From the above image, it can be verified that the empty Git repository is initialized.

How to Check the Status of Git?

To check git status, run the following command:


From the above image, it can be seen that there are no commits to the repository yet.

How to Uninstall Git From Linux/Ubuntu?

Git can be removed by executing the following command:


Conclusion

Git can be installed on Ubuntu 22.04 by three methods: Ubuntu repository, PPA, and building from the source. To install Git via the Ubuntu repository execute the “sudo apt install git“ command after updating the system’s repositories. Whereas, to install git using PPA, first, execute the “sudo add-apt-repository ppa:git-core/ppa” command, and then run the “sudo apt install git” command. In contrast,

to install Git via source, first download the tarball of Git’s latest version, then build and compile the source code. This article demonstrated three different methods of installing Git along with the configuration of Git on Ubuntu 22.04 LTS.

Print Friendly, PDF & Email