// Tutorial //

How To Install Git on Ubuntu 18.04

Published on July 6, 2018 · Updated on July 25, 2022
Default avatar
English
How To Install Git on Ubuntu 18.04
Not using Ubuntu 18.04?Choose a different version or distribution.
Ubuntu 18.04

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:

  1. sudo apt update

With the update complete, you can download and install Git:

  1. sudo apt install git

You can confirm that you have installed Git correctly by running the following command:

  1. git --version
Output
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:

  1. git --version

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

Output
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:

  1. sudo apt update

Then install the packages:

  1. 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:

  1. 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.

  1. 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:

  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 you 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.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:

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

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 your preferred text editor. The following example uses nano:

  1. 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:


Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar
Developer and author at DigitalOcean.

Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?
3 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!

@johnaaronrose — you can look at the official Git documentation for extra guidance. You can click the link for Justin’s tutorial to see the full post, but that is a server setup installation not a desktop. This tutorial also assumes the use of a server. Often, desktops will function the same but there may be differences.

git fails to install on 18.04 using sudo apt install git: john@JohnPC:~$ sudo apt install git [sudo] password for john: Reading package lists… Done Building dependency tree
Reading state information… Done The following additional packages will be installed: git-man liberror-perl Suggested packages: git-daemon-run | git-daemon-sysvinit git-doc git-el git-email git-gui gitk gitweb git-cvs git-mediawiki git-svn The following NEW packages will be installed git git-man liberror-perl 0 to upgrade, 3 to newly install, 0 to remove and 3 not to upgrade. 4 not fully installed or removed. Need to get 4,731 kB of archives. After this operation, 33.9 MB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://gb.archive.ubuntu.com/ubuntu bionic/main amd64 liberror-perl all 0.17025-1 [22.8 kB] Get:2 http://gb.archive.ubuntu.com/ubuntu bionic-updates/main amd64 git-man all 1:2.17.1-1ubuntu0.1 [803 kB] Get:3 http://gb.archive.ubuntu.com/ubuntu bionic-updates/main amd64 git amd64 1:2.17.1-1ubuntu0.1 [3,905 kB] Fetched 4,731 kB in 1s (6,041 kB/s) Selecting previously unselected package liberror-perl. (Reading database … 221319 files and directories currently installed.) Preparing to unpack …/liberror-perl_0.17025-1_all.deb … Unpacking liberror-perl (0.17025-1) … Selecting previously unselected package git-man. Preparing to unpack …/git-man_1%3a2.17.1-1ubuntu0.1_all.deb … Unpacking git-man (1:2.17.1-1ubuntu0.1) … Selecting previously unselected package git. Preparing to unpack …/git_1%3a2.17.1-1ubuntu0.1_amd64.deb … Unpacking git (1:2.17.1-1ubuntu0.1) … Setting up git-man (1:2.17.1-1ubuntu0.1) … Setting up liberror-perl (0.17025-1) … Setting up winbind (2:4.7.6+dfsg~ubuntu-0ubuntu2.2) … Job for winbind.service failed because the control process exited with error code. See “systemctl status winbind.service” and “journalctl -xe” for details. invoke-rc.d: initscript winbind, action “restart” failed. ● winbind.service - Samba Winbind Daemon Loaded: loaded (/lib/systemd/system/winbind.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Thu 2018-08-16 09:01:22 BST; 20ms ago Docs: man:winbindd(8) man:samba(7) man:smb.conf(5) Process: 8571 ExecStart=/usr/sbin/winbindd --foreground --no-process-group $WINBINDOPTIONS (code=exited, status=1/FAILURE) Main PID: 8571 (code=exited, status=1/FAILURE)

I think the title should specify “and configure” as well since we’re configuring our email address as well. Installation is just apt install git but the articles tells a lot more.