Install and use multiple versions of go?


1. Demand background

In the actual development, we will encounter different open source projects, which may be developed by different teams and use different go versions.

Taking me as an example, I recently studied kubevirt (an open source project that can manage virtual machines under k8s). In order to meet my own needs, I need to carry out secondary development of kubevirt.

Via go Mod learned that kubevirt was developed using go 1.13. At the same time, in order to use LVM as a storage method for virtual functions under kubevirt, we introduced the LVM CSI plug-in developed by Ali. Similarly, the functions realized by this plug-in are still a little behind our expectations, so we also need to carry out secondary development. View go Mod found that LVM CSI was developed under an older version, namely go 1.12.

At the same time, I installed go 1.14 on my machine long ago and have been using it for development.

The problem is, how can I install so many versions of golang on my machine without conflict?

2. Install multiple versions of go

If you want different versions of go not to conflict with each other, just install them in different directories.

First download the two versions of the installation package

$ wget
$ wget

Then extract them to different directories

#Unzip go 1.12
sudo tar -C /tmp/ -xzf go1.12.linux-amd64.tar.gz
mv /tmp/go /usr/local/go12 

#Unzip go 1.13
sudo tar -C /tmp/  -xzf go1.13.linux-amd64.tar.gz
mv /tmp/go /usr/local/go13

3. Simple and crude scheme

You should pay attention to installing multiple versions of golang on the same machine

Distinguish different versions of go entry

When you have multiple go in the environment, then when you usego buildHow does the system know which version you want to use when waiting for a command?

The system doesn’t know, so you need to give different names to different versions of go

  • The go entry of go 1.12 is changed to/usr/local/go12/bin/go12
  • The go entry of go 1.13 is changed to/usr/local/go13/bin/go13
  • Push with this column

In this way, there will be no confusion

$ go12 version
go version go1.12 linux/amd64
$ go13 version
go version go1.13 linux/amd64

Toggles the use of different environment variables

Different go entries are used, and the corresponding environment variables also need variables, such as goroot. It will not automatically switch with which version of go you use. See below, they output a value.

$ go12 env GOROOT
$ go13 env GOROOT

Therefore, you have to do this by yourself

Another thing to note is

  1. Go 1.12 nogo env -wMedium-wparameter
  2. Go 1.13-wHowever, the OS level environment variables cannot be overwritten. You need to unset them before modifying them

After knowing these points, you can realize the switching scheme by yourself.

I think the plan can be/usr/local/go13/bin/Write a script to modify environment variables

$ cat << EOF >/usr/local/go12/bin/goto12
unset GOROOT
go env -w GOROOT="/usr/local/go12/"

$ chmod +x /usr/local/go12/bin/goto12

The next time you want to use go 1.12, you can directly use the following command to modify the environment variables

$ source goto12

Go 1.13 is the same. I dare not repeat it.

The results are as follows

$ go env GOROOT

$ source goto12
$ go12 env GOROOT

$source goto13
$ go13 env GOROOT

4. Better scheme than above

At this point, you should have felt that you have to manually source every time you switch, which is still a little troublesome.

In fact, I have a better method. This scheme can avoid changing the name of the go entry, and also not even the source.

Just execute the following two conditional commands (two for each version, and four for two versions)

$ cat << EOF >/usr/local/go12/bin/go12
unset GOROOT
go env -w GOROOT="/usr/local/go12/"
/usr/local/go12/bin/go [email protected]

$ chmod + /usr/local/go12/bin/go12

$ cat << EOF >/usr/local/go13/bin/go13
unset GOROOT
go env -w GOROOT="/usr/local/go13/"
/usr/local/go13/bin/go [email protected]

$ chmod + /usr/local/go13/bin/go13

If so, when you execute go12, you will automatically modify the environment variables of go12, and when you execute go13, you will automatically modify the environment variables of go13 without affecting the default behavior of go.

Install and use multiple versions of go?

Of course, I only changed the environment variable goroot above. If there are other differences in different go versions, you can continue to add the corresponding shell code in go12 or go13.

Talk about it

I have written many Python related articles on segmentfault, including Python utilities, python efficiency skills and pycharm usage skills. I am glad to have been recognized and supported by many friends.

With their encouragement, I sorted the past articles into three PDF e-books

Pycharm Chinese guide

The pycharm Chinese guide uses more than 300 GIF dynamic diagrams to explain in detail the 105 efficient use skills of pycharm most suitable for actual development. The content is easy to understand and suitable for all Python developers.

Online experience address:

Install and use multiple versions of go?

Python dark magic guide

The python guide to dark magic is now in v3 0, which contains more than 100 development tips, is very suitable for fragment reading in leisure time.

Online experience address:

Install and use multiple versions of go?

Python Chinese guide

The best learning material for learning Python is always the official Python documents. Unfortunately, most of the official documents are in English. Although there is a Chinese translation, the progress is really worrying. In order to take care of students with poor English, I wrote an online Python document for zero foundation friends – Python Chinese guide

Online experience address:

Install and use multiple versions of go?

If it helps, remember to helpLike it~