How to Install Git on Ubuntu 24.04, 22.04 or 20.04

This guide will demonstrate how to install Git on Ubuntu 24.04, 22.04, or 20.04 LTS releases through three distinct command-line methods: using Ubuntu’s APT default repository, employing the Ubuntu Maintainers LaunchPAD PPA for an up-to-date version, and manually downloading, compiling, and installing Git from the source for either the latest or a specific version tailored to your needs.

Git, a cornerstone in the world of 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 seamless, 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 loss of original content.
  • Collaboration and Merging: Facilitates team collaboration by allowing multiple people to work on the same project simultaneously, with features for merging changes and resolving conflicts.
  • Branching and Tagging: 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: Supports both local and remote repositories, giving users the flexibility 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.

As you delve into the technical aspects of Git installation on Ubuntu, it’s important to appreciate these features as the backbone of what makes Git a vital tool for developers and sysadmins alike. The following sections will guide you through the installation process, ensuring that you can leverage the full potential of Git in your development environment.

Install GIT on Ubuntu 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

Install GIT on Ubuntu 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 on Ubuntu

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
Screenshot of apt-cache policy confirming Git version from Ubuntu maintainers' PPA on Ubuntu 24.04, 22.04, 20.04
Screenshot showing apt-cache policy command to confirm Git version on Ubuntu

Install GIT on Ubuntu via Source

This section provides a detailed guide on how to install GIT on Ubuntu 24.04, 22.04 or 20.04 by learning to download and 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 on Ubuntu

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 on Ubuntu

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.

Configure GIT Examples on Ubuntu

This section will explore a range of typical Git setups and commands that users frequently employ in their day-to-day workflow. Whether new to Git or seeking to expand your understanding of its capabilities, this comprehensive walkthrough aims to enhance your overall knowledge and proficiency.

Configure GIT Username And E-mail with Ubuntu

To kick things off, let’s begin with the user setup. When you install Git, it’s essential to establish basic user configurations such as your name and e-mail address. These details are primarily used in Git commit messages to identify the author of the changes.

Here’s how you can set up your global Git identity:

git config --global user.name "YOUR NAME"
git config --global user.email "YOUR EMAIL"

Create and Initialize a GIT Directory with Ubuntu

Creating a dedicated directory for your Git repositories helps maintain organization. Here are the steps to create a new directory and navigate into it:

mkdir example-directory
cd example-directory

Once inside the directory, you must initialize it as a Git repository. The git init command accomplishes this by creating a hidden .git directory, which houses Git’s essential files and directories, such as configuration, history, and more.

git init

You can then verify the initialization and inspect the content of the .git directory using:

ls -a .git

Review GIT Configuration with Ubuntu

To confirm your Git configuration, you can employ the git config --list command:

git config --list

Unless specified otherwise, Git stores configuration details in the ~/.gitconfig file. You can inspect this file using:

cat ~/.gitconfig

Remember that using sudo with the git config command will affect global settings, potentially overriding individual user configurations.

Store GIT Credentials with Ubuntu

In case you’re working with remote repositories and want Git to remember your credentials for a certain period, you can enable Git’s credential helper:

git config --global credential.helper cache

However, for enhanced security, storing the credentials only for a limited period is recommended. For example, if you’ll be working with Git for 1 to 4 hours, you could set the cache expiry to 5 hours:

git config --global credential.helper "cache --timeout=18000"

After 5 hours, Git will automatically delete the credentials.

Check GIT Repository Status with Ubuntu

The git status command provides a snapshot of your Git repository’s current state, highlighting changes made, files staged for commit, and more.

git config --global credential.helper cache

Connect to a Remote GIT Repository with Ubuntu

If you need to work with remote repositories, you’ll need to establish a link using the git remote command. The following example assumes you’re linking to the master branch of your remote repository:

git remote add origin remote-repository-link

After 5 hours, the credentials will be deleted, which secures your GIT.

Commit and Sync GIT Changes with Ubuntu

After making changes in your repository, you can commit them using the git commit command. A commit message should accompany each commit, explaining the changes made:

git commit -m "changelog message"

To push your changes to the remote repository, synchronizing both versions, use the git push command:

git push origin master

Alternatively, if you want to fetch the latest changes from the remote repository and synchronize your local version, use the git pull command:

git pull origin master

Create a Branch in Git with Ubuntu

Branching is an essential feature in Git that lets you isolate your work without affecting others. Here’s how to create a new branch:

git branch branch-name

To switch to your newly created branch, use the git checkout command:

git checkout branch-name

Merge GIT Branches with Ubuntu

Once you’re satisfied with the changes in your branch, you might want to integrate them into the master branch. You can accomplish this by first checking out to the master branch and then merging your branch into it:

git checkout master
git merge branch-name

Delete a GIT Branch with Ubuntu

After merging, if you no longer need the branch, you can delete it:

git branch -d branch-name

Clone a Git Repository with Ubuntu

Cloning a repository creates a copy of an existing Git repository. This is especially handy when you want to get a local copy of a project hosted on a remote server:

git clone remote-repository-link

Stash GIT Changes with Ubuntu

The git stash command allows you to temporarily save changes you don’t want to commit immediately. You can apply these changes later:

git stash

And when you’re ready to apply these changes again, use:

git stash apply

Additional Commands to Manage GIT on Ubuntu

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 from Ubuntu

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 on Ubuntu

If you’ve installed Git on Ubuntu 22.04 or 20.04 via source and need to uninstall it, the process can be more involved 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

Alright, 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 right method depends on your specific needs, whether it’s 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.

Leave a Comment


Your Mastodon Instance
Share to...