How to install Git on Linux
Installing Git on Linux is essential for version control, collaboration, and development workflows across all Linux distributions and server environments. As the creator of CoreUI, a widely used open-source UI library, I’ve guided thousands of Linux developers through Git installation for contributing to our repositories, managing server deployments, and maintaining enterprise development environments. From my expertise, the most reliable approach is to use your distribution’s package manager. This method provides automatic updates, proper system integration, and dependency management while ensuring compatibility with your Linux distribution and development tools.
Use your Linux distribution’s package manager to install Git with proper system integration and automatic updates.
# Ubuntu/Debian (apt) sudo apt update sudo apt install git # CentOS/RHEL 7 (yum) sudo yum install git # CentOS/RHEL 8+ (dnf) sudo dnf install git # Fedora (dnf) sudo dnf install git # Arch Linux (pacman) sudo pacman -S git # openSUSE (zypper) sudo zypper install git # Alpine Linux (apk) sudo apk add git # Amazon Linux (yum) sudo yum install git # Verify installation git --version # Output: git version 2.x.x # Check installation path which git # Output: /usr/bin/git # Alternative: Install latest version from source # Download latest source cd /tmp wget https://github.com/git/git/archive/v2.42.0.tar.gz tar -xzf v2.42.0.tar.gz cd git-2.42.0 # Install dependencies (Ubuntu/Debian) sudo apt install make libssl-dev libghc-zlib-dev libcurl4-gnutls-dev libreadline-dev autoconf build-essential gettext # Install dependencies (CentOS/RHEL) sudo yum groupinstall "Development Tools" sudo yum install gettext-devel openssl-devel perl-CPAN perl-devel zlib-devel curl-devel # Compile and install ./configure --prefix=/usr/local make all sudo make install # Alternative: Install via Snap (if available) sudo snap install git --classic # Alternative: Install via Flatpak flatpak install flathub org.git_scm.Git # Post-installation configuration # Set up user identity git config --global user.name "Your Name" git config --global user.email "[email protected]" # Configure default editor git config --global core.editor "nano" # or vim, code, etc. # Configure line endings (important for cross-platform) git config --global core.autocrlf input # Set default branch name git config --global init.defaultBranch main # Configure credential helper git config --global credential.helper store # View configuration git config --list # View specific config git config user.name git config user.email
# Distribution-specific advanced installation # Ubuntu/Debian: Install latest Git from PPA sudo add-apt-repository ppa:git-core/ppa sudo apt update sudo apt install git # CentOS/RHEL: Install from IUS repository for latest version sudo yum install https://repo.ius.io/ius-release-el7.rpm sudo yum install git236 # Fedora: Install development tools with Git sudo dnf groupinstall "Development Tools and Libraries" sudo dnf install git # Arch Linux: Install with additional tools sudo pacman -S git git-lfs gitk git-gui # Docker container installation # Create Dockerfile for Git-enabled container cat > Dockerfile << 'EOF' FROM ubuntu:22.04 RUN apt-get update && \ apt-get install -y git && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* # Set up Git user (replace with your details) RUN git config --global user.name "Container User" && \ git config --global user.email "[email protected]" WORKDIR /workspace EOF # Build and run container docker build -t git-container . docker run -it -v $(pwd):/workspace git-container bash # Verify Git works in container git --version
# Troubleshooting common issues # Fix "git: command not found" error echo $PATH # Check if /usr/bin is in PATH sudo ln -s /usr/bin/git /usr/local/bin/git # Create symlink if needed # Update Git to latest version # Ubuntu/Debian sudo apt update && sudo apt upgrade git # CentOS/RHEL sudo yum update git # Check for multiple Git installations whereis git ls -la /usr/bin/git* # Remove old Git version before installing new one sudo apt remove git # Ubuntu/Debian sudo yum remove git # CentOS/RHEL # Install Git with development headers (for building extensions) sudo apt install git git-core git-man # Ubuntu/Debian sudo yum install git git-core git-devel # CentOS/RHEL # Set up Git aliases for common commands git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.unstage 'reset HEAD --' git config --global alias.last 'log -1 HEAD' git config --global alias.visual '!gitk' # Test Git installation with a sample repository mkdir test-repo cd test-repo git init echo "# Test Repository" > README.md git add README.md git commit -m "Initial commit" git log --oneline # Clean up test cd .. rm -rf test-repo
Most Linux distributions include Git in their default repositories, making installation straightforward with the system package manager. The package manager approach ensures proper system integration, automatic security updates, and dependency resolution. For the latest Git version, use distribution-specific repositories like PPAs for Ubuntu or IUS for CentOS. After installation, configure your user identity and preferences globally. The package manager installation typically places Git in /usr/bin/git
and includes man pages and documentation.
Best Practice Note:
This is the same installation method we recommend for CoreUI contributors and Linux development teams in enterprise environments. Use the distribution’s package manager for system-wide installation, configure Git immediately after installation, keep Git updated through regular system updates, use official repositories for the latest versions, and test the installation with a simple repository to verify functionality.