How to Upgrade Go on Ubuntu

I needed to upgrade Go recently on an Ubuntu server I was using to do some Packer builds. The golang version that was installed on my system was 1.10.4. I needed to go to a later version so that I could use the mod subcommand, which isn’t present in version 1.10.

Go is a compiled, statically typed programming language developed by Google. Many modern applications such as Docker and Kubernetes are written in Go.

This article will cover the steps I took to update Go to the later version. First of all you can check the version of Go you have installed by running:

$ go version
go version go1.10.4 linux/amd64

This confirms that I am running go version 1.10.4. In order to update go, we first need to remove this version.

Note: As always, before proceeding with any steps listed in this article, ensure you have tested the process fully, and that you have a backup and restore plan in place – especially if you are working on a production system!

Remove the Existing Go Version

The golang-go package can be removed by running the following series of commands:

$ sudo apt-get remove golang-go

The following packages were automatically installed and are no longer required:
   golang-1.10-go golang-1.10-race-detector-runtime golang-1.10-src golang-race-detector-runtime golang-src pkg-config
 Use 'sudo apt autoremove' to remove them.
 The following packages will be REMOVED:
   golang-go
 0 upgraded, 0 newly installed, 1 to remove and 13 not upgraded.
 After this operation, 52.2 kB disk space will be freed.
 Do you want to continue? [Y/n] Y
$ sudo apt-get remove --auto-remove golang-go
$ sudo rm -rvf /usr/local/go 

These commands will remove the existing version of Go and it’s dependencies. Once done we can install the new version.

Install Go on Ubuntu

First of all we need to grab the correct install package for our system. These can be found here. I have chosen to use the latest version of Go, which at the time of writing is 1.15.5:

The package can be downloaded easily using wget:

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

Once the file is downloaded, the contents need to be extracted using unzip:

sudo tar -xvf go1.15.5.linux-amd64.tar.gz

Next we want to move the extracted directory and it’s contents to /usr/local, which is the recommended location:

sudo mv go /usr/local

With that done, the next step is to set up some environment variables so that we can run Go easily. To do so, edit the .profile file, which can be found in the home directory. I used vim to do so:

$ vim ~/.profile

The following lines need to be added:

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

Save the file once the changes have been made, then load these settings into the shell by running:

source ~/.profile

With that done, run go to check the version is as expected:

$ go version
go version go1.15.5 linux/amd64

As you can see from the output, the desired version of Go is now installed. We can further test the go upgrade is working as expected by running a simple ‘hello world’ program.

Testing the Go Upgrade

Using your favourite text editor, create a new file named hello.go, which contains the following lines:

package main
import "fmt"
func main() {
    fmt.Println("Hello, World! Just testing my Upgraded Go!")
}

Save the file, then run it using the go run command:

$ go run hello.go
Hello, World! Just testing my Upgraded Go!

There we go! An upgraded and working Go on Ubuntu!

Related posts

Mastering the Linux ifconfig Command

Docker Exec Command With Practical Examples

Debugging with Git Bisect

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More