How to Install Git on Ubuntu Linux

Git tracks code changes, manages project history, and enables collaboration through distributed workflows. Unlike Windows tools such as GitHub Desktop or Git Bash that bolt Git onto the operating system, Ubuntu exposes Git directly inside the terminal developers already use. The system scales from solo file tracking to complex branching across platforms like GitHub, GitLab, and Bitbucket.

Ubuntu supports three installation paths to match your stability and feature needs: the default APT repository for well-tested releases, the Ubuntu Git Maintainers PPA for near-upstream builds, and source compilation for full control. This guide walks through each method plus configuration and troubleshooting so Git runs exactly the way you need on Ubuntu.

Choose Your Git Installation Method

Ubuntu offers multiple installation paths depending on your stability and version requirements. The default APT repository provides well-tested releases that match Ubuntu’s stability guarantees but typically lags several versions behind the latest Git release. In contrast, the Ubuntu Git Maintainers PPA delivers current stable releases within days of upstream publication while maintaining Ubuntu integration. Meanwhile, source compilation gives you immediate access to the absolute latest code and custom build options but requires manual updates and dependency management.

MethodVersion/ChannelStabilityBest For
APT RepositoryUbuntu-tested stableHighestProduction servers, minimal maintenance, proven stability
Git PPALatest stable releaseHighCurrent features, security patches, regular Git users
Source BuildBleeding-edgeVariableDevelopment, testing, custom configurations, learning

Most users should start with the default APT repository for reliability. However, choose the PPA when you need current Git features or security patches faster than Ubuntu’s release schedule provides. On the other hand, reserve source compilation for development environments where you need unreleased features or want to understand Git’s build process.

Method 1: Install Git via Default APT Repository

Before installing Git, update your system packages to prevent conflicts:

sudo apt update && sudo apt upgrade

Check for Existing Git Installation

Next, check if Git is already installed on your system to avoid redundant installations:

Use the --version flag to check for an existing Git installation:

git --version

Install Git via APT Command

If the command prints git: command not found or a similar error, Git is not installed. Install Git from Ubuntu’s repository:

sudo apt install git

Verify Git Installation

After installation, confirm Git installed successfully and check the version:

git --version

The output shows the installed version:

git version 2.43.0

Method 2: Install Git via Ubuntu Git Maintainers PPA

The Ubuntu Git Maintainers team provides a Personal Package Archive (PPA) with the latest stable Git releases. As a result, this method delivers newer features and bug fixes faster than Ubuntu’s default repository.

Import Ubuntu Git Maintainers PPA

Minimal Ubuntu installs may lack the add-apt-repository helper. Install the required package first:

sudo apt install software-properties-common -y

First, add the Git PPA to your system’s repository list for access to the latest stable release:

sudo add-apt-repository ppa:git-core/ppa -y

Update Package Index After PPA Import

Next, update the package index so your system recognizes the newly available packages:

sudo apt update

Install Git via PPA

Now install or upgrade Git from the PPA:

sudo apt install git -y

If you previously installed Git from Ubuntu’s repository, this command upgrades Git to the latest version from the PPA.

After installation, verify the installed Git version:

git --version

The output shows the new version:

git version 2.47.1

Additionally, check which repository your Git installation originates from:

apt-cache policy git

Method 3: Install Git via Source Archive

Building Git from source provides access to the absolute latest version, custom compilation flags, and features not yet available in distribution packages. Consequently, this method suits users who need bleeding-edge development or want to learn the build process.

Install Build Dependencies

First, install the required build dependencies for Git compilation:

sudo apt install build-essential libssl-dev zlib1g-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip -y

Download Git Source Code

Next, download Git source code from the Git release page. The example below pulls v2.47.1; replace that version string if you need a different release. The -O flag saves the archive with a predictable filename for the next commands:

Always confirm the latest Git version on the release page before running these commands and update every v2.47.1 reference to match the version you actually download. Copying the commands verbatim without swapping the version string installs an outdated build.

Source installations are not tied to APT updates, so plan to revisit this section whenever a new Git release ships and rebuild from the fresh tarball to stay current.

wget https://github.com/git/git/archive/refs/tags/v2.47.1.tar.gz -O git-v2.47.1.tar.gz

Extract and Compile Git Source Code

After downloading, extract the downloaded archive. Update the version string if you downloaded a different release. The tar command unpacks the compressed source files:

tar -xvf git-v2.47.1.tar.gz

Then change into the extracted directory. Git strips the leading v from the folder name, so version v2.47.1 becomes git-2.47.1 after extraction:

cd git-2.47.1

Next, compile Git with the installation prefix set to /usr/local:

make prefix=/usr/local all

After compilation completes, install the compiled binaries:

sudo make prefix=/usr/local install

Next, reload your shell environment so the system recognizes the newly installed Git binary:

exec bash

Finally, verify the installation:

git --version

Configure Git

After installation, configure your identity before using Git so commits are properly attributed to you across all repositories. This identity appears in project history, pull requests, and collaboration platforms like GitHub or GitLab. Think of it like signing into GitHub Desktop on Windows; it ties your work to your identity.

First, set your name and email address globally:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Additionally, configure additional helpful settings for a better Git experience:

git config --global init.defaultBranch main
git config --global core.editor nano
git config --global color.ui auto

These commands set the default branch name to main for new repositories, configure nano as the default text editor for commit messages, and enable colored terminal output for better readability.

Next, verify your configuration:

git config --list

The output displays your configured settings:

user.name=Your Name
user.email=your.email@example.com
init.defaultbranch=main
core.editor=nano
color.ui=auto

