How to Install Go on RHEL 7

Go (also called Golang) is a statically typed, compiled programming language developed by Google, designed for simplicity, efficiency, and excellent support for concurrent programming. It has become a popular choice for building command-line tools, microservices, container runtimes (Docker and Kubernetes are both written in Go), and high-performance network services. RHEL 7’s default repositories contain an older version of Go that is typically not suitable for modern development. This guide demonstrates how to install a current Go release from the official binary distribution, configure your environment properly, write your first Go program, and use the module system to manage dependencies.

Prerequisites

  • RHEL 7 with root or sudo access
  • wget installed (sudo yum install -y wget)
  • At least 500 MB of free disk space in /usr/local
  • Basic familiarity with the Bash shell

Step 1: Remove Any Old Go Installation

If you previously installed Go from the RHEL repositories or manually, remove it first to avoid version conflicts:

# Remove yum-installed Go if present
sudo yum remove -y golang

# Remove a previous manual installation
sudo rm -rf /usr/local/go

Check that no Go binary remains in your PATH:

which go
go version

Both commands should return errors after removal.

Step 2: Download the Go Tarball

Visit https://go.dev/dl/ to find the latest stable release. This guide uses Go 1.22.3 as an example. Always download the Linux AMD64 archive for standard x86-64 servers:

cd /tmp
wget https://go.dev/dl/go1.22.3.linux-amd64.tar.gz

Verify the SHA-256 checksum against the value published on the Go downloads page:

sha256sum go1.22.3.linux-amd64.tar.gz

Compare the output hash character-by-character against the checksum listed on https://go.dev/dl/. Do not proceed if they do not match — a mismatch indicates a corrupted or tampered download.

Step 3: Extract Go to /usr/local

The official Go documentation recommends installing to /usr/local/go. Extract the tarball there, which creates the /usr/local/go directory automatically:

sudo tar -C /usr/local -xzf /tmp/go1.22.3.linux-amd64.tar.gz

Confirm the extraction was successful:

ls /usr/local/go/bin/
# Expected: go  gofmt

Step 4: Add Go to PATH with /etc/profile.d/

Create a profile script to add the Go binary directory to the PATH for all users on the system. This is the recommended approach on RHEL 7 because scripts in /etc/profile.d/ are automatically sourced for all login shells.

sudo tee /etc/profile.d/go.sh <<'EOF'
# Go programming language environment
export PATH=$PATH:/usr/local/go/bin
EOF

Apply it to your current session:

source /etc/profile.d/go.sh

Verify the installation:

go version

Expected output:

go version go1.22.3 linux/amd64

Also confirm gofmt (the Go source formatter) is available:

gofmt --help

Step 5: Configure GOPATH

Go uses a workspace directory called GOPATH to store downloaded module cache data and, in older Go versions (pre-modules), package source trees. In modern Go (1.11+), the module system largely supersedes GOPATH for source management, but the directory is still used for the module download cache. By default, GOPATH is $HOME/go for each user.

If you want a custom location, add it to the profile script:

sudo tee -a /etc/profile.d/go.sh <<'EOF'
# Custom GOPATH (optional — defaults to ~/go if not set)
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
EOF

source /etc/profile.d/go.sh

The $GOPATH/bin addition ensures that binaries installed with go install are available in your PATH.

Display the current Go environment configuration:

go env

Key variables to note in the output:

  • GOROOT — the Go installation directory (/usr/local/go)
  • GOPATH — your workspace/cache directory
  • GOOS — the target operating system (linux)
  • GOARCH — the target architecture (amd64)
  • GOMODCACHE — where downloaded module zip files are cached

Step 6: Initialize a Go Module

Modern Go development uses modules, which are collections of related Go packages versioned together. A module is defined by a go.mod file at the root of the project.

Create a new project directory and initialize a module:

mkdir -p ~/projects/hello
cd ~/projects/hello
go mod init github.com/yourusername/hello

This creates a go.mod file with content similar to:

module github.com/yourusername/hello

go 1.22

The module path does not need to match a real GitHub repository during local development, but it should be a valid unique identifier following the domain/path convention.

Step 7: Write and Run a Hello World Program

Create the main Go source file:

cat > ~/projects/hello/main.go <<'EOF'
package main

import (
    "fmt"
    "runtime"
)

func main() {
    fmt.Println("Hello, World!")
    fmt.Printf("Running on %s/%s with Go %sn",
        runtime.GOOS, runtime.GOARCH, runtime.Version())
}
EOF

Run it directly without compiling to a binary first:

go run main.go

Expected output:

Hello, World!
Running on linux/amd64 with Go go1.22.3

Step 8: Build a Compiled Binary

Compile the program to a standalone binary using go build:

go build -o hello main.go
./hello

The resulting hello binary is statically linked by default and can be copied to any compatible Linux system without needing Go installed:

# Check binary details
file hello
ldd hello

For a pure Go program with no C dependencies, ldd will report not a dynamic executable, confirming the binary is fully self-contained.

To strip debug symbols and reduce the binary size:

go build -ldflags="-s -w" -o hello main.go
ls -lh hello

Conclusion

Installing Go on RHEL 7 from the official tarball takes only a few minutes and gives you access to the latest language features and performance improvements unavailable in the distribution’s package repositories. By extracting to /usr/local/go, adding the binary directory to PATH through /etc/profile.d/go.sh, and using the module system for dependency management, you have a clean, maintainable Go development environment. The statically compiled binaries that Go produces make deployment simple: a single file copied to the target server is all that is needed to run your application.