Introduction / Why This Is Needed
Git is the de facto standard for version control in modern development. It's essential for effectively collaborating on code within a team, participating in open-source projects on GitHub/GitLab, or even tracking changes in personal projects. Installing Git on Linux is typically the first task for any developer, DevOps engineer, or system administrator. This guide covers all popular distributions and provides working commands.
Prerequisites / Preparation
Before you begin, ensure that:
- You have access to a terminal (Ctrl+Alt+T or via the applications menu).
- You have superuser (
sudo) privileges to install packages. - You have an internet connection to download packages from official repositories.
- Important: The instructions use commands for modern distribution versions (Ubuntu 22.04+, Fedora 36+, RHEL 8+). For very old systems, package names or commands may differ.
Step-by-Step Guide
Step 1: Install Git
Choose the command corresponding to your distribution.
For Debian/Ubuntu-based distributions (Ubuntu, Linux Mint, Pop!_OS)
sudo apt update
sudo apt install git -y
apt update— updates the list of available packages.apt install git— installs thegitpackage.- The
-yflag automatically confirms the installation.
For RHEL/Fedora-based distributions (Fedora, RHEL, CentOS Stream, AlmaLinux, Rocky Linux)
sudo dnf install git -y
For older versions of CentOS 7/RHEL 7, use yum instead of dnf:
sudo yum install git -y
For Arch Linux and derivatives (Manjaro, EndeavourOS)
sudo pacman -S git --noconfirm
-S— the installation syntax forpacman.--noconfirm— skips confirmation prompts.
For openSUSE (Leap, Tumbleweed)
sudo zypper install git
Step 2: Verify the Installation
After the installation completes, verify that the git command is available and functional:
git --version
Expected output (example):
git version 2.43.0
If the command is not found, restart your terminal or run hash -r (in Bash) to refresh the command cache.
Step 3: Initial Configuration (Mandatory!)
Git requires you to specify your name and email. This data will be publicly visible in your commits on GitHub/GitLab.
git config --global user.name "Your Full Name"
git config --global user.email "your.email@example.com"
--global— sets the configuration for all repositories for the current user.- Replace
"Your Full Name"and"your.email@example.com"with your actual details.
Verify your configuration:
git config --list
You should see lines containing user.name and user.email.
Verification of Results
- Installation successful: The
git --versioncommand shows version 2.30 or higher. - Configuration complete: The
git config user.namecommand returns your name, andgit config user.emailreturns your email. - Test commit: Create a test repository to ensure everything works:
If themkdir ~/test-git && cd ~/test-git git init echo "# Test Project" > README.md git add README.md git commit -m "First commit"git commitcommand executes without errors (except for a warning about an empty email if you didn't set one), Git is fully ready for use.
Potential Issues
Error: git: command not found
- Cause: Git is not installed or its path is not included in the
PATHvariable. - Solution: Reinstall Git by following the steps above. If the problem persists, locate the path to the executable (
which gitorwhereis git) and add it toPATHin your~/.bashrcor~/.zshrcfile.
Permission error during installation (Permission denied)
- Cause: Attempting to install without superuser privileges.
- Solution: All installation commands must start with
sudo. Ensure your user is in thesudogroup (groups $USER).
Error: fatal: unable to access '...': Could not resolve host: github.com
- Cause: Network connectivity or DNS issues.
- Solution: Check your internet connection. Try pinging GitHub:
ping github.com. If the ping fails, check your DNS settings or firewall.
Issue: git --version shows an old version after installation
- Cause: In the official repositories of some distributions (especially LTS versions), the Git version may not be the latest.
- Solution: To get the latest version, you will need to add a third-party repository (PPA for Ubuntu) or build Git from source. For most tasks, the version from the official repository is sufficient.
# Example for Ubuntu (adds the official Git PPA)
sudo add-apt-repository ppa:git-core/ppa
sudo apt update
sudo apt install git
# Example for Fedora (uses the Copr repository)
sudo dnf copr enable @git/core
sudo dnf install git
# Building from source (a universal but complex method)
# 1. Install dependencies: build-essential, libssl-dev, libcurl4-gnutls-dev, etc.
# 2. Download the source from kernel.org
# 3. ./configure && make && sudo make install