These settings apply to all Git repositories on your system. However, you can override them per-repository by omitting the --global flag inside a specific repository directory.

Your global Git configuration lives in ~/.gitconfig in your home directory. Consider backing up this file when you’ve customized aliases, editor preferences, or other workflow settings. Storing your .gitconfig in a Git repository lets you sync settings across multiple Ubuntu machines or restore your development environment quickly after reinstallation.

Common Installation Issues and Solutions

Even straightforward Git installations can hit unexpected snags. The following issues surface frequently on Ubuntu systems, especially when mixing installation methods or working with remote repositories for the first time.

Outdated Git Version from Default Repository

Ubuntu’s default repository prioritizes stability over currency, meaning the included Git version often lags several releases behind the latest stable release. This becomes problematic when you encounter newer Git commands like git switch or git restore in documentation, tutorials, or team workflows, only to find they don’t work on your system because your Git version predates those features.

Switch to the Ubuntu Git Maintainers PPA when you need current features. The PPA delivers the latest stable Git release while maintaining Ubuntu package integration, so you receive updates through your normal apt upgrade cycle without sacrificing the convenience of package management.

Missing User Identity Configuration

Git refuses to create commits until you configure your identity with user.name and user.email. Without this configuration, Git displays error messages when you attempt your first commit, stopping your work until you provide the required information. Even worse, some users configure a placeholder email that doesn’t match their GitHub or GitLab account, creating commits that appear unattributed in web interfaces and breaking contribution statistics.

Configure your identity immediately after installation using the exact email address linked to your GitHub, GitLab, or Bitbucket account. This ensures your commits display properly attributed authorship across all platforms and maintains accurate contribution tracking in project insights and profile statistics.

Credential Storage and Authentication

When you first push to a remote repository over HTTPS, Git prompts for your username and password on every operation unless you configure credential storage. Some users configure credential.helper store which saves credentials in plain text at ~/.git-credentials, creating a security risk if someone gains access to your home directory. Others skip credential helpers entirely and repeatedly type passwords dozens of times per day, wasting time and creating frustration.

Use SSH keys instead of HTTPS authentication for a more secure and convenient workflow. SSH keys provide strong authentication without exposing passwords, and once configured, Git never prompts for credentials again.

Generate an SSH key pair:

ssh-keygen -t ed25519 -C "your.email@example.com"

Then add the public key from ~/.ssh/id_ed25519.pub to your GitHub, GitLab, or Bitbucket account settings. When cloning repositories, use the SSH URL format (git@github.com:user/repo.git) instead of HTTPS to authenticate with your key automatically.

Conflicting Installation Methods

Installing Git from both APT and source simultaneously creates conflicts where your system maintains two separate Git installations in different paths. The shell might execute the older APT version while you expect the newer source build, or worse, commands might behave inconsistently depending on how you invoke Git. This confusion makes troubleshooting difficult and wastes time debugging version mismatches.

Choose one installation method and stick with it. For most users, the PPA method provides the best balance of currency and maintainability. If you previously installed from source and want to switch to APT, remove the source installation completely using the steps in the removal section before installing via package manager.

Default Branch Name Mismatch

Older Git versions default to master for the initial branch when you run git init, but GitHub, GitLab, and most modern platforms now use main as the default branch name. This mismatch creates confusion when you push a new repository to a remote that expects main, forcing you to rename branches or configure upstream tracking manually before your first push succeeds.

Configure Git to use main as the default branch name for new repositories:

git config --global init.defaultBranch main

This setting aligns your local workflow with modern platform conventions and eliminates the branch naming mismatch that trips up new repository creation.

Manage Your Git Installation

Update Git

APT Update Method

Update Git installed via APT or PPA with your system’s regular update process:

sudo apt update && sudo apt upgrade

Source Update Method

Alternatively, update source installations by downloading the latest version and repeating the compilation process from Method 3.

Remove Git

APT Remove Method

First, uninstall Git installed via APT:

sudo apt remove git

Additionally, if you installed Git via PPA, also remove the PPA:

sudo add-apt-repository --remove ppa:git-core/ppa -y

Remove Source Installation

Removing source installations requires manually deleting files since no package manager tracks them. Based on the installation prefix /usr/local used in Method 3, first navigate to the directory:

cd /usr/local

The following commands permanently delete files. Verify the directory contents before executing to avoid removing unintended files. Use the following command to check what will be deleted:

ls -la bin/git* libexec/git-core share/doc/git* share/man/man1/git* share/man/man5/git* share/man/man7/git*

Next, remove Git files and directories:

sudo rm -rf git* 
sudo rm -rf bin/git* 
sudo rm -rf libexec/git-core
sudo rm -rf share/doc/git*
sudo rm -rf share/man/man1/git*
sudo rm -rf share/man/man5/git*
sudo rm -rf share/man/man7/git*

Finally, verify Git was removed by checking for the command:

git --version

If successful, the terminal returns an error stating the command is not found.

Conclusion

Git delivers powerful version control for tracking code changes, managing project history, and enabling team collaboration through distributed workflows. The installation process covers three Ubuntu-specific paths: the default APT repository for proven stability and minimal maintenance, the Ubuntu Git Maintainers PPA for current features and rapid security patches, and source compilation for bleeding-edge access or custom build configurations. Your Ubuntu system now runs a fully functional Git installation ready for repository management, branch operations, and collaborative development workflows. After configuring your identity and choosing an authentication method, explore essential workflows like renaming Git branches, undoing commits, or clearing Git cache when needed.

Leave a Comment