How to Install Git on Ubuntu 24.04, 22.04, or 20.04

Git, a cornerstone in modern software development and system administration, offers a flexible and robust environment for managing code versions and collaborative projects. Whether you’re a developer or a sysadmin, understanding Git on Ubuntu servers is essential for efficient workflows.

Below are key highlights of Git’s features that underscore its importance and utility:

  • Version Control: Git provides powerful tools for tracking and managing changes to your code, enabling multiple iterations and revisions without losing the original content.
  • Collaboration and Merging: This feature facilitates team collaboration by allowing multiple people to work on the same project simultaneously. It also includes features for merging changes and resolving conflicts.
  • Branching and Tagging: This tool offers advanced branching capabilities, enabling developers to create separate branches for new features or testing and tagging specific points in a repository’s history for reference.
  • Local and Remote Repositories: This feature supports local and remote repositories, allowing users to work offline and sync up with remote repositories as needed.
  • Security and Integrity: Ensures the integrity of your code through cryptographic hash functions, making it nearly impossible for your project’s history to be changed without detection.

Next, we’ll guide you through the installation process so that you can get the most out of Git in your development environment.

Method 1: Install GIT via Default APT Repository

Ensuring Up-to-Date System Packages Before GIT Installation

To establish a solid foundation for the Git installation, your Ubuntu system’s packages must be current. This action mitigates potential package conflicts during the installation process.

Updating your system’s packages is accomplished by invoking the Advanced Packaging Tool (APT) with the update command:

sudo apt update

After running the update, upgrading any outdated packages is a good practice. This ensures that all your system’s software is at the most recent version. Perform this upgrade with the following command:

sudo apt upgrade

Verifying Git’s Presence on Ubuntu

Before proceeding with the Git installation, it’s prudent to check whether Git is already installed on your system. By doing so, we avoid redundant installations and keep our system clean.

To verify if Git is installed, use the --version flag with the git command. This should return the installed version of Git, if present:

git --version

Install GIT on Ubuntu via APT Command

Should the above command return nothing, it confirms that Git is absent from your system. Now, it’s time to install Git.

We will use Ubuntu’s repository to install Git as it provides a straightforward method. Install Git by invoking the install command with sudo apt:

sudo apt install git

Confirming GIT Successful Installation via Ubuntu APT

With the installation process complete, it’s advisable to verify that Git was successfully installed. This step reassures us that the installation process went smoothly and that Git is ready for use.

Again, we can use the --version flag to confirm the installation. This command should now return the version of Git that you’ve just installed:

git --version

Upon running the command, you should see an output similar to:

git version x.x.x

Method 2: Install GIT via Ubuntu Git Maintainers PPA

Sometimes, it is desirable to work with the most up-to-date version of Git, especially when newer features or essential bug fixes are needed. The Ubuntu Git Maintainers team provides a Personal Package Archive (PPA) that regularly holds the latest stable version of Git. Using this method can offer significant advantages, depending on your specific needs and the environment in which you’re working.

Import Ubuntu Git Maintainers PPA

To start, we need to add the Git PPA provided by the Ubuntu Git Maintainers team to our system’s list of repositories. This PPA ensures access to the latest stable Git release. While the following packages are most likely already installed on your system, it doesn’t hurt to check:

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

Refreshing the Packages Index After PPA Import

Once the Git PPA is imported into your system’s repository list, updating the packages index is essential. This step lets your system recognize the newly available packages from the added repository.

To update the packages index, run the following:

sudo apt update

Install GIT on Ubuntu via APT PPA Command

With the PPA in place, you can install or upgrade Git. The following command will execute this task:

sudo apt install git -y

Note: If you have GIT previously installed from Ubuntu’s repository, running this command will upgrade Git to the latest version from the added PPA.

Once the installation or upgrade is completed, verify the installed Git version with the following:

git --version

You should see an output similar to:

git version x.x.x

This output denotes that the latest Git version has been successfully installed or upgraded on your Ubuntu system.

For additional insight, you can check which repository your Git installation originates from. Given that the PPA tends to contain a much newer version of Git, executing the following command should reflect the recent PPA addition:

apt-cache policy git

Method 3: Install GIT via Source Archive

This section provides a detailed guide on installing GIT by learning to download and build, compile then install the GIT source code. This approach offers users greater control over the installation procedure and allows access to particular features that may not be present in the pre-packaged distributions.

