Go is a statically typed, compiled language developed by Google that excels at building fast, portable command-line tools and system utilities. On RHEL 8, the version available through the default repositories may lag behind the upstream release, so installing directly from the official tarball ensures you get the latest stable toolchain. This tutorial walks you through downloading Go 1.22, configuring your environment, and building a production-ready CLI tool using the Cobra framework. By the end you will have a distributable binary and a working knowledge of cross-platform releases with GoReleaser.
Prerequisites
- A RHEL 8 server with a non-root sudo user
- EPEL 8 repository enabled (
dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm) - Internet access to reach dl.google.com and GitHub
- At least 1 GB of free disk space under
/usr/local
Step 1 — Download and Install the Go 1.22 Tarball
The official Go distribution is a self-contained tarball. Download it with wget, verify the checksum, and extract it to /usr/local.
wget https://dl.google.com/go/go1.22.3.linux-amd64.tar.gz -P /tmp
sha256sum /tmp/go1.22.3.linux-amd64.tar.gz
# Compare output against https://go.dev/dl/ before continuing
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf /tmp/go1.22.3.linux-amd64.tar.gz
/usr/local/go/bin/go version
Step 2 — Configure the Go Environment System-Wide
Create a profile drop-in so every user and login shell inherits the correct PATH and GOPATH.
sudo tee /etc/profile.d/go.sh << 'EOF'
export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
EOF
source /etc/profile.d/go.sh
go version
# Expected: go version go1.22.3 linux/amd64
Step 3 — Bootstrap a CLI Project with the Cobra Framework
Cobra is the most widely used framework for building CLI applications in Go. Install the cobra-cli scaffolding tool, then initialise a new project.
go install github.com/spf13/cobra-cli@latest
mkdir ~/myproject && cd ~/myproject
cobra-cli init --viper
# Creates main.go, cmd/root.go, and go.mod
go get github.com/spf13/cobra@latest
go mod tidy
Step 4 — Add Subcommands and Implement Logic
Use cobra-cli add to scaffold each subcommand, then fill in the business logic inside the generated Run function.
cobra-cli add greet
cobra-cli add version
# Edit cmd/greet.go and set the Run field:
# Run: func(cmd *cobra.Command, args []string) {
# name, _ := cmd.Flags().GetString("name")
# fmt.Printf("Hello, %s!n", name)
# },
# Add a persistent flag in cmd/root.go init():
# rootCmd.PersistentFlags().StringP("name", "n", "World", "Name to greet")
go run . greet --name RHEL
Step 5 — Build an Optimised Production Binary
Use linker flags to strip the symbol table and DWARF debug information, significantly reducing binary size.
cd ~/myproject
go build -ldflags="-s -w" -o mytool .
# Install system-wide
sudo cp mytool /usr/local/bin/mytool
sudo chmod 0755 /usr/local/bin/mytool
mytool greet --name "RHEL 8"
mytool version
Step 6 — Cross-Platform Releases with GoReleaser
GoReleaser automates building binaries for multiple OS/architecture combinations and can produce RPM and DEB packages from a single configuration file.
# Install GoReleaser
go install github.com/goreleaser/goreleaser@latest
# Initialise a .goreleaser.yaml in your project root
goreleaser init
# Perform a local dry-run snapshot (no VCS tag required)
goreleaser release --snapshot --clean
# Inspect generated archives in ./dist/
ls dist/
# mytool_Linux_x86_64.tar.gz mytool_Darwin_x86_64.tar.gz
# mytool_Linux_arm64.tar.gz checksums.txt
Conclusion
You have installed Go 1.22 from the official tarball on RHEL 8, configured a system-wide environment, scaffolded a Cobra CLI project, implemented subcommands with flags, produced an optimised binary with stripped debug symbols, and packaged it for multiple platforms with GoReleaser. This workflow gives you a repeatable, professional-grade pipeline for any Go-based tooling project.
Next steps: How to Write Unit Tests for Go CLI Tools with testify, How to Configure GitHub Actions CI/CD for Go on RHEL 8, and How to Build Go Microservices with gRPC on RHEL 8.