How to install Go in Fedora and Rocky Linux/Centos/RHEL

This tutorial will help you install Go(Golang) on a Fedora or a RHEL based system i.e. Centos/Rocky Linux/Alma Linux/RHEL. This guide can also work for other Linux systems like Debian and Ubuntu besides the Redhat specifics like the running the yum commands.

Go is a statically typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. It’s has always been said to be an easy language for developers to learn quickly.

Golang is very useful for writing light-weight microservices, infrastructure like networked servers and also tools and systems for developers. It can alsobe used for generating APIs that interact with front-end applications. If you want to build a small functional microservice quickly, then Golang is a great tool to use.

Prerequisites

To follow along this guide, ensure that you have:

  • An up to date Fedora or a Redhat based system i.e. Centos/Rocky Linux/Alma Linux/RHEL
  • Connection to the internet
  • Knowledge of Linux Terminal

Table of Content

  1. Updating the server
  2. Installing Go
  3. Setting up Go environment
  4. Verifying the installation and creating Hello World App with Go

Updating the server

Before proceeding, it is always a good practice to ensure that your server packages are up to date. Use this command to ensure that server packages are updated

sudo yum -y update

Installing Go

Golangis not available in the default Fedora or RHEL repositories. Instead, it is available as a downloadable package from the Golang website downloads page https://golang.org/dl/. We are going to download the Linux package.

The latest version of Golang as of the writting of this tutorial is 1.17.2. Download it uing this curl command:

curl -LO https://dl.google.com/go/go1.17.2.linux-amd64.tar.gz 

Extract the archive you downloaded to your desired location in the system. I am extracting it to the/usr/local directory, creating a Go tree in /usr/local/go.

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

In a system where you do not have access to the root system or if you want to install it for your user alone, you can extract it to a path in the home directory.

Setting up Go Environment

Now that Golang is downloaded to a local path, we need to set up the environment. Golang normally uses the two variables, GOROOT and GOPATH to define the root for golang packages and the location of the working directory. We need to add the golang binary directory to the executable directories as well using the PATH variable.

Add this content to the ~/.bashrc to achieve the above mentioned.

Set the GOROOT location where the Go packages are installed in the system

export GOROOT=/usr/local/go 

Next, let’s set the GOPATH, the location of our working directory. I normally prefer having Golang work in ~/go, so I will set it using this:

export GOPATH=$HOME/go

Finally set up the PATH by appending the go binary paths so we access go binary system wide

export PATH=$GOPATH/bin:$GOROOT/bin:$PATH 

To apply the above chages you need to logout and login back to the shell or use the source command to apply the changes:

source ~/.bashrc

Verifying the installation and creating Hello World App with Go

Now that you have successfully installed and configured go language on your system, let’s confirm that it is working fine by checking its version:

$ go version
go version go1.17.2 linux/amd64

Now also verify all configured environment variables using following command.

$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/fedora/.cache/go-build"
GOENV="/home/fedora/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/fedora/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/fedora/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.2"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build970078185=/tmp/go-build -gno-record-gcc-switches"

Let us create a simple Hello world program to test our installation. Create a file main.go in the current directory and add these content to it.

package main

import "fmt"

func main(){
	fmt.Println("Citizix - Hello World!")
}

Then run the program with this command:

$ go run main.go
Citizix - Hello World!

Conclusion

Congratulations! In this guide, you have learned to install Golang on Fedora & CentOS/RHEL Linux systems.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy