For our Blog Visitor only Get Additional 3 Month Free + 10% OFF on TriAnnual Plan YSBLOG10
Grab the Deal

How to Install Git on Linux Server in 2026 – Complete Setup Guide

To install Git on a Linux server, use your distribution’s package manager and then configure your identity. For example: Debian/Ubuntu: sudo apt update && sudo apt install git; RHEL/AlmaLinux/Rocky: sudo dnf install git; Fedora: sudo dnf install git; openSUSE: sudo zypper install git; Arch: sudo pacman -S git. Verify with git --version.

Installing Git on a Linux server is a quick way to enable modern version control, reliable deployments, and collaborative workflows. In this guide, you’ll learn how to install Git on Linux server distributions, verify and configure it correctly, keep it updated, compile the latest release from source, and even host a simple Git server over SSH.

Why Git on a Linux Server Matters?

Install Git on Linux Server

Git is the de facto standard for source control. On servers, it powers CI/CD pipelines, automates deployments, and provides auditable changes to your infrastructure-as-code and apps. With Git installed on your Linux server, you can pull code securely, tag releases, and roll back confidently—best practices we use daily across production environments.

Prerequisites

  • Root or sudo access to the Linux server
  • Network connectivity to your distro repositories (or Git source mirrors)
  • Basic terminal knowledge (running commands as sudo)
  • Optional: SSH key pair for secure Git operations

Install Git Using Your Distribution’s Package Manager

The fastest, safest method is your OS package manager. It ensures dependencies, security patches, and stable updates. Use the commands below for your distribution.

Debian/Ubuntu and derivatives

sudo apt update
sudo apt install -y git
git --version

On older Ubuntu LTS versions, you can get newer Git by enabling the official PPA:

sudo add-apt-repository ppa:git-core/ppa -y
sudo apt update && sudo apt install -y git
git --version

RHEL, CentOS Stream, AlmaLinux, Rocky Linux

sudo dnf install -y git
git --version

For very old RHEL/CentOS systems, enable CodeReady Builder/EPEL if needed, or consider building from source to get newer versions.

Fedora

sudo dnf install -y git
git --version

openSUSE Leap/Tumbleweed

sudo zypper refresh
sudo zypper install -y git
git --version

Arch Linux/Manjaro

sudo pacman -Sy --needed git
git --version

Using Snap (multi-distro)

sudo snap install git --classic
git --version

Verify Your Installation

Confirm the binary and path, then check global configuration:

git --version
which git
git config --list --show-origin

If the command isn’t found, ensure your package manager completed successfully and that /usr/bin or /usr/local/bin is in your PATH.

Configure Git After Installation

Set your identity (required)

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Improve defaults

# Use "main" for new repos
git config --global init.defaultBranch main

# Enable helpful hints and sensible diffs
git config --global help.autocorrect 1
git config --global color.ui auto
git config --global pull.ff only
git config --global fetch.prune true

# Choose your editor (nano/vim/Code)
git config --global core.editor "nano"

Store credentials securely

On servers, prefer SSH keys over HTTPS passwords. If you must use HTTPS, consider a credential helper with short caching windows:

# Cache HTTPS credentials for 15 minutes
git config --global credential.helper 'cache --timeout=900'
ssh-keygen -t ed25519 -C "you@example.com"
# Add the key to the agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# Print your public key to add to Git host (GitHub/GitLab/etc.)
cat ~/.ssh/id_ed25519.pub

Test SSH connectivity to your Git provider:

ssh -T git@github.com
# or
ssh -T git@gitlab.com

Install the Latest Git from Source (Optional)

If your distribution ships an older Git but you need new features or fixes, compile from source. This is common on long-term support servers or locked-down repos.

1) Install build dependencies

# Debian/Ubuntu
sudo apt update
sudo apt install -y build-essential libssl-dev libz-dev \
    libcurl4-gnutls-dev libexpat1-dev gettext unzip

# RHEL/CentOS/Alma/Rocky
sudo dnf groupinstall -y "Development Tools"
sudo dnf install -y curl-devel expat-devel openssl-devel zlib-devel gettext

2) Download, build, and install

cd /usr/src
sudo curl -LO https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.46.0.tar.xz
sudo tar -xf git-2.46.0.tar.xz
cd git-2.46.0
sudo make prefix=/usr/local all
sudo make prefix=/usr/local install

# Verify
/usr/local/bin/git --version

Installing to /usr/local avoids overwriting distro packages. Ensure /usr/local/bin precedes /usr/bin in PATH for the newer Git to take precedence.

3) Manage multiple versions

# Update PATH session-wide
echo 'export PATH=/usr/local/bin:$PATH' | sudo tee /etc/profile.d/usr-local-bin.sh
source /etc/profile

# Pin a user to a specific binary
echo 'alias git=/usr/local/bin/git' >> ~/.bashrc

Keep Git Updated

Staying current reduces vulnerabilities and brings performance improvements—important on build and deployment servers.

Update via package manager

# Debian/Ubuntu
sudo apt update && sudo apt -y upgrade git

# RHEL/Alma/Rocky/Fedora
sudo dnf upgrade -y git

# openSUSE
sudo zypper update git

# Arch
sudo pacman -Syu git

Update source build

# Build and install the new release as before
cd /usr/src
sudo curl -LO https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.46.1.tar.xz
sudo tar -xf git-2.46.1.tar.xz
cd git-2.46.1
sudo make prefix=/usr/local all
sudo make prefix=/usr/local install
git --version