Setting Up GIT Build Dependencies

The first phase involves preparing your Ubuntu system with the required build dependencies. These dependencies are vital for a successful Git compilation. To set these up, use the following command:

sudo apt install make libssl-dev libghc-zlib-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip -y

Download the GIT Source Code

To get the Git source code, navigate to the Git release page. From there, you can select either the latest stable release or the master archive. The following command aids in downloading the desired version. Remember to substitute {version} with the desired Git version number:

wget https://github.com/git/git/archive/refs/tags/{version}.tar.gz

Extract and Install the GIT Source Code

The next phase entails extracting the downloaded archive. When doing this, ensure to replace {version} with the relevant Git version you previously selected:

tar -xvf git-{version you downloaded}

Note: Quick tip for new users, in your CLI terminal, type “git-” and then press the Tab key for auto-completion. If you have multiple versions installed, you might need to provide more specifics, such as “git-2.4”, before pressing Tab.

Now, it’s time to compile and set up GIT. Initiate the compilation with the following command:

sudo make prefix=/usr/local all

This command instructs the build system to anticipate an installation in the /usr/local directory upon the conclusion of the compilation process. The all flag ensures a comprehensive build covering all components.

Once the compilation concludes, move on to the installation phase with this command:

sudo make prefix=/usr/local install

Here, Git gets installed into the /usr/local directory. The process involves copying essential files and establishing the appropriate permissions, thus making Git accessible on your system.

To confirm that the installation was successful and that the build is correct, run:

git --version

This command should return the Git version you’ve installed, verifying its proper integration into your system.

Additional Commands to Manage GIT

Update GIT

APT GIT Update Method

Updating Git on Ubuntu Linux is straightforward. Regardless of your previous installation method, you can update Git with a single command because you installed it using the Advanced Package Tool (APT) package manager.

In your terminal, run the following command:

sudo apt update && sudo apt upgrade

This command first updates your package lists (with sudo apt update) and then upgrades all upgradable packages on your system (with sudo apt upgrade). This way, not only Git but all other packages not marked on hold in your system will be updated.

Source GIT Update Method

Repeat the process to upgrade your installation for installations that were done by downloading and installing the source.

Remove GIT

APT GIT Remove Method

To uninstall Git, execute the following command:

sudo apt remove git

This command will remove Git from your system. Remember to confirm the operation when prompted.

If you initially installed Git using the Personal Package Archive (PPA) from the Ubuntu Git Maintainers team, you should also remove this PPA. Here’s the command to do that:

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

Running this command will eliminate the PPA, ensuring your system no longer receives updates.

GIT Remove Method For Source Installations

If you’ve installed Git on Ubuntu via source and need to uninstall it, the process can involve more than a package manager since there isn’t a direct uninstall command. However, with careful steps, you can manually remove the installation.

Identify the Installed Files

Before removing Git, you need to know where it’s installed. If you followed our previous guide, you would have installed Git in the /usr/local directory.

Manually Remove the Files

Navigate to the installation directory:

cd /usr/local

Now, you’ll need to remove the Git files and directories manually:

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 the Removal

To ensure that Git has been removed, you can check its version:

git --version

If GIT was successfully removed, the terminal should return an error message stating that the git command is not found.

Note: Manual removal, like this method, requires extra care to avoid accidentally deleting unrelated files or system-critical components. Always double-check commands and paths before execution.

Conclusion

That wraps up our guide on installing Git on your Ubuntu server. We’ve walked through three handy methods: the classic APT repository approach, the up-to-date LaunchPAD PPA route, and the hands-on compilation from the source. Remember, choosing the correct method depends on your specific needs, whether sticking to the tried-and-true APT for stability or venturing into the latest features with PPA or source compilation. My parting advice? Keep your Git version updated, and don’t shy away from experimenting with its robust features for your development projects.

Useful Links

Here are some valuable links related to using Git:

  • Git Official Website: Visit the official Git website for information about the version control system, features, and download options.
  • Git Releases: Check out the latest Git releases on GitHub to download the most recent versions and view release notes.
  • Git Documentation: Access comprehensive documentation for detailed guides on using and configuring Git.
  • Git Community: Explore the community page for various Git communities, including Discord, IRC channels, newsletters, and mailing lists.
Joshua James
Follow me
Latest posts by Joshua James (see all)