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:
apt
package manager for a fast and straightforward setup.git config --global user.name
command sets the name that will be associated with your commits.git config --global user.email
command sets the email address for your commits.git --version
command.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.
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.
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.
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.
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.
If you installed Git from Ubuntu’s default repositories, you can use the apt
command-line utility to remove it.
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.
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 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.
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.
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.
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.
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.
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.
sudo apt install git
may install an older version of Git.sudo add-apt-repository ppa:git-core/ppa
sudo apt update
sudo apt install git -y
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.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
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
.
master
) does not match the remote default branch (main
).main
as the default name for the initial branch in all new repositories.git config --global init.defaultBranch main
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.
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.
After running the installation command, it is good practice to confirm that Git was installed correctly and is accessible through your system’s PATH
.
git
command and receive a command not found
error.# 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.
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.
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.
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.
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
.
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
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.
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
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
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.
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.
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
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.
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 !
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
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
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.