Tutorial

How To Install Git on Ubuntu

Updated on July 1, 2024
English
How To Install Git on Ubuntu
Not using Ubuntu 20.04?Choose a different version or distribution.
Ubuntu 20.04

Introduction

Version control systems like Git are essential to modern software development best practices. 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.

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

In this guide, we will go through how to install and configure Git on an Ubuntu server. We will cover how to install the software two different ways:

  1. via the built-in package manager,
  2. Or via source.

Each of these approaches come with their own benefits depending on your specific needs.

Simplify deploying applications with DigitalOcean App Platform. Deploy directly from GitHub in minutes.

Prerequisites

You will need an Ubuntu server with a non-root superuser account.

To set this up, you can follow our Initial Server Setup Guide for Ubuntu.

With your server and user set up, you are ready to begin.

Installing Git with Default Packages

The option of installing with default packages is best if you want to get up and running quickly with Git if you prefer a widely-used stable version or if you don’t need the newest available functionalities. If you are looking for the most recent release, you should jump to the section on installing from source so you can choose the specific version you want to install.

Git is likely already installed in your Ubuntu server. You can confirm this is the case on your server with the following command:

  1. git --version

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

Output
git version 2.25.1

If this is the case for you, you may need to update the Git version if yours is outdated and then you can move on to setting up your version.

If you did not get output of a Git version number, you will need to install it with the Ubuntu default package manager APT.

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

  1. sudo apt update

With the update complete, you can install Git:

  1. sudo apt install git

You can confirm that you have installed Git correctly by running the following command and checking that you receive relevant output.

  1. 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 method of installing Git, you may want to compile the software from source, which we will go over in this section. 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 greater control over the options you include if you wish to make customizations.

Verify the version of Git currently installed on the server:

  1. git --version

If Git is installed, you’ll receive output similar to the following:

Output
git version 2.25.1

Before you begin, you need to install the software that Git depends on. This is all available in the default repositories, so we can update our local package index and then install the relevant packages.

  1. sudo apt update
  2. 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 move into it. This is where we will download our Git tarball.

  1. mkdir tmp
  2. cd /tmp

From the Git project website, we can navigate to the tarball list available at https://mirrors.edge.kernel.org/pub/software/scm/git/ and download the version you would like. At the time of writing, the most recent version is 2.26.2, so we will download that for demonstration purposes. We’ll use curl and output the file we download to git.tar.gz.

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

Unpack the compressed tarball file:

  1. tar -zxf git.tar.gz

Next, move into the new Git directory:

  1. cd git-*

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

  1. make prefix=/usr/local all
  2. sudo make prefix=/usr/local install

Now, replace the shell process so that the version of Git we just installed will be used:

  1. exec bash

With this complete, you can be sure that your install was successful by checking the version.

  1. 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 quickly get out of date.

To update Git you first, update your package lists:

sudo apt update

Update Git:

sudo apt install git

Verify you have the latest stable Git Version

git --version

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.

Configuration can be achieved by using the git config command. Specifically, we need to provide our name and email address because Git embeds this information into each commit we do. We can go ahead and add this information by typing:

  1. git config --global user.name "Your Name"
  2. git config --global user.email "youremail@domain.com"

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

  1. git config --list
Output
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 a text editor of your choice like this (we’ll use nano):

  1. nano ~/.gitconfig
~/.gitconfig contents
[user]
  name = Your Name
  email = youremail@domain.com

Press CTRL and X, then Y then ENTER to exit the 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

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

To learn more about how to use Git, check out these articles and series:

Get Ubuntu on a hosted virtual machine in seconds with DigitalOcean Droplets! Simple enough for any user, powerful enough for fast-growing applications or businesses.

Learn more here


Tutorial Series: Introduction to Git: Installation, Usage, and Branches

This series covers the installation and usage of git on an Ubuntu server. After completing the series, the reader should feel comfortable installing and using git, as well as how to create two branches (master and develop) and how to merge code from the development stage to production.

About the authors

Default avatar

Sr Technical Writer

Senior Technical Writer @ DigitalOcean | 2x Medium Top Writers | 2 Million+ monthly views & 34K Subscribers | Ex Cloud Consultant @ AMEX | Ex SRE(DevOps) @ NUTANIX


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
4 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Thank you for this tutorial Lisa.

I just want to add this informations:

For Ubuntu, this PPA provides the latest stable upstream Git version

# sudo add-apt-repository ppa:git-core/ppa 
# sudo apt update
# sudo apt install git

I hope this will help others and maybe updated in this tutorial :p

This information was found : here

and you can find more informations about git : here

Happy coding !

Your teaching style is really smooth!

Can you tell me please why there is a sentence - You will need an Ubuntu 20.04 server with a non-root superuser account.

Why I need a non-root user to set up Git?

Thanks for the awesome tutorial, Lisa. I would like to add the login when you’re using 2FA. tldr - generate new token and use it instead the password (https://stackoverflow.com/questions/29297154/github-invalid-username-or-password)

This is SUPER not obvious so will be awesome to be seen often to shorten the search adventure for everyone.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.