BLOG POSTS
How to Install Git on CentOS 8

How to Install Git on CentOS 8

Git is the backbone of modern software development – a distributed version control system that’s become absolutely essential for tracking code changes, collaborating with teams, and managing software projects of any size. Whether you’re setting up a development environment or configuring a production server, having Git properly installed on your CentOS 8 system is a fundamental requirement. This guide will walk you through multiple installation methods, from the straightforward package manager approach to compiling from source, plus troubleshooting common issues you might encounter along the way.

Understanding Git Installation Options on CentOS 8

CentOS 8 gives you several paths to get Git up and running, each with its own trade-offs. The default package repository typically contains a stable but potentially older version of Git, while installing from source gives you access to the latest features and bug fixes. Before diving into installation, it’s worth understanding what each method brings to the table.

The package manager approach (dnf) is your quickest route – it handles dependencies automatically and integrates cleanly with your system’s package management. However, you’re limited to whatever version the CentOS maintainers have packaged. Source compilation takes more time and requires additional development tools, but you get complete control over the version and can include or exclude specific features.

Method 1: Installing Git via DNF Package Manager

The most straightforward approach uses CentOS 8’s default package manager. This method will get you a working Git installation in under a minute, assuming you have sudo privileges.

First, update your package index to ensure you’re working with the latest repository information:

sudo dnf update -y

Install Git using the dnf package manager:

sudo dnf install git -y

Verify the installation by checking the Git version:

git --version

You should see output similar to:

git version 2.27.0

The exact version number will depend on what’s available in the CentOS 8 repositories at the time of installation. While this version might not be the absolute latest, it’s thoroughly tested for stability and compatibility with CentOS 8.

Method 2: Installing Latest Git from Source

When you need the latest Git features or want to customize your installation, compiling from source is the way to go. This process requires a few more steps but gives you complete control over your Git installation.

Start by installing the development tools and dependencies needed for compilation:

sudo dnf groupinstall "Development Tools" -y
sudo dnf install gettext-devel openssl-devel perl-CPAN perl-devel zlib-devel curl-devel -y

Remove any existing Git installation to avoid conflicts:

sudo dnf remove git -y

Download the latest Git source code from the official repository. Check the Git releases page for the most current version:

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

Configure and compile Git:

make configure
./configure --prefix=/usr/local
make all

Install the compiled version:

sudo make install

Add the new Git binary to your PATH by creating a symbolic link:

sudo ln -sf /usr/local/bin/git /usr/bin/git

Verify your installation:

git --version

Method 3: Using EPEL Repository for Newer Versions

The Extra Packages for Enterprise Linux (EPEL) repository often contains more recent versions than the default CentOS repositories, striking a balance between the convenience of package management and access to newer features.

Enable the EPEL repository:

sudo dnf install epel-release -y

Update your package cache:

sudo dnf update -y

Install Git from EPEL:

sudo dnf install git -y

This method typically provides a version that’s newer than the default CentOS packages but still maintains good stability and compatibility.

Initial Git Configuration

After installation, regardless of method, you’ll want to configure Git with your identity information. This is crucial for commit attribution and is required for most Git operations.

Set your global username and email:

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

Configure your preferred text editor for Git operations:

git config --global core.editor "vim"

Verify your configuration:

git config --list

For enhanced security, especially when working with remote repositories, consider setting up SSH keys:

ssh-keygen -t ed25519 -C "your.email@example.com"

Installation Method Comparison

Method Speed Version Control Maintenance Best For
DNF Package Manager Very Fast (1-2 minutes) Limited to repository version Automatic updates Production servers, quick setup
Source Compilation Slow (10-15 minutes) Any version available Manual updates required Latest features, custom builds
EPEL Repository Fast (2-3 minutes) Newer than default Automatic updates Balance of new features and stability

Common Issues and Troubleshooting

Even straightforward installations can hit snags. Here are the most common issues you’ll encounter and their solutions:

Permission Denied Errors: If you see permission denied errors during installation, ensure you’re using sudo for commands that require root privileges. Double-check that your user account has sudo access.

Missing Dependencies: When compiling from source, missing development tools can cause compilation failures. The error messages are usually specific about what’s missing:

# If you see SSL-related errors
sudo dnf install openssl-devel -y

# For compression-related issues
sudo dnf install zlib-devel -y

# For Perl-related problems
sudo dnf install perl-devel -y

Path Issues: After source installation, if Git commands aren’t found, check your PATH variable:

echo $PATH
which git

If the Git binary isn’t in your PATH, add it to your shell profile:

echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Version Conflicts: Multiple Git installations can cause confusion. Clean up by removing unwanted versions:

# Remove package manager version
sudo dnf remove git -y

# Remove source-compiled version
sudo rm -rf /usr/local/bin/git
sudo rm -rf /usr/local/libexec/git-core

Real-World Use Cases and Examples

Git installation is just the beginning – here are some practical scenarios where different installation methods shine:

Development Server Setup: When setting up a development environment on a VPS, the source compilation method ensures you have access to the latest Git features and security patches. This is particularly important when working with modern frameworks that depend on newer Git capabilities.

Production Web Servers: For production environments running on dedicated servers, the package manager approach provides stability and automatic security updates. The slightly older version is usually not a limitation for deployment scripts and automated workflows.

CI/CD Pipeline Integration: Continuous integration servers benefit from EPEL installations, which provide a good balance of features and stability. Here’s a typical automation script for CI server setup:

#!/bin/bash
# CI Server Git Installation Script
sudo dnf install epel-release -y
sudo dnf update -y
sudo dnf install git -y

# Configure Git for CI operations
git config --global user.name "CI Server"
git config --global user.email "ci@yourcompany.com"
git config --global init.defaultBranch main

Best Practices and Security Considerations

Proper Git installation extends beyond just getting the binary working. Consider these best practices for a secure and maintainable setup:

  • Always verify Git installations with version checks and basic functionality tests
  • Keep your Git installation updated, especially for security patches
  • Use SSH keys instead of HTTPS authentication for better security
  • Configure proper file permissions for Git repositories
  • Consider using Git hooks for automated security scanning

For servers handling sensitive code repositories, implement additional security measures:

# Set restrictive umask for Git operations
echo "umask 077" >> ~/.bashrc

# Configure Git to use SSH by default
git config --global url."git@github.com:".insteadOf "https://github.com/"

Monitor your Git installation for security updates by subscribing to the official Git security announcements and maintain an update schedule appropriate for your environment’s risk tolerance.

The installation method you choose ultimately depends on your specific requirements – quick deployment, latest features, or balanced approach. Each method covered here will give you a fully functional Git installation ready for version control operations, repository management, and integration with your development workflow.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

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