Maintain multiple versions of Go in one single environment

  sonic0002        2019-11-10 00:43:47       8,469        0    

In a development environment, there might be multiple projects going on at the same time and they may require different development environments with different versions of build tool. In many programming languages, it is possible to have multiple versions of different build tool or development tool on a single environment. For example, there can be multiple JDKs, multiple versions of Ruby using RVM. For GoLang, there is a similar tool called GVM which can also be used to maintain multiple versions of Go in one single environment.

In this post, we provide a mini guide on how to setup multiple versions of Go using GVM.

The first thing needs to be done is to install GVM on the environment.

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

This will download the GVM installer and install it on local environment. After the installation, it would remind you to run source command so that gvm command  is available right away otherwise the command would not be found.

source /home/pike/.gvm/scripts/gvm

Post that, GVM is installed successfully and try to list all available Go versions on the environment. You should expect to see a list of Go installations available. But surprisingly, you might see some errors like below which asks you to install the missing binary tools like make, gcc, bison.

pike@DESKTOP-G352RBR:~$ gvm listall

Could not find binutils

  linux: apt-get install binutils

Could not find bison

  linux: apt-get install bison

Could not find gcc

  linux: apt-get install gcc

Could not find make

  linux: apt-get install make

ERROR: Missing requirements.

Follow the instructions and install the missing tools one by one. Thereafter try to run gvm listall again. You should see no error but a list of Go installations available to be installed.

pike@DESKTOP-G352RBR:~$ gvm listall

gvm gos (available)

   go1
   go1.0.1
   go1.0.2
   go1.0.3
   go1.1
   go1.1rc2
   go1.1rc3
   go1.1.1
   go1.1.2
   go1.2
   go1.2rc2
   go1.2rc3
   go1.2rc4
   go1.2rc5
   go1.2.1
   go1.2.2
   go1.3
   go1.3beta1
   go1.3beta2
   go1.3rc1
   go1.3rc2
   go1.3.1
   go1.3.2
   go1.3.3
   go1.4
   go1.4beta1
   go1.4rc1

Choose one version you want to install and run command gvm install to install it.

pike@DESKTOP-G352RBR:~$ gvm install go1.13.4
Downloading Go source...
Installing go1.13.4...
 * Compiling...
/home/pike/.gvm/scripts/install: line 84: go: command not found
ERROR: Failed to compile. Check the logs at /home/pike/.gvm/logs/go-go1.13.4-compile.log
ERROR: Failed to use installed version

Unfortunately you may see an error again saying cannot compile the go build as go command is not found. The reason for this error is because you may not have a Go installed on your environment yet. Go and install a Go first without using GVM.

pike@DESKTOP-G352RBR:~$ sudo apt install golang-go
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
  libfreetype6
Use 'sudo apt autoremove' to remove it.

Once a Go is installed, run gvm install go1.12.13 again. This time the installation would succeed. To check the installed go versions, run gvm list.

pike@DESKTOP-G352RBR:~$ gvm list

gvm gos (installed)

   go1.12.13
   go1.13.4

To use a specific go version for a project, run gvm use [version].

pike@DESKTOP-G352RBR:~$ gvm use go1.12.13
Now using version go1.12.13
pike@DESKTOP-G352RBR:~$ gvm list

gvm gos (installed)

=> go1.12.13
   go1.13.4

The => indicates the current version of Go being used. When switching the Go version being used, the environment variable $GOPATH is also being changed. The installation path of go get packages is $GOPATH which means each different Go version would have its own set of go packages which makes multiple versions of different go package possible in a single environment.

pike@DESKTOP-G352RBR:~$ echo $GOPATH
/home/pike/.gvm/pkgsets/go1.12.13/global
pike@DESKTOP-G352RBR:~$ gvm use go1.13.4
Now using version go1.13.4
pike@DESKTOP-G352RBR:~$ echo $GOPATH
/home/pike/.gvm/pkgsets/go1.13.4/global

Furthermore, there is a subcommand gvm pkgset which can even make the package management more robust even when using same version of Go but for different projects.For detailed commands about this, can refer to gvm GitHub repo.

RVM  GVM PKGSET  GVM  GOLANG 

       

  RELATED


  0 COMMENT


No comment for this article.



  RANDOM FUN

Programmer after a hard day