Installing Go (Golang) Properly on Rocky Linux, RHEL, AlmaLinux, and CentOS

Contents

When installing Go (Golang) on Enterprise Linux systems, the default package repositories are usually behind the official Go release cycle. This can cause compatibility issues when compiling modern projects or building statically linked binaries.

This guide walks through a proper, official installation method using the Go project’s own binaries. The result is a clean, system-wide Go installation that is safe to re-run and suitable for development, automation, and production tooling.


Why Not Use dnf install golang?

While Go is available in most RHEL-based repositories, it comes with drawbacks:

  • Versions are often several releases behind

  • Not ideal for modern Go modules

  • Poor choice for building portable or static binaries

For serious development, the official Go tarball is the correct approach.


Installation Overview

This method:

  • Installs Go from go.dev

  • Removes any existing distro Go packages

  • Sets system-wide environment variables

  • Creates a clean Go workspace

  • Is safe to re-run at any time


Go Installation Script

Save the following as install-go.sh:

#!/bin/bash
# ------------------------------------------------------------
# Go (Golang) Official Install Script
# - Installs latest Go from go.dev
# - Sets system-wide environment variables
# - Removes distro Go if present
# - Safe to re-run
# ------------------------------------------------------------

set -e

GO_VERSION="1.22.2"
GO_TARBALL="go${GO_VERSION}.linux-amd64.tar.gz"
GO_URL="https://go.dev/dl/${GO_TARBALL}"
INSTALL_DIR="/usr/local"
PROFILE_FILE="/etc/profile.d/go.sh"

echo "==> Installing Go ${GO_VERSION}"

# ------------------------------------------------------------
# Require root
# ------------------------------------------------------------
if [[ $EUID -ne 0 ]]; then
  echo "ERROR: Run as root or with sudo"
  exit 1
fi

# ------------------------------------------------------------
# Remove old Go installs
# ------------------------------------------------------------
echo "==> Removing old Go installs (if any)"
dnf -y remove golang >/dev/null 2>&1 || true
rm -rf /usr/local/go

# ------------------------------------------------------------
# Download Go
# ------------------------------------------------------------
echo "==> Downloading Go"
cd /tmp
curl -fsSLO "${GO_URL}"

# ------------------------------------------------------------
# Install
# ------------------------------------------------------------
echo "==> Extracting Go to ${INSTALL_DIR}"
tar -C "${INSTALL_DIR}" -xzf "${GO_TARBALL}"

# ------------------------------------------------------------
# Environment setup
# ------------------------------------------------------------
echo "==> Configuring environment variables"
cat << 'EOF' > "${PROFILE_FILE}"
# Go environment
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
EOF

chmod 644 "${PROFILE_FILE}"

# ------------------------------------------------------------
# Create workspace skeleton (for current user invoking sudo)
# ------------------------------------------------------------
if [[ -n "$SUDO_USER" ]]; then
  USER_HOME=$(eval echo "~${SUDO_USER}")
  mkdir -p "${USER_HOME}/go"/{bin,src,pkg}
  chown -R "${SUDO_USER}:${SUDO_USER}" "${USER_HOME}/go"
fi

# ------------------------------------------------------------
# Cleanup
# ------------------------------------------------------------
rm -f "/tmp/${GO_TARBALL}"

# ------------------------------------------------------------
# Verify
# ------------------------------------------------------------
source "${PROFILE_FILE}"

echo "==> Go installed successfully"
go version

echo
echo "NOTE:"
echo " - Open a NEW shell to apply PATH changes"
echo " - GOPATH default: $HOME/go"
echo " - GOROOT: /usr/local/go"

Running the Installer

chmod +x install-go.sh
sudo ./install-go.sh

After installation, open a new shell and verify:

go version

Expected output:

go version go1.22.2 linux/amd64

Go Environment Layout

This installation follows Go best practices:

Variable Purpose
GOROOT Go installation directory
GOPATH User workspace (~/go)
PATH Includes Go binaries

Workspace structure:

~/go/
├── bin
├── src
└── pkg

Why This Method Works Well

  • Uses official Go binaries

  • Avoids stale distro packages

  • Safe for CI, servers, and desktops

  • Clean system-wide configuration

  • Ideal for compiling tools and static binaries


Final Notes

  • This method works on Rocky Linux, RHEL, AlmaLinux, and CentOS

  • Updating Go later is as simple as changing the version and re-running the script

  • Perfect foundation for building Go-based CLI tools, monitors, and services