Install multiple versions of Golang on Ubuntu

The Go language, better known as Golang, is a programming language that’s gaining a lot of popularity lately. My friend Brayan Bautista showed me his interest in this language, which ignited my curiosity. As well-known innovative projects such as Docker or Kubernetes use this language, I felt it would be useful for developing my career in DevOps engineering. That’s one of the reasons why I use Hugo for this blog.

If you want to try it, there are different ways to install Golang in your GNU/Linux distribution. Some managers allow you to install several versions of the development tools at the same time and also do so in the user’s space. For example, nvm for node.js or Sdkman! for Java, including tools such as Maven or Gradle and languages such as Groovy or Kotlin.

The version manager for Golang is called Gvm, and today it will be used by us to install Golang through it. I am doing it in KDE Neon 5, a GNU/Linux distribution based on Ubuntu 18.04 LTS. The first step is to install the necessary dependencies.

$ sudo apt-get install curl git mercurial make binutils bison gcc build-essential

Once the required dependencies are ready, gvm must be installed:

$ bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)

To use gvm, you have to restart your terminal or to load the environment configuration with the command below:

$ source ~/.gvm/scripts/gvm

Now you can check that gvm is correctly installed.

$ gvm

Before trying to install a version, have in mind that by default, gvm installs the different Golang versions by downloading the source code and compiling it. To do this at first it used the C compiler, but since version 1.5 of Golang, this compiler was replaced by another one written in Go. So now it’s impossible to compile version 1.5 or higher without having Golang installed in the system. How will we face this problem? That’s simple. Just make a binary installation of version 1.4. After that you will be able to compile and install the latest versions.

So let’s install Golang version 1.4 and use it for the current shell session.

$ gvm install go1.4 -B
$ gvm use go1.4
$ export GOROOT_BOOTSTRAP=$GOROOT

Check that Go 1.4 is correctly installed and set:

$ go version
go version go1.4 linux/amd64

As everything is ready to compile and install Go, list all the available versions:

$ gvm listall

Latest stable version is 1.14 right now, so let’s install it and set as the default one:

$ gvm install go1.14
$ gvm use go1.14 --default

Buccaneer, gvm allows you to have several versions of Golang installed on your system at the same time. You already know how to list available versions and how to install them. You can use the command gvm list to list all the installed Golang versions and gvm use go<version> to select the version to use in the current shell. If you want to set the version as default to always use that as default, add the argument --default as we did in the command above. Good winds!