(Last Updated On: January 26, 2019)

In this guide, I’ll walk you through different ways you can install Go on Ubuntu 18.04 / CentOS 7 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.

Go 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.

It is a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.

Linux Mint 19: How to Install Go (Golang) on Linux Mint 19

For RHEL 8 / CentOS 8: How to Install Go on RHEL 8

How to Install 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.

sudo rpm --import https://mirror.go-repo.io/centos/RPM-GPG-KEY-GO-REPO
curl -s https://mirror.go-repo.io/centos/go-repo.repo | sudo 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

How to Install Go on Ubuntu 18.04

For Ubuntu 18.04 server, add Golang backports PPA repository and install Go on Ubuntu 18.04.

sudo add-apt-repository ppa:longsleep/golang-backports 
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: Install Go using Golang installer

The second method is the installation of Go from provided Golang installer.

First install wget

sudo apt-get -y install wget
sudo yum -y install wget

Download installer with wget to your local system.

wget https://storage.googleapis.com/golang/getgo/installer_linux

After downloading the file, make it executable:

chmod +x ./installer_linux

And lastly run the installer from your current terminal shell.

$ ./installer_linux 
Welcome to the Go installer!
Downloading Go version go1.11.5 to /home/jmutai/.go
This may take a bit of time…
Downloaded!
Setting up GOPATH
GOPATH has been set up!

One more thing! Run source /home/jmutai/.bash_profile to persist the new environment variables to your current session, or open a
new shell prompt.

Source the ~/.bash_profile to start using Go environment variables in your current session.

$ source ~/.bash_profile
$ go version
go version go1.11.5 linux/amd64

Method 3: 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

Run the commands below to set your set $GOPATH

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

Testing Go installation on Ubuntu 18.04 / CentOS 7

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
go build

Run binary to test

$ ./test
Hello, Gophers

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

Tags:

How to Install Golang on Ubuntu 18.04

How to Install Golang on CentOS 7

How to Install latest Golang on Ubuntu 18.04

How to Install latest Golang on CentOS 7

How to Install latest Go on Ubuntu 18.04

How to Install latest Go on CentOS 7