Set Up a Simple Git Server over SSH

For small teams and internal use, hosting a basic Git remote via SSH is fast and secure. This approach avoids extra services and leverages existing Linux permissions.

1) Create a dedicated user

sudo adduser --system --shell /usr/bin/git-shell --group --home /srv/git git
sudo mkdir -p /srv/git
sudo chown -R git:git /srv/git

git-shell restricts users to Git commands only, improving security.

2) Add developer SSH keys

sudo -u git mkdir -p /srv/git/.ssh
sudo -u git chmod 700 /srv/git/.ssh
sudo -u git sh -c 'cat >> /srv/git/.ssh/authorized_keys'
# paste public keys, then Ctrl+D
sudo -u git chmod 600 /srv/git/.ssh/authorized_keys

3) Create a bare repository

sudo -u git git init --bare /srv/git/app.git

Developers can now push using:

git remote add origin git@your-server:/srv/git/app.git
git push -u origin main

Security and Best Practices

  • Use SSH keys, not passwords, and restrict with git-shell on server accounts.
  • Keep Git updated to patch security vulnerabilities.
  • Audit authorized_keys regularly; remove stale keys immediately.
  • Enable 2FA on your Git provider accounts (GitHub, GitLab, Bitbucket).
  • Backup bare repositories and test restore procedures.
  • For multi-tenant servers, isolate with dedicated Unix users or containers.

Troubleshooting Common Git Installation Issues

Command not found or wrong version

Check that Git is in your PATH and no older binary shadows it:

which -a git
echo $PATH

Adjust PATH to put /usr/local/bin first if you compiled from source.

SSL or proxy errors when fetching

Behind corporate proxies, set proxy variables or Git proxy config:

export https_proxy=http://proxy.example.com:3128
export http_proxy=http://proxy.example.com:3128
git config --global http.proxy http://proxy.example.com:3128

Permission denied (publickey)

Ensure your SSH key is added to the agent and uploaded to the Git provider:

ssh-add -l
ssh -T git@github.com

“fatal: not a git repository”

Run commands inside your project’s Git directory or initialize one:

git init
git status

Uninstall Git (If Needed)

Package-managed installs

# Debian/Ubuntu
sudo apt remove -y git
# RHEL/Fedora/Alma/Rocky
sudo dnf remove -y git
# openSUSE
sudo zypper remove -y git
# Arch
sudo pacman -R git

Source installs

If installed to /usr/local, remove the binary and related directories cautiously:

sudo rm -f /usr/local/bin/git
sudo rm -rf /usr/local/libexec/git-core
sudo rm -rf /usr/local/share/git-core

Real-World Use Cases on Servers

  • Zero-downtime deploys: Pull from a tagged release to a symlinked current/ directory and atomically switch.
  • Infrastructure as Code: Track Ansible, Terraform, or Helm charts and audit every change.
  • CI/CD runners: Build, test, and release artifacts on ephemeral or dedicated runners using Git workflows.
  • Secure mirroring: Mirror upstream repositories to a private server for internal builds and resilience.

FAQ’s

1. Is it better to install Git from my distro or from source?

For most servers, the distro package is best—stable, patched, and easy to update. Build from source if you need newer features than your repository provides or if you’re constrained by older enterprise repositories. Install to /usr/local to avoid conflicts.

2. How do I set a default branch name to main for new repos?

Run: git config --global init.defaultBranch main. This sets the default branch for all new repositories you initialize on the server.

3. How do I update Git to the latest version on Ubuntu?

Use the official Git PPA: sudo add-apt-repository ppa:git-core/ppa -y, then sudo apt update && sudo apt install git. Verify with git --version. Alternatively, compile from source if you want a specific point release.

4. Should I use SSH or HTTPS for Git on servers?

Use SSH keys on servers. They are more secure, don’t expose passwords, and support tighter restrictions (e.g., git-shell). HTTPS is fine for ad-hoc usage but avoid storing long-lived credentials on servers.

5. How can I run a private Git server without extra software?

Create a dedicated git user, restrict it with git-shell, add SSH keys, and host bare repositories in a directory like /srv/git/. This simple SSH-based approach is secure and lightweight for small teams.

6. What’s the safest way to remove an old Git version when I compiled a new one?

Install the new Git to /usr/local and ensure your PATH prioritizes /usr/local/bin. Test thoroughly. When ready, remove the old package-managed Git with your package manager to prevent confusion.

7. Can I automate Git installs across multiple servers?

Yes. Use configuration management tools like Ansible, Chef, or Puppet to install Git, enforce configs (identity, default branch), deploy SSH keys, and set PATH. This ensures consistency and repeatability across environments.

Conclusion

You can install Git on Linux server distributions in minutes using your package manager or compile the newest version from source for cutting-edge features. Configure your identity, prefer SSH keys, keep Git updated, and back up your repos. If you manage production workloads, these practices pay dividends in reliability and security.

Need a secure, performance-optimized server to host private Git remotes or CI runners? YouStable provides SSD/NVMe hosting and managed Linux servers with hardened security, 24/7 monitoring, and expert support. We can preinstall and configure Git to your standards so your team ships faster with confidence.

Prahlad Prajapati

Prahlad is a web hosting specialist and SEO-focused organic growth expert from India. Active in the digital space since 2019, he helps people grow their websites through clean, sustainable strategies. Passionate about learning and adapting fast, he believes small details create big success. Discover his insights on web hosting and SEO to elevate your online presence.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top