Install Go on Ubuntu 18.04 Bionic Beaver Linux

Objective

The objective is to install Go on Ubuntu 18.04. Go also know as Golang, is an open source programming language developed by Google.

This guide will show you how to install Go on Ubuntu using a standard Ubuntu repository by use of the apt command and use of the Google’s Golang installer as well as how to install go using the snap command.

Operating System and Software Versions

  • Operating System: – Ubuntu 18.04 Bionic Beaver
  • Software: – go version go1.10 linux/amd64 or higher

Requirements

Privileged access to your Ubuntu System as root or via sudo command is required.

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Other Versions of this Tutorial

Ubuntu 20.04 (Focal Fossa)

Instructions

Install Prerequisites

To install and test Go we will need the following linux commands: wget and git. These tools are most likely already installed on your Ubuntu system. In case they are not, run:

$ sudo apt install wget git

Install Go using Golang installer

Using Google’s golang installer is most likely the most convenient way how to install Go on your Ubuntu system. Let’s start by downloading the Golang installer:

$ wget -q https://storage.googleapis.com/golang/getgo/installer_linux

Make the Golang installation executable:

$ chmod +x installer_linux 

Lastly, run the installer_linux executable to install Go on your Ubuntu system. The installer will download and install the latest version Go:

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

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

As prompted by installer, run the source command to update your current shell session variables to include the new GOPATH, or simply open a new shell session:

$ source /home/linuxconfig/.bash_profile

Check for go version:

$ go version
go version go1.10 linux/amd64

Lastly, as a test, try to get and execute Golang’s hello world example:

$ go get github.com/golang/example/hello
$ hello 
Hello, Go examples!


Install Go on ubuntu using golang installer

Go language on Ubuntu 18.04

The disadvantage of using Golang installer to install Go on Ubuntu system is that we are unable to select an installation version. But I’m sure this is just a temporary setback:

$ ./installer_linux -i
Welcome to the Go installer!
Would you like to install Go? Y/n [Y]: y
The latest go version is go1.10, install that? Y/n [Y]: n
Aborting install.

Install Go from Ubuntu repostiory

Another alternative to install Go on Ubuntu 18.04 is by using the apt command to install Go binaries from the Ubuntu’s repository:

$ sudo apt install golang

This will install the latest traditional package which at the the time of writing is go version go1.10:

$ go version
go version go1.10 linux/amd64

Before we perform a test we first need to set GOPATH:

$ echo 'export GOPATH=$HOME/go' >> ~/.bashrc 
$ echo 'export PATH=${PATH}:${GOPATH}/bin' >> ~/.bashrc 
$ source ~/.bashrc 

Perform a go command test with pre-compiled hello world:

$ go get github.com/golang/example/hello
$ hello 
Hello, Go examples!

Install Go using snap

In this section we will perform an installation of Go on Ubuntu 18.04 using the snap command. To install Go on Ubuntu 18.04 using snap is as easy as the following linux command:

$ sudo snap install go --classic
go 1.10 from 'mwhudson' installed

Check for installed version:

$ go version
go version go1.10 linux/amd64

Next, set GOPATH:

$ echo 'export GOPATH=$HOME/go' >> ~/.bashrc 
$ echo 'export PATH=${PATH}:${GOPATH}/bin' >> ~/.bashrc 
$ source ~/.bashrc 

Lastly, to test go installation, use the go command to download and execute Hello World example:

$ go get github.com/golang/example/hello
$ hello 
Hello, Go examples!


Alternatively, the snap command allows you to select a Go version to be installed on you Ubuntu system. The below command will list all available Go versions:

$ snap info go
name:      go
summary:   Go programming language compiler, linker, stdlib
publisher: mwhudson
contact:   michael.hudson@ubuntu.com
license:   BSD-3-Clause
description: |
  This snap provides an assembler, compiler, linker, and compiled libraries for
  the Go programming language.
snap-id:          Md1HBASHzP4i0bniScAjXGnOII9cEK6e
channels:                              
  stable:         1.10          (1473) 64MB classic
  candidate:      1.10rc2       (1406) 64MB classic
  beta:           ↑                         
  edge:           devel-86a3389 (1598) 69MB classic
  1.10/stable:    1.10          (1473) 64MB classic
  1.10/candidate: ↑                         
  1.10/beta:      ↑                         
  1.10/edge:      ↑                         
  1.6/stable:     1.6.4         (122)  49MB classic
  1.6/candidate:  ↑                         
  1.6/beta:       ↑                         
  1.6/edge:       ↑                         
  1.7/stable:     1.7.6         (324)  48MB classic
  1.7/candidate:  ↑                         
  1.7/beta:       ↑                         
  1.7/edge:       ↑                         
  1.8/stable:     1.8.7         (1407) 51MB classic
  1.8/candidate:  ↑                         
  1.8/beta:       ↑                         
  1.8/edge:       ↑                         
  1.9/stable:     1.9.4         (1404) 55MB classic
  1.9/candidate:  ↑                         
  1.9/beta:       ↑                         
  1.9/edge:       ↑  

For example, the bellow command will install Go 1.7 stable:

$  sudo snap install go --channel 1.7 --classic                                                                                                                                                            
go (1.7/stable) 1.7.6 from 'mwhudson' installed
$ go version                                                                                                                                                          
go version go1.7.6 linux/amd64 


Getting Started Go Example

Let’s start a new project by creating a new directory within ~/go/src/. We will call it the linuxconfig project:

$ mkdir ~/go/src/linuxconfig

Using nano or any other text editor edit the new file linuxconfig.go:

$ nano ~/go/src/linuxconfig/linuxconfig.go

While in the editing mode, enter the following Go code:

package main

import (
        "fmt"
        "github.com/golang/example/stringutil"
)

func main() {
        fmt.Println(stringutil.Reverse("gro.gifnoCxuniL gnitisiv rof uoY knahT"))
}

Compile and install new binary:

$ go install linuxconfig

Run Golang binary executable:

$ linuxconfig 
Thank You for visiting LinuxConfig.org