How to Install Git in Ubuntu
Login
Version control systems are one of the indispensable parts of software development. It allows you to save the changes you make to the code, revert to the previous version if necessary, and create different versions. Git is one of the most popular tools for this job. Many projects are stored in Git repositories, and thanks to platforms such as GitHub, GitLab and Bitbucket, teams can easily share their codes and work together.
In this guide, we will install and configure Git on an Ubuntu server.
We will talk about two methods: installation with Ubuntu's own package manager apt and installation by compiling from the source code.
Each method has different advantages; You can choose according to your needs. After the installation, we will make the initial configuration and start using Git.
Summary Notes
- Git is a distributed version control system that allows you to track code changes, roll back to previous versions, and collaborate with your teammates.
- apt package manager can be used to quickly install Git in Ubuntu.
- For the latest version, you can compile and install Git from the source code.
- After installation, you need to set your username and email address.
git config --global user.name "Adınız"→ determines the name that will appear in commits.git config --global user.email "mail@ornek.com"→ determines the email that will appear in commits.- You can run
git --versionto verify the installation. - Git can be installed and used on Windows Subsystem for Linux (WSL) just like on Ubuntu.
Prerequisites
You will need a non-root, sudo-enabled user account on an Ubuntu server.
To set this up, you can follow our article First Server Setup with Ubuntu.
If you have prepared your server and user account, you can now start.
Git Installation with Default Packages
This method is best if you want to install Git quickly and prefer a stable, widely used version.
If you want the latest version, you can go directly to install from source and select the version you want.
Git may be installed on your Ubuntu server.
To check this, you can run this command:
git --version
If you see output similar to the following, Git is already installed:
git version 2.34.1
If your Git version is old, you may need to update it. You can then continue adjusting your version.
If you don't see any screenshots, you need to install Git using Ubuntu's default package manager APT.
First, run the following command to update your package manager:
sudo apt update
Once the update is complete, you can install Git:
sudo apt install git
To make sure the installation is done correctly, run the following command and check the output:
git --version
If Git has been installed successfully, you can proceed to the Configuring Git section to complete the installation.
Installing Git from Source Code
If you want to install Git in a more flexible way, you can compile the software from source code.
This method may take a little longer and will not update automatically with the package manager.
But it allows you to download the latest version and you can make customizations if you want. It provides the opportunity to control the installation.
To check the version of Git installed on your server, run the following command:
git --version
Before we start, we need to install some packages that Git needs.
These packages are available in Ubuntu's default repositories. You should first update the package list and then install the required dependencies:
sudo apt update
sudo apt install libz-dev libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext cmake gcc
After installing the necessary packages, create a temporary folder and enter the folder.
We will download Git's compressed file (tarball) here.
mkdir tmp
cd tmp
You can go to tarball list page on Git project website and download the version you want.
At the time of writing this article, the most current version was 2.51.0, we will download this version as an example.
To do this, we use curl and save the file with the name git.tar.gz:
curl -o git.tar.gz https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.51.0.tar.gz
Unzip the Git tarball** file you downloaded:
tar -zxf git.tar.gz
Now switch to the newly created Git folder:
cd git-*
You can now compile and install the package. To do this, run these two commands in order:
make prefix=/usr/local all
sudo make prefix=/usr/local install
Now refresh the terminal session so that the version of Git we just installed will be used:
exec bash
Now you can check the version to see if the installation was successful:
git --version
Git is now successfully installed, you can now complete the configuration.
Configuring Git
Now that you have your Git version ready, the next step is to configure it.
This way, the correct information is included in the commit messages you create and you can manage your projects more regularly.
We use the git config command to make these settings.
Git adds your name and email address to every commit, so you'll need to define these.
You can add your information with the following commands:
git config --global user.name "Adın"
git config --global user.email "mail@ornek.com"
To see all the settings you have configured, you can type this command:
git config --list
Output
user.name=Adın
user.email=mail@ornek.com
...
Apart from your credentials, you can also make other global settings.
For example, it is common practice to define the default branch name as main in newly created repositories:
git config --global init.defaultBranch main
You can also choose which text editor you want to use when writing your commit messages.
For example, if you prefer to use nano, you can do it like this:
Note:
Nano is a simple text editor widely used in Linux.
git config --global core.editor "nano"
You can use the --list parameter to see all your global settings:
git config --global --list
Output
user.name=Adın
user.email=mail@ornek.com
init.defaultBranch=main
core.editor=nano
These settings are stored in the .gitconfig file located in the user account's home folder.
You can edit this file directly if you want, but it's generally safer to edit it using the git config command.
Once you have completed these initial settings, Git is now ready to use.
Uninstall Git
The method to uninstall Git depends on how you installed it.
Whether you installed it with the APT package manager or compiled it from source code, you can delete the application and related files from your system.
Uninstalling Git Installed with APT
If you installed Git from Ubuntu's default repositories, you can use the apt command to remove it:
Step 1 — Uninstall Git Package
You can use the following command to uninstall Git.
This just deletes the package, the settings files remain on the system:
sudo apt remove git -y
Note:
It will ask you for confirmation during uninstallation. Continue by typing Y and pressing ENTER.
With this process, the Git package is removed from the system.
Step 2 — Remove Unused Add-on Packages
You may have also installed some additional packages with apt while installing Git.
If these packages are not used by other software, they will take up unnecessary space.
You can remove these packages to free up disk space.
sudo apt autoremove
This command finds packages that were installed with Git but are no longer needed and deletes them from the system.
Uninstalling Git Installed from Source Code
Removing Git compiled from source code is a slightly more manual process.
You need to perform these steps in the same source folder where you compiled Git.
Step 1 — Going to the Source Folder
First, use the cd command to go to the folder where Git's source codes are located:
cd /path/to/git/source
You need to replace /path/to/git/source with the correct folder on your system.
If you forget where the folder is, you may need to search your system.
Step 2 — Running the Uninstall Command
Once in the source folder, you can run the uninstall command in the Makefile with authority sudo:
sudo make uninstall
This command removes Git files previously added to the system with make install.
Verifying Removal
To check whether Git has been deleted, you can try querying its version:
git --version
If Git has been successfully removed, you will see an error message saying not found when you run the command:
Output
Command 'git' not found, but can be installed with:
sudo apt install git
This confirms that Git is no longer in your system.
Remember, this does not delete the global Git configuration file (~/.gitconfig) or local repositories.
If you also want to remove the settings file, you can do this manually with this command:
rm ~/.gitconfig
This command deletes the user-specific Git settings file.
This way, when you reinstall Git, you can start with a completely clean configuration.
Common Errors and Troubleshooting
Git installation usually goes smoothly, but sometimes small errors can break things.
Problems such as incorrect settings, version incompatibilities or security vulnerabilities may arise.
In this section, you will find the most common mistakes and correct solutions made during and after installation on Ubuntu.
1. Using the Old Version of Git in Ubuntu's Default Repositories
A common mistake is to trust the version of Git in Ubuntu's default repositories.
These versions are often outdated and may be incompatible with some new features on GitHub or GitLab.
-
Problem: The
sudo apt install gitcommand often installs an old Git version. -
Solution: Add the official Git PPA (Personal Package Archive) before starting the installation to get the latest version.
This way you get the latest features and security patches.
sudo add-apt-repository ppa:git-core/ppa
sudo apt update
sudo apt install git -y
2. Bypassing User ID Settings
Many people forget to set the name and email after installing Git.
In this case, Git uses a default value when you commit and team work may be confused about who did what.
-
Problem: Commits can be registered with an "unknown" user or with a wrong e-mail that the system automatically generates.
-
Solution: Set your global username and email as soon as you install Git.
Also, make sure that the email you use is the same as your GitHub or GitLab account so that your contributions match your profile.
git config --global user.name "Adın"
git config --global user.email "mail@ornek.com"
3. Not Changing the Default Branch Name
In the past, the default branch name in Git was master, but now main is used as standard.
When you create a new repo, the branch name may be master, and this may create incompatibility when pushing to the remote repository, as services such as GitHub expect main.
-
Problem: The default branch name in the local repo is master, but in the remote repo it is main. This causes conflict.
-
Solution: Set Git to set the default branch name to main in all newly created repos:
git config --global init.defaultBranch main
4. Storing Credentials Insecurely or Constantly Re-Entering them
The first time you connect to a remote repo over HTTPS, Git prompts you for credentials.
Some users turn on the credential.helper store option, but this saves the password or Personal Access Token (PAT) in plain text to the computer. This is not safe at all.
Others don't make any helpful settings and have to enter username/password every time.
-
Problem: Storing credentials in plain text poses a security risk, and entering them every time is inefficient.
-
Solution: Use SSH keys to work with remote repos safely and practically.
This way, you don't have to enter your username/password for every transaction.
First create a new SSH key pair:
ssh-keygen -t ed25519 -C "mail@ornek.com"
After generating the key, grab your public key file (~/.ssh/id_ed25519.pub) and add it to your GitHub or GitLab account.
This way, your authentication is done automatically when connecting via SSH.
5. Not Verifying the Installation
After installing Git, it's important to check to make sure everything is working correctly.
Query your version to see if the installation was successful and to make sure Git is accessible in the system's PATH.
-
Problem: When you run a git command, you get the command not found error.
-
Solution: Check installation path and version:
# Yürütülebilir konumu kontrol edin
which git
# Yüklü sürümü kontrol edin
git --version
If these commands do not work, either there was an error in the installation or the PATH environment variable is set incorrectly.
6. Mixing Installation Methods
Using different methods together, such as installing Git both with apt and by compiling it from the source code, may cause problems.
This confuses which version of Git is running and makes removal difficult.
-
Problem: If there is more than one version of Git in the system, which version is running will be confused and unexpected errors may occur.
-
Solution: Choose a single installation method and go with it.
For most users, the PPA method is best.
If you need a specific version, you can compile it from source code, but make sure all other versions are completely deleted first.
Note:
PPA (Personal Package Archive) is a private software repository used in Ubuntu and its derivatives.
7. Not Backing Up Settings Files
The global .gitconfig file stores any custom settings you make.
If you don't sync this file when working on multiple computers, you'll have to deal with different settings and credentials on each device.
-
Problem: Git settings, shortcuts and user information differ between machines.
-
Solution: Save the settings files (commonly called “dotfiles”) to a Git repository and clone them on your other devices.
This way, you get the same environment everywhere.
Result
Git is now installed and configured on your Ubuntu server.
In this guide, we have seen two installation methods: apt packages for a fast and stable installation, and compiling from source code for the latest version.
You are also ready to commit by setting your user ID.
After that, you can start following your projects.
If you want, you can create a new repo for an existing project or start contributing by cloning a remote repository.
You can check out other guides to learn more and use version control efficiently.

