sodcalculator

10 Tech How-To

How to Install Git on Ubuntu 20.04 LTS

Guide 10-52511 · Updated · Step by step

This guide walks you through installing Git, the version control system, on Ubuntu 20.04 LTS. You’ll learn the quickest method using the default repositories, how to verify the installation, and how to configure your identity so your commits are properly attributed. No prior experience with Git is required.

What You’ll Need

  • A machine running Ubuntu 20.04 LTS with an active internet connection.
  • Access to a user account with sudo privileges to install packages.
  • A terminal window (Ctrl+Alt+T).

Step-by-Step

  1. Open a terminal. Press Ctrl+Alt+T on your keyboard, or search for “Terminal” in the applications menu.
  2. Update your package list. Run sudo apt update. This refreshes the list of available packages from the Ubuntu repositories. You’ll be prompted for your password; enter it and press Enter.
  3. Install Git. Run sudo apt install git. The system will show you how much disk space the installation will use and ask for confirmation. Type Y and press Enter to proceed.
  4. Verify the installation. After the installation finishes, check the version with git --version. You should see output similar to git version 2.25.1. The exact version number may vary depending on the latest updates in the Ubuntu 20.04 repositories.
  5. Set your Git username. Git needs to know who is making commits. Run the following, replacing “Your Name” with your actual name (keep the quotes):
    git config --global user.name "Your Name"
  6. Set your Git email. Run the following, replacing “you@example.com” with the email address you want associated with your commits:
    git config --global user.email "you@example.com"
  7. Confirm your configuration. Run git config --list to see all your Git settings. You should see the user.name and user.email entries you just set, along with other defaults.
  8. Start using Git. You can now use Git in any directory. For example, to initialize a new repository, navigate to your project folder with cd /path/to/your/project and run git init.

Common Mistakes

  • Forgetting to run sudo. The apt install command requires administrative privileges. Running it without sudo will result in a “Permission denied” error.
  • Not updating first. Skipping sudo apt update can lead to installing an outdated version of Git or, in rare cases, a “package not found” error if the package index is stale.
  • Setting global config in the wrong directory. The --global flag sets the configuration for your current user across all projects. If you omit it, the config is only applied to the current repository, which can cause confusion later.
  • Using an incorrect email address. Git associates commits with the email you set. A typo here can misattribute your work. Double-check the address before proceeding.

Tips

  • Use a meaningful email. If you plan to push code to GitHub, GitLab, or another hosting service, use the same email address you registered with there. This ensures your commits are linked to your account.
  • Check the version periodically. Ubuntu 20.04 LTS ships with Git 2.25.x. If you need a newer version with the latest features, you can add the official Git PPA (Personal Package Archive) with sudo add-apt-repository ppa:git-core/ppa, then run sudo apt update && sudo apt upgrade git. This is optional but useful for developers who want the newest tools.
  • Explore Git help. Run git help or git help <command> (for example, git help commit) to see built-in documentation.
  • Set a default text editor. Git uses an editor for commit messages. If you prefer nano or vim, set it with git config --global core.editor "nano" (or “vim”).

Troubleshooting

  • “E: Unable to locate package git” – This usually means your package list is out of date. Run sudo apt update first, then try the install again. If it persists, check your internet connection and that your /etc/apt/sources.list file contains valid Ubuntu repositories.
  • “Permission denied” when running apt install – You are not using sudo. Prepend sudo to the command. If you still get permission errors, your user may not be in the sudo group. Contact your system administrator.
  • Git is installed but git --version shows nothing – The installation may have failed partway. Run sudo apt install --fix-broken git to repair any broken dependencies, then check again.
  • Changes to global config don’t seem to apply – If you are inside a repository that has its own local config (in .git/config), those local settings override the global ones. Check with git config --list --show-origin to see where each setting is coming from.

Frequently Asked Questions

Do I need to install Git if I only want to download code from GitHub?

Yes. Git is the tool that GitHub uses for version control. To clone (download) a repository, you need Git installed on your system.

What is the difference between git and github?

Git is a version control system that runs locally on your machine. GitHub is a web-based hosting service for Git repositories. You can use Git without GitHub, but GitHub requires Git to interact with it.

How do I uninstall Git?

Run sudo apt remove git to uninstall the software. If you also want to remove configuration files, use sudo apt purge git. Your existing repositories and their .git folders will remain untouched.

Can I install Git using Snap or Flatpak?

Yes, but the apt method is the most straightforward and well-integrated with Ubuntu. Snap (sudo snap install git) and Flatpak are available, but they may have slightly different file paths and permissions. The apt version is recommended for most users.

Why do I need to set user.name and user.email?

Every commit in Git records the author’s name and email. Without these, Git will either prompt you for them or use a default like “root” and “root@hostname”, which is rarely what you want. Setting them globally once saves you from entering them for every repository.

What if I already have an older version of Git installed?

Running sudo apt install git will upgrade it to the latest version available in the Ubuntu 20.04 repositories. You can check your current version with git --version before and after.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →