How to Install Git on Ubuntu (26.04, 24.04, 22.04)

Git tracks code changes, manages project history, and enables collaboration through distributed version control. Whether you are working on personal scripts, contributing to open-source projects, or managing enterprise codebases, Git provides the foundation for tracking every modification and coordinating work across teams.

This guide covers three installation methods for Ubuntu 26.04, 24.04, and 22.04 LTS: the default APT repository for stability, the Ubuntu Git Maintainers PPA for current releases, and source compilation for full control. You will also learn essential configuration, common troubleshooting solutions, and how to maintain your installation over time.

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.

This guide covers Ubuntu 26.04 LTS, 24.04 LTS, and 22.04 LTS. The Git PPA provides identical packages across all three releases, and the steps work the same regardless of which Ubuntu version you run.

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

Before installing, check if Git is already present on your system:

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 displays your installed Git version. The exact version depends on your Ubuntu release since each ships with a different default:

Ubuntu ReleaseDefault Git VersionStatus
26.04 LTS (Resolute)2.51.xCurrent LTS
24.04 LTS (Noble)2.43.xSupported LTS
22.04 LTS (Jammy)2.34.xSupported LTS

For many workflows, the default APT version works well. However, if you need recent Git features like improved sparse-checkout performance, enhanced git switch and git restore commands, or the latest security patches, consider the PPA method in the next section.

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 installations may lack the add-apt-repository helper. Install the required package if needed:

sudo apt install software-properties-common -y

Add the Git PPA to your system’s repository list:

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

Update Package Index After PPA Import

Update the package index so APT recognizes packages from the newly added PPA:

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 confirms the latest stable version from the PPA:

git version 2.52.0

Check which repository provides your Git installation:

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

Install the required build tools and development libraries for Git compilation. These packages provide the compiler, SSL support, compression handling, and HTTP functionality Git needs:

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

Download Git Source Code

Download the Git source code from the Git tags page. The example below uses v2.52.0 (the latest stable release at the time of writing). The wget command -O flag saves the archive with a predictable filename that matches subsequent extraction commands:

The version numbers in this guide (v2.52.0) are examples current at the time of writing. Always check the official tags page for the latest stable release and substitute accordingly in all commands below. Source installations are not tied to APT updates, so you will need to repeat this process whenever a new Git release ships. An update script is provided at the end of this section to simplify future upgrades.

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

Extract and Compile Git Source Code

Extract the downloaded archive using the tar command:

tar -xvf git-v2.52.0.tar.gz

Change into the extracted directory. GitHub archives strip the leading v from the folder name, so v2.52.0 becomes git-2.52.0:

cd git-2.52.0

Compile Git with the installation prefix set to /usr/local. The -j$(nproc) flag uses all available CPU cores to speed up compilation:

make -j$(nproc) prefix=/usr/local all

Compilation typically takes 2-5 minutes depending on your system. When it completes, install the compiled binaries:

sudo make prefix=/usr/local install

Reload your shell environment so the system recognizes the newly installed Git binary in /usr/local/bin:

exec bash

Verify the source installation succeeded and the correct binary is being used:

git --version && which git

The output confirms Git is installed from your source build:

git version 2.52.0
/usr/local/bin/git

The /usr/local/bin path confirms Git runs from your source build rather than the system package. This location takes precedence over /usr/bin in the default PATH, so your compiled version runs automatically.

Update Script for Source Builds

Users who compile Git from source need a clear upgrade path. Months later, you may forget the exact build steps or miss security updates. This script automates the update process by checking the latest version, downloading if newer, and recompiling:

Create a dedicated build directory and the update script:

mkdir -p ~/git-source-build
cat > ~/git-source-build/update-git.sh << 'SCRIPT'
#!/bin/bash
set -e

# Configuration
INSTALL_PREFIX="/usr/local"
BUILD_DIR="$HOME/git-source-build"

# Refuse to run as root
if [ "$(id -u)" -eq 0 ]; then
  echo "Run this script as a regular user, not root."
  exit 1
fi

# Check required build tools
for cmd in curl tar make gcc; do
  if ! command -v "$cmd" &> /dev/null; then
    echo "Error: $cmd is required but not installed."
    echo "Run: sudo apt install build-essential curl"
    exit 1
  fi
done

# Get current installed version
CURRENT_VERSION=$($INSTALL_PREFIX/bin/git --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -n 1 || echo "none")

# Fetch latest stable version from GitHub
echo "Checking for latest Git version..."
LATEST_TAG=$(curl -s https://api.github.com/repos/git/git/tags | grep -oE '"name": "v[0-9]+\.[0-9]+\.[0-9]+"' | head -n 1 | cut -d '"' -f4)

if [ -z "$LATEST_TAG" ]; then
  echo "Error: Could not fetch latest version from GitHub."
  exit 1
fi

LATEST_VERSION=${LATEST_TAG#v}

echo "Current version: $CURRENT_VERSION"
echo "Latest available: $LATEST_VERSION"

if [ "$CURRENT_VERSION" = "$LATEST_VERSION" ]; then
  echo "Already up to date."
  exit 0
fi

echo "Updating from $CURRENT_VERSION to $LATEST_VERSION..."

cd "$BUILD_DIR"

# Clean previous builds
rm -rf git-*/

# Download and extract
echo "Downloading Git $LATEST_TAG..."
curl -fLO --progress-bar "https://github.com/git/git/archive/refs/tags/$LATEST_TAG.tar.gz"
tar -xzf "$LATEST_TAG.tar.gz"
rm "$LATEST_TAG.tar.gz"

cd git-*/

# Compile and install
echo "Compiling (this may take a few minutes)..."
make -j$(nproc) prefix="$INSTALL_PREFIX" all
echo "Installing..."
sudo make prefix="$INSTALL_PREFIX" install

# Verify
NEW_VERSION=$($INSTALL_PREFIX/bin/git --version | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -n 1)
echo "Successfully updated to Git $NEW_VERSION"
SCRIPT
chmod +x ~/git-source-build/update-git.sh

Run the update script whenever you want to check for and install new Git versions:

~/git-source-build/update-git.sh

Example output when already up to date:

Checking for latest Git version...
Current version: 2.52.0
Latest available: 2.52.0
Already up to date.

When an update is available, the script automatically downloads the new version, recompiles, and installs it. The entire process takes 2-5 minutes on most systems.

Avoid automating this script with cron. Compilation can fail due to missing dependencies, API rate limits, or network issues. Run the script manually so you can monitor output and address any problems immediately.

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"

Configure additional settings that improve your day-to-day 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.

Verify your configuration with:

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. For detailed SSH setup including key management and troubleshooting, see our Ubuntu SSH guide.

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

If you installed Git via PPA, remove the PPA as well:

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

Remove Source Installation

Source installations must be removed manually since no package manager tracks them. Navigate to the installation prefix used during compilation:

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*

Remove the Git files and directories that were installed to /usr/local:

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*

Verify Git was removed:

git --version

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

Conclusion

Git provides powerful version control for tracking changes, managing project history, and collaborating across distributed teams. Your Ubuntu system now has a fully functional Git installation ready for repository management, branching workflows, and integration with platforms like GitHub, GitLab, or Bitbucket. With your identity configured and an authentication method in place, explore related workflows like renaming Git branches, undoing commits, clearing Git cache, or setting up the GitHub CLI for streamlined repository management.

Leave a Comment

Let us know you are human: