How To Install Git on Ubuntu 18.04
Table of Contents
Introduction #
Version control systems are increasingly indispensable in modern software development as versioning allows you to keep track of your software at the source level. You can track changes, revert to previous stages, and branch to create alternate versions of files and directories.
One of the most popular version control systems currently available is Git. Many projects’ files are maintained in a Git repository, and sites like GitHub, GitLab, and Bitbucket help to facilitate software development, project sharing, and collaboration.
In this guide, you will install and configure Git on an Ubuntu 18.04 server. This guide will cover how to install the software two different ways: via the built-in package manager, and via source. Each of these approaches come with their own benefits depending on your specific needs.
Prerequisites #
In order to complete this tutorial, you should have a non-root user with sudo
privileges on an Ubuntu 18.04 server. To learn how to achieve this setup, follow our Initial Server Setup Guide.
With your server and user set up, you are ready to begin.
Installing Git with Default Packages #
Ubuntu’s default repositories provide you with a fast method to install Git. Note that the version you install via these repositories may be older than the newest version currently available. If you need the latest release, consider moving to the next section of this tutorial to learn how to install and compile Git from source.
First, use the apt package management tools to update your local package index:
sudo apt update
With the update complete, you can download and install Git:
sudo apt install git
You can confirm that you have installed Git correctly by running the following command:
git --version
git version 2.17.1
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 #
A more flexible method of installing Git is to compile the software from source. This takes longer and will not be maintained through your package manager, but it will allow you to download the latest release and will give you some control over the options you include if you wish to customize.
Verify the version of Git currently installed:
git --version
If Git is installed, you’ll receive output similar to the following:
git version 2.17.1
Before you begin, you need to install the software that Git depends on. This is all available in the default repositories, so you can update your local package index:
sudo apt update
Then install the packages:
sudo apt install libz-dev libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext cmake gcc
After you have installed the necessary dependencies, move into the tmp
directory. This is where you will download your Git tarball:
cd /tmp
From the Git project website, you can navigate to the tarball list available at
https://mirrors.edge.kernel.org/pub/software/scm/git/ and download the version you of your choosing. At the time of writing, the most recent version is 2.37.1. Use curl
and output the file downloaded to git.tar.gz
.
curl -o git.tar.gz https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.37.1.tar.gz
Unpack the compressed tarball file:
tar -zxf git.tar.gz
Next, move into the new Git directory:
cd git-*
Now, you can make 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 so that the version of Git you just installed will be used:
exec bash
With this complete, you can be sure that your install was successful by checking the version:
git --version
git version 2.37.1
With Git successfully installed, you can now complete your setup.
Setting Up Git #
After you are satisfied with your Git version, you should configure Git so that the generated commit messages you make will contain your correct information and support you as you build your software project.
This can be achieved by using the git config
command. Specifically, you need to provide your name and email address because Git embeds this information into each commit you do. Add this information by typing:
git config --global user.name "Your Name"
git config --global user.email "youremail@domain.com"
Display all of the configuration items that have been set by typing:
git config --list
user.name=Your 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 your preferred text editor. The following example uses nano
:
nano ~/.gitconfig
~/.gitconfig contents
[user]
name = Your Name
email = youremail@domain.com
Press CTRL + X
, then Y
then ENTER
to exit the nano
text editor.
There are many other options that you can set, but these are the two essential ones needed. If you skip this step, you’ll likely see warnings when you commit to Git. This makes more work for you because you will then have to revise the commits you have done with the corrected information.
Conclusion #
Git is a great way to keep track of changes, revert to previous stages, or branch to create different versions of files and directories. With this tutorial, you’ve learned how to install Git on your system and how to set up the essential Git configurations.
To learn more about how to use Git, check out these articles and series:
How To Use Git Effectively
How To Use Git Branches
An Introduction to Open Source