How To Install Go Lang on CentOS 8/7 & RHEL 8/7

2

ADVERTISEMENT

Go Lang is an open-source programming language developed by a team of Google engineers in 2007. Go language was designed to resolve the common criticisms of other languages while maintaining their positive characteristics and most widely used for writing servers these days.

Most notable projects using Go language are,

  1. Docker, a set of tools for deploying containers
  2. Juju, a service orchestration tool by canonical
  3. Dropbox moved the critical components to Go lang from python
  4. snappy, a package manager

More.

System Requirements

Go binary packages are available for the below supported operating systems. Please ensure your system meets these requirements before installing the Go language.

Operating system Architectures Notes
FreeBSD 8-STABLE or later x86_64 Debian GNU/FreeBSD not supported
Linux 2.6.23 or later with glibc x86_64, arm CentOS/RHEL 5 not supported
Mac OS X 10.7 or later x86_64 use the clang or gcc that comes with Xcode for cgo support
Windows XP or later x86_64, 386 use MinGW gcc. No need for Cygwin or msys.

This guide will help you to do the installation of Go Lang on CentOS 8/7 & RHEL 8 /7.

Download and Install Go Language

Before installing the Go language, update your system with the latest security patches to ensure the system is not vulnerable.

Update your system using the following command.

yum update -y

Install wget package.

yum install wget -y

Download the Go language binary package using the following command. Alternatively, you can visit the Go Language download page to download the latest version of Go.

wget https://golang.org/dl/go1.15.5.linux-amd64.tar.gz

Extract the archive to your desired location. Here, I choose to extract it on /usr/local.

tar -zxvf go1.15.5.linux-amd64.tar.gz -C /usr/local/

Setup Go Environment variables

Now you would need to set up two important variables for Go, GOROOT (PATH) and GOPATH. Add /usr/local/go/bin to your path variable.

GOROOT

You can add this by running the below command (temporary) or place the following command in /etc/profile or $HOME/.bash_profile file for persistent across sessions.

export PATH=$PATH:/usr/local/go/bin

If you have installed Go on custom location replace /usr/local/go/bin/ with /path/to/bin/ directory.

GOPATH

GOPATH is a Go environment variable for your project workspace. Let’s create a workspace directory called work in your home directory.

mkdir $HOME/work

Set the GOPATH variable. You can add this by running the below command (temporary) or place the following command in /etc/profile or $HOME/.bash_profile file for persistent across sessions.

ADVERTISEMENT

export GOPATH=$HOME/work

Verify Go Installation

Run the below command to see the version of the Go language.

go version

Output:

go version go1.15.5 linux/amd64

Check the Go environment variables we set in previous sections.

go env

Output:

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/work/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/work"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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-build604512806=/tmp/go-build -gno-record-gcc-switches"

Create First Project

Assuming that you have a workspace called work located in the $HOME directory.

I logged in as the root user, in my case /root/work is my workspace.

cd $GOPATH

OR

cd $HOME/work

Create a directory src/hello under your workspace.

mkdir -p src/hello

Let’s create a simple program (hello) to test the Go installation.

vi src/hello/hello.go

Place the following content in the hello.go file.

package main

import "fmt"

func main() {
    fmt.Printf("Welcome To ITzGeek\n")
}

Now, compile it with go command.

go install hello

go command will put an executable command (hello) inside the bin directory of your workspace. Execute the executable using the following command.

$GOPATH/bin/hello

You should get the following greeting text.

Welcome To ITzGeek

Conclusion

That’s all. You have successfully installed Go Lang on CentOS 8/7 & RHEL 8 /7.

You might also like