Installing Go featured image

Installing Go on Ubuntu 20.04

Go is an open-source programming language. Originally designed at Google, Go shares syntax similarities with C. However, it includes additional programming features like structural typing, garbage collection, memory safety, and CSP-style concurrency. Most of the time, the Go programming language is referred to as “Golang” because of the official Go domain name.

This guide demonstrates installing and configuring the latest version of Go on Ubuntu.

Go on Ubuntu

Just like installing any other package, it requires that you have root access or a non-root user with sudo privilege. All the demonstrations were performed on a standard Ubuntu server. Here’s a quick guide on setting up your Ubuntu server. There are multiple methods we can apply to install Go on Ubuntu. We can install Go directly from the Ubuntu package servers. Alternatively, Go is also available from the Snapcraft store.

We can manually configure Go without having to use any package manager. However, this method comes with inconveniences like complex installation methods, manual package management, etc. CloudSigma offers API for Go programming for easier cloud management.

  • Installing Go from the Ubuntu package server

This is the simplest way of installing Go on Ubuntu. It can directly be installed using the APT package manager. It will also automatically keep the package up-to-date. If not needed, it’s easier to uninstall Go.

There are two types of Go available on Ubuntu – golang-go and gccgo-go. The gccgo-go is the GCC implementation of the Go language whereas golang-go is the original implementation by Google. Both come with their own perks. In this guide, we’ll focus on golang-go, the original Go compiler. First, run the following command to install golang-go:

Installing Go 1

If you want to install gccgo-go, then run the following command instead. Note that installing both golang-go and gccgo-go isn’t possible, because they conflict with each other. Golang has an official documentation page on gccgo.

Installing Go 2

  • Installing Go snap

Snaps are universal Linux packages that can function on any Linux distro. Go is available as snap for all Linux distros. You can check out Go on Snapcraft:

Installing Go 3Installing snap requires having snapd installed. It’s the snap daemon (along with snappy, the snap package manager) required to install and manage snap packages. Ubuntu comes with snapd installed by default. If it’s not installed, then follow the official snap installation guide on Ubuntu. The following snap command will install Go from Snapcraft:

sudo snap install

  • Installing Go manually

As mentioned before, this process is a bit messy. You have to manually manage the Go package. First, we need to download the Go binary package. The Go download page lists all the available binary packages. At the time of writing this guide, the latest stable Go package is go1.16.5.linux-amd64.tar.gz:

wget

Once the download is complete, we will extract the archive at /usr/local/go. It will remove any previous installations of Go at the location:

sudo rm

Next, we need to update the PATH environment variable to include the Go binary path. The system uses the PATH variable to find binaries:

export

The PATH changes will only last for the current shell session. To make a permanent change, you need to declare the new value of PATH in either ~/.profile (for the current user only) or /etc/profile (for all users in the system). Reload the file to take the changes into effect:

Verifying Installation

  • Go version

The installation of Go is now complete! Next, we need to verify that it was successful. Run the following Go command. It will print out the Go version:

go version

  • Sample program

The next process in verification is to create the classic hello world program. First, we need to create the workspace. Go uses the variable GOPATH that specifies the workspace location. By default, it’s set to the location $HOME/go. Create the workspace:

Within the workspace, create a new directory tree src/hello:

Next, open a new file within the directory hello.go:

After that, add the following code in the Go file:

Installing Go 4

Finally, navigate to the workspace directory and run the program:

go run hello.go

If this is your first time installing Go, then you should also check out the official starting guide on Golang.

Final Thoughts

Go is a powerful programming language. Many popular applications use Go, for example, Kubernetes, Dropbox, Openshift, InfluxDB, and more. Go has a simple language design, independent of platform, and comes with a powerful standard library. With the help of this guide, you are now ready to start your journey with Go programming.

Happy Computing!