CARVIEW |
- Log in to:
- Community
- DigitalOcean
- Sign up for:
- Community
- DigitalOcean

Introduction
Version control systems are essential tools in modern software development. A version control system allows you to track changes to your code, revert to previous stages, and create alternate versions of files and directories. Git is one of the most popular distributed version control systems, allowing many software projects to maintain their files in Git repositories. Platforms like GitHub, GitLab, and Bitbucket use Git to help facilitate software development project sharing and collaboration.
In this tutorial, you will install and configure Git on an Ubuntu server. We will cover two different methods to install the software: using the built-in apt
package manager and installing from the source code. Each approach has distinct benefits that may be better for your specific setup. After installing, you will also complete the initial configuration to start using the software.
Simplify deploying applications with DigitalOcean App Platform. Deploy directly from GitHub in minutes.
Key Takeaways:
Here are eight key takeaways from the article:
- Git is a distributed version control system that allows you to track changes to your code, revert to previous stages, and collaborate with others.
- You can install Git on Ubuntu using the built-in
apt
package manager for a fast and straightforward setup. - For the most recent version, you can compile and install Git from the source code.
- After installation, you must configure your user name and email address for Git to properly track your commits.
- The
git config --global user.name
command sets the name that will be associated with your commits. - The
git config --global user.email
command sets the email address for your commits. - You can verify your Git installation and version number by running the
git --version
command. - Git can be installed and used on the Windows Subsystem for Linux (WSL) in the same way as a native Ubuntu installation.
Prerequisites
You will need an Ubuntu server with a non-root superuser account. To set this up, you can follow our Initial Server Setup Guide for Ubuntu.
With your server and user set up, you are ready to begin.
Installing Git with Default Packages
The option of installing with default packages is best if you want to get up and running quickly with Git if you prefer a widely-used stable version or if you don’t need the newest available functionalities. If you are looking for the most recent release, you should jump to the section on installing from source so you can choose the specific version you want to install.
Git is likely already installed on your Ubuntu server. You can confirm this is the case on your server with the following command:
- git --version
If you receive output similar to the following, then Git is already installed.
Outputgit version 2.51.0
If this is the case for you, you may need to update the Git version if yours is outdated and then you can move on to setting up your version.
If you did not get output of a Git version number, you will need to install it with the Ubuntu default package manager APT.
First, use the apt
package management tools to update your local package index.
- sudo apt update
With the update complete, you can install Git:
- sudo apt install git
You can confirm that you have installed Git correctly by running the following command and checking that you receive relevant output.
- git --version
Outputgit version 2.51.0
With Git successfully installed, you can now move on to the Setting Up Git section of this tutorial to complete your setup.
Installing Git from Source
If you’re looking for a more flexible method of installing Git, you may want to compile the software from source, which we will go over in this section. This takes longer and will not be maintained through your package manager, but it will allow you to download the latest release and will give you greater control over the options you include if you wish to make customizations.
Verify the version of Git currently installed on the server:
- git --version
If Git is installed, you’ll receive output similar to the following:
Outputgit version 2.51.0
Before you begin, you need to install the software that Git depends on. This is all available in the default repositories, so we can update our local package index and then install the relevant packages.
- sudo apt update
- sudo apt install libz-dev libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext cmake gcc
After you have installed the necessary dependencies, create a temporary directory and move into it. This is where we will download our Git tarball.
- mkdir tmp
- cd tmp
From the Git project website, we can navigate to the tarball list and download the version you would like. At the time of writing, the most recent version is 2.51.0
, so we will download that for demonstration purposes. We’ll use curl
and output the file we download to git.tar.gz
.
- curl -o git.tar.gz https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.51.0.tar.gz
Unpack the compressed tarball file:
- tar -zxf git.tar.gz
Next, move into the new Git directory:
- cd git-*
Now, you can make the package and install it by typing these two commands:
- make prefix=/usr/local all
- sudo make prefix=/usr/local install
Now, replace the shell process so that the version of Git we just installed will be used:
- exec bash
With this complete, you can be sure that your install was successful by checking the version.
- git --version
Outputgit version 2.51.0
With Git successfully installed, you can now complete your setup.
Setting Up Git
After you are satisfied with your Git version, you should configure Git so that the generated commit messages you make will contain your correct information and support you as you build your software project.
Configuration can be achieved by using the git config
command. Specifically, we need to provide our name and email address because Git embeds this information into each commit we do. We can go ahead and add this information by typing:
- git config --global user.name "Your Name"
- git config --global user.email "youremail@domain.com"
We can display all of the configuration items that have been set by typing:
- git config --list
Outputuser.name=Your Name
user.email=youremail@domain.com
...
Beyond your identity, you can set other useful global configurations. A common practice is to define the default branch name for new repositories as main
.
- git config --global init.defaultBranch main
You can also set your preferred text editor for writing commit messages. If you prefer nano, for example, you can set it like this:
- git config --global core.editor "nano"
To review all your global configuration settings, use the --list
flag.
- git config --global --list
Outputuser.name=Your Name
user.email=youremail@domain.com
init.defaultBranch=main
core.editor=nano
These settings are stored in a file named .gitconfig
in your home directory. You can edit this file directly, but using the git config
command is generally safer. With this initial setup complete, Git is now ready for use.
Uninstalling Git
If you need to uninstall Git, the process depends on how you originally installed it. Whether you used the APT package manager or compiled it from source, you can remove the application and its associated files from your system.
Uninstalling Git Installed with APT
If you installed Git from Ubuntu’s default repositories, you can use the apt
command-line utility to remove it.
Step 1 — Uninstalling the Git Package
To remove Git, use the apt remove
command. This command uninstalls the package but leaves any configuration files behind.
- sudo apt remove git
You will be prompted to confirm the removal. Press Y
and then ENTER
to proceed. This action uninstalls the main Git package.
Step 2 — Removing Unused Dependencies
When you installed Git, apt
may have also installed other packages that Git depends on. If these packages are no longer needed by any other software on your system, you can remove them to free up disk space.
- sudo apt autoremove
This command identifies and removes any packages that were installed as dependencies but are no longer required.
Uninstalling Git Installed from Source
Uninstalling a version of Git that was compiled from source is a more manual process. You must perform the steps from the same source directory where the compilation took place.
Step 1 — Navigating to the Source Directory
First, use the cd
command to move into the directory containing the Git source code.
- cd /path/to/git/source
Replace /path/to/git/source
with the correct path for your system. If you have forgotten the location, you may need to search for it.
Step 2 — Running the Uninstall Command
Once inside the source directory, you can run the uninstall
target from the Makefile
with sudo
privileges.
- sudo make uninstall
This command executes a script that removes the program files that make install
had previously placed on the system.
Verifying Uninstallation
You can verify that Git has been removed by attempting to check its version.
- git --version
If Git has been successfully uninstalled, you will see a message indicating that the command could not be found.
OutputCommand 'git' not found, but can be installed with:
sudo apt install git
This confirms that the Git application is no longer on your system. Note that this process does not remove your global Git configuration file (~/.gitconfig
) or any local repositories. If you wish to remove your configuration, you can do so manually with the rm
command:
- rm ~/.gitconfig
This will delete the user-specific Git configuration file from your home directory, allowing you to start with a completely fresh configuration when you reinstall Git.
Common Mistakes and Troubleshooting
Installing Git is usually a straightforward process, but a few common missteps can lead to configuration issues, version incompatibilities, or security oversights. In this section, we’ll cover the frequent mistakes made during and after installation on Ubuntu and provide the correct procedures to follow.
1. Using an Outdated Git Version from Default Repositories
A frequent issue is relying on the version of Git included in Ubuntu’s default repositories. These packages often lag behind the latest stable release, which can cause incompatibility with features available on services like GitHub or GitLab, such as the git switch
and git restore
commands.
- Problem: Running
sudo apt install git
may install an older version of Git. - Solution: To get the most recent version, add the official Git PPA (Personal Package Archive) before installing. This ensures you have access to the latest features and security patches.
sudo add-apt-repository ppa:git-core/ppa
sudo apt update
sudo apt install git -y
2. Skipping User Identity Configuration
Many users forget to configure their user identity after installing Git. When you make a commit without setting your name and email, Git may use a generic placeholder, making it difficult to attribute work correctly in a collaborative project.
- Problem: Commits are made with an “unknown” user or an incorrect system-generated email.
- Solution: Always set your global user name and email immediately after installation. Use the same email address that is associated with your GitHub or GitLab account to ensure your contributions are linked to your profile.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
3. Not Changing the Default Branch Name
Historically, the default branch in Git was named master
. However, the industry standard has shifted to main
. If you initialize a new local repository, it may still default to master
, creating a mismatch when you try to push it to a remote service like GitHub that expects main
.
- Problem: Your local default branch (
master
) does not match the remote default branch (main
). - Solution: Configure Git to use
main
as the default name for the initial branch in all new repositories.
git config --global init.defaultBranch main
4. Storing Credentials Insecurely or Repetitively Entering Them
When you first connect to a remote repository using HTTPS, Git will ask for your credentials. Some users configure the credential.helper store
option, which saves your password or Personal Access Token (PAT) in plain text on your machine. Others skip credential helpers altogether and type their password for every interaction with the remote.
- Problem: Storing credentials in plain text is a security risk, while repeatedly typing them is inefficient.
- Solution: Use SSH keys for authentication. SSH provides a more secure and convenient way to interact with remote repositories without needing to enter your credentials each time. First, generate a new SSH key pair:
ssh-keygen -t ed25519 -C "your_email@example.com"
After generating the key, add the public key (~/.ssh/id_ed25519.pub
) to your GitHub or GitLab account.
5. Not Verifying the Installation
After running the installation command, it is good practice to confirm that Git was installed correctly and is accessible through your system’s PATH
.
- Problem: You run a
git
command and receive acommand not found
error. - Solution: Verify the installation path and the installed version with the following commands.
# Check the executable's location
which git
# Check the installed version
git --version
If these commands fail, your installation may have encountered an error, or the PATH
environment variable may be misconfigured.
6. Mixing Installation Methods
Installing Git from multiple sources, such as through apt
and also by compiling from source, can create conflicts. This can lead to confusion over which Git executable is being used and can make uninstallation difficult.
- Problem: Multiple versions of Git exist on the system, leading to unpredictable behavior.
- Solution: Choose a single installation method and stick with it. For most users, the PPA method is recommended. If you need a custom build, compile from source, but first ensure that any other versions of Git have been fully removed.
7. Not Backing Up Configuration Files
Your global .gitconfig
file, located in your home directory, stores all the custom settings you’ve configured. When you work on multiple machines, failing to synchronize this file leads to an inconsistent development environment.
- Problem: Your Git settings, aliases, and user identity differ from one machine to another.
- Solution: Store your configuration files, often called “dotfiles,” in a Git repository and clone it across your machines. This practice ensures your setup remains consistent everywhere you work.
FAQs
1. How do I install Git on Ubuntu 22.04?
The most straightforward way to install Git on Ubuntu 22.04 is by using apt
.
First, open your terminal and update your package list to ensure you are getting the latest version available in the default repositories:
sudo apt update
Next, install Git with the following command:
sudo apt install git
The -y
flag can be added to automatically confirm the installation.
2. How do I check if Git is installed on Ubuntu?
You can verify that Git has been installed correctly by checking its version. Run the following command in your terminal:
git --version
If Git is installed, the output will display the installed version, for example: git version 2.51.0
.
3. How do I configure Git after installation?
After installing Git, it’s important to configure your user name and email address. These details are used to identify the author of commits.
To set your user name, use the following command, replacing "Your Name"
with your actual name:
git config --global user.name "Your Name"
To set your email address, use this command, replacing "youremail@domain.com"
with your email:
git config --global user.email "youremail@domain.com"
The --global
flag ensures that these settings are applied to all your Git projects on the current user account. You can verify your configuration with this command:
git config --global --list
4. What’s the difference between installing Git via apt and source?
There are two primary methods for installing Git on Ubuntu: using the apt
package manager or compiling from the source code.
Feature | Installation via apt |
Installation from Source |
---|---|---|
Ease of Installation | Simple and quick, requiring a single command. | More complex, involving downloading the source code and compiling it manually. |
Version | Installs the latest version available in the Ubuntu repositories, which may not be the most recent Git release. | Allows you to install the very latest version of Git. |
Updates | Updates are managed by the apt package manager. |
You are responsible for manually updating to new versions. |
Dependencies | Automatically handles all necessary dependencies. | Requires you to manually install several dependencies before compilation. |
For most users, installing Git via apt
is the recommended method due to its simplicity and ease of maintenance.
5. How do I update Git to the latest version on Ubuntu?
If you installed Git using apt
, you can update it with the following commands:
sudo apt update
sudo apt upgrade git
To get the absolute latest version of Git as soon as it is released, you can add the official Git PPA:
sudo add-apt-repository ppa:git-core/ppa
sudo apt update
sudo apt install git
6. How do I uninstall Git from Ubuntu?
To remove the Git package from your system, use the apt remove
command:
sudo apt remove git
If you also want to remove any configuration files associated with Git, use the apt purge
command:
sudo apt purge git
To remove any dependencies that were installed with Git and are no longer needed, you can run:
sudo apt autoremove
7. Can I install Git on WSL Ubuntu?
Yes, you can install Git on Ubuntu running under the Windows Subsystem for Linux (WSL) in the same way you would on a native Ubuntu installation. The commands for installing, configuring, and managing Git are identical.
Open your Ubuntu terminal within WSL and follow the standard installation and configuration steps mentioned above. It’s important to note that you will need to install and configure Git separately on your Windows file system if you plan to use it there as well.
Conclusion
You now have Git installed and configured on your Ubuntu server. This guide covered two installation paths: using the default apt
packages for a stable and straightforward setup, and compiling from source to get the latest version. You also completed the initial configuration of your user identity, which prepares you to start making commits.
With Git ready for use, you can begin tracking your software projects. Your next step could be to create a new repository for an existing project or clone a remote repository to start contributing. To continue learning and make the most of version control, you can explore the following guides:
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Tutorial Series: Introduction to Git: Installation, Usage, and Branches
This series covers the installation and usage of git on an Ubuntu server. After completing the series, the reader should feel comfortable installing and using git, as well as how to create two branches (master and develop) and how to merge code from the development stage to production.
Browse Series: 3 tutorials
About the author(s)
Community and Developer Education expert. Former Senior Manager, Community at DigitalOcean. Focused on topics including Ubuntu 22.04, Ubuntu 20.04, Python, Django, and more.
I help Businesses scale with AI x SEO x (authentic) Content that revives traffic and keeps leads flowing | 3,000,000+ Average monthly readers on Medium | Sr Technical Writer @ DigitalOcean | Ex-Cloud Consultant @ AMEX | Ex-Site Reliability Engineer(DevOps)@Nutanix
With over 6 years of experience in tech publishing, Mani has edited and published more than 75 books covering a wide range of data science topics. Known for his strong attention to detail and technical knowledge, Mani specializes in creating clear, concise, and easy-to-understand content tailored for developers.
Still looking for an answer?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Thanks for the awesome tutorial, Lisa. I would like to add the login when you’re using 2FA. tldr - generate new token and use it instead the password (https://stackoverflow.com/questions/29297154/github-invalid-username-or-password)
This is SUPER not obvious so will be awesome to be seen often to shorten the search adventure for everyone.
Can you tell me please why there is a sentence - You will need an Ubuntu 20.04 server with a non-root superuser account.
Why I need a non-root user to set up Git?
Thank you for this tutorial Lisa.
I just want to add this informations:
For Ubuntu, this PPA provides the latest stable upstream Git version
# sudo add-apt-repository ppa:git-core/ppa
# sudo apt update
# sudo apt install git
I hope this will help others and maybe updated in this tutorial :p
This information was found : here
and you can find more informations about git : here
Happy coding !
- Table of contents
- Introduction
- Prerequisites
- Installing Git with Default Packages
- Installing Git from Source
- Setting Up Git
- Uninstalling Git
- Common Mistakes and Troubleshooting
- FAQs
- Conclusion
Deploy on DigitalOcean
Click below to sign up for DigitalOcean's virtual machines, Databases, and AIML products.
Become a contributor for community
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
DigitalOcean Documentation
Full documentation for every DigitalOcean product.
Resources for startups and SMBs
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Get our newsletter
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
The developer cloud
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Get started for free
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.