How to install Git on Ubuntu

0 Shares
0
0
0
0

Introduction

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

Many software project files are stored in Git repositories, and platforms like GitHub, GitLab, and Bitbucket help facilitate software development project sharing and collaboration.

Installing Git with default packages

If you prefer a stable, widely used version or don't need the latest features, the default package installation option is the best option for a quick installation with Git. If you're looking for the latest version, you'll need to go to the Install from Source section so you can select the specific version you want to install.

Git is probably already installed on your Ubuntu server. You can verify this on your server with the following command:

git --version

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

Output
git version 2.25.1

If this is the case for you, if your Git version is outdated, you may need to update your Git version and then you can continue to set up your version.

If you don't get the Git version number output, you need to install it with APT, Ubuntu's default package manager.

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

sudo apt update

Once the update is complete, you can install Git:

sudo apt install git

You can verify that you have installed Git correctly by running the following command and checking that you get the corresponding output.

git --version
Output
git version 2.45.2

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

Installing Git from source

If you're looking for a more flexible way to install Git, you may want to compile the software from source, which we'll cover in this section. This takes longer and isn't maintained through your package manager, but it allows you to download the latest version and gives you more control over the options you add if you want to customize it.

Verify the version of Git currently installed on the server:

git --version

If Git is installed, you will get output similar to the following:

Output
git version 2.25.1

Before we begin, you need to install the software that Git depends on. All of this is available in the default repositories, so we can update our local package list 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 and navigate to it. This is where we will download our Git tarball.

mkdir tmp
cd /tmp

From the Git project website, we can access the tarball directory available at https://mirrors.edge.kernel.org/pub/software/scm/git/ Let's go ahead and download the version we want. At the time of writing, the latest version is 2.26.2, so we'll download that for demonstration purposes. We'll use curl and extract the file we download into 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 tarball file:

tar -zxf git.tar.gz

Next, go to the new Git directory:

cd git-*

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

make prefix=/usr/local all
sudo make prefix=/usr/local install

Now, replace the shell process to use the version of Git we just installed:

exec bash

With this complete, you can make sure your installation was successful by checking the version.

git --version
Output
git version 2.26.2

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

Update Git

Before setting up Git, you should first make sure you are using the latest stable version as they can become outdated quickly.

To update Git, first update your package lists:

sudo apt update

Git update:

sudo apt install git

Check that you have the latest stable version of Git.

git --version

Setting up Git

After you are satisfied with your Git version, you need to configure Git so that the commit messages it generates contain your correct information and support you as you build your software project.

The configuration can be obtained using the git config command. Specifically, we need to provide our name and email address because Git embeds this information in every commit we make. We can go ahead and add this information by typing:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

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

git config --list
Output
user.name=Your Name
[email protected]
...

The information you enter is stored in your Git configuration file, which you can optionally edit with a text editor of your choice, like this (we'll use nano):

nano ~/.gitconfig
[user]
name = Your Name
email = [email protected]

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

There are many options you can set, but these two are essential. If you skip this step, you will likely see warnings when you commit to Git. This will create more work for you because you will then have to revise the commits you made with the corrected information.

Result

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like