How To Install Go on Ubuntu 18.04

In this tutorial, we will show you how to install Go on an Ubuntu 18.04 VPS, as well as show you how to build a simple Go application.

Go is an open-source, compiled programming language created by Google. Go is popular for many applications, including Docker and Kubernetes, which are written in Go. The language is designed for writing servers and provides a big set of libraries, making it very versatile and applicable for many use cases.

Let’s get started with the installation.

Prerequisites

  • For the purposes of this tutorial, we will use one of our Ubuntu 18.04 VPSes.
  • Full SSH root access or a user with sudo privileges is also required.

Step 1: Connect via SSH and Update

Connect to your server via SSH as the root user using the following command:

ssh root@IP_ADDRESS -p PORT_NUMBER

Remember to replace “IP_ADDRESS” and “PORT_NUMBER” with your server’s respective IP address and SSH port number.

Before starting with the installation, you will need to update your system packages to their latest versions. It’s easy to do, and it won’t take more than a few minutes.

You can do this by running the following command:

sudo apt-get update
sudo apt-get upgrade

Once the updates are completed, we can move on to the next step.

Step 2: Install Go

First, we will have to download the current binary archive file. Before downloading it, you should visit the official Go download page and check for the latest version.

We will use the wget command to download the latest stable version.

wget https://dl.google.com/go/go1.12.7.linux-amd64.tar.gz

To check the tarball checksum we will use the ‘sha256sum‘ command:

sha256sum go1.12.7.linux-amd64.tar.gz

The output should look similar to the following:

66d83bfb5a9ede000e33c6579a91a29e6b101829ad41fffb5c5bb6c900e109d9 go1.12.7.linux-amd64.tar.gz

You will have to make sure that the hash printed from the sha256sum command matches the one from the Go download page.

Next, we will extract the tar file to the /usr/local directory with the following command:

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

Now, we should tell on our system where to find the Go executable binaries. This will allow you to actually run the commands in your terminal. In order to do that we need to set the PATH environment variable. We have to append the path to the $HOME/.profile file for the current user.

nano $HOME/.profile

And add the following path:

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

We will save the file and load the new PATH variable into the current shell session by executing the following command.

source ~/.profile

Step 3: Verifying the Go installation

If everything is installed and set up correctly we should be able to execute the Go commands.

To verify the Go version execute the following command:

go version

The output should be similar to the following:

go version go1.12.7 linux/amd64

If you want to verify all of the configured environment variables you can use the following command:

go env

The output should be similar to the following:

GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build959242624=/tmp/go-build -gno-record-gcc-switches"

Go has been successfully installed and set up on the server. Next, we will test the installation with a simple example program.

Step 4: Testing the Go installation

We will create a new workspace directory and build a simple Go application.

To create a workspace directory run the following command:

mkdir ~/go

We need to create a new directory inside the workspace with the following command:

mkdir -p ~/go/src/helloGo

We should create a new file in that directory named helloGo.go and paste the following content:

package main

import "fmt"

func main() {
     fmt.Printf("Hello, Go\n")
}

Now, we need to build the file by running the following commands:

cd ~/go/src/helloGo
go build

We can then run the executable by executing the command below:

./helloGo

You should see the following output:

Hello, Go

That’s all there is to it – in this tutorial, we learned how to install Go on an Ubuntu 18.04 VPS, as well as how to build a simple Go application. Now you can take advantage of the large community and the many programs that are written in Go.


Of course, you don’t have to install Go on an Ubuntu 18.04 VPS if you use one of our Ubuntu VPS Plans, in which case you can simply ask our expert Linux admins to install Go on Ubuntu 18.04 for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post on how to install Go on Ubuntu 18.04,  please share it with your friends on the social networks using the share buttons below, or simply leave a reply down in the comments section. Thanks.

Leave a Comment