(Last Updated On: September 17, 2018)

Let’s cover how to install latest Go on CentOS 7 / Ubuntu 18.04 server. Go is an open source programming language with a novel type system to enable flexible and modular program construction, and make programmers more productive. It uses concurrency mechanisms which makes it easy to write programs that get the most out of multicore and networked machines. Go has been known to be efficient, clean, expressive and concise.

Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It’s a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.

How to Install latest Go on CentOS 7

We have two options to install latest Go on CentOS 7

Method 1: Using Go repository ( For CentOS 7 only)

We will use a Go repository to ensure we are getting the latest release without having to build it.

rpm --import https://mirror.go-repo.io/centos/RPM-GPG-KEY-GO-REPO
curl -s https://mirror.go-repo.io/centos/go-repo.repo | tee /etc/yum.repos.d/go-repo.repo

Once the repository has been added, install golang package

sudo yum install golang

Go binary will be placed under /bin/go

# which go
/bin/go

For Ubuntu 18.04

For Ubuntu 18.04 server, run

$ sudo add-apt-repository ppa:gophers/archive
$ sudo apt-get update
$ sudo apt-get install golang-go

Set $GOPATH

mkdir -p ~/go/{bin,pkg,src}
echo 'export GOPATH="$HOME/go"' >> ~/.bashrc
echo 'export PATH="$PATH:${GOPATH//://bin:}/bin"' >> ~/.bashrc

Method 2: Manually Download Go

The second method involves downloading Go archive and installing it to the system. You may need to check Go releases page for the latest version.

Ensure you have wget installed

sudo yum install wget
sudo apt install wget

Then download Go

VER=1.11
wget https://dl.google.com/go/go${VER}.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go${VER}.linux-amd64.tar.gz
rm go${VER}.linux-amd64.tar.gz

Set Go PATH Variable

vim ~/.bashrc

Add

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

Source the file for PATH to be updated

source /etc/profile

Testing

mkdir -p ~/go/src/test
vim ~/go/src/test/test.go

Add print Hello Gophers message

package main
import "fmt"
func main() {
    fmt.Printf("Hello, Gophers\n")
}

Build test.go file

cd ~/go/src/test

Run binary to test

$ ./test 
Hello, Gophers

Thanks for using our guide to install latest Go on CentOS 7 and Ubuntu 18.04 server.