Skip to content

Managing golang versions

James Won edited this page Feb 24, 2021 · 6 revisions

Install system golang:

$ sudo yum install golang-bin In my instance on a RHEL7 machine, it installed go 1.13.6. This gives you something to start with.

Install any other version you want using go get

I want to use go 1.13.10. I can achieve like so:

$ go get golang.org/dl/go1.13.10
$ go1.13.10 download

At this point, go 1.13.10 should be installed in ~/sdk/go1.13.10

Use update-alternatives to switch where the go binary points to

$ sudo update-alternatives --display go
go - status is auto.
 link currently points to /usr/lib/golang/bin/go
/usr/lib/golang/bin/go - priority 90
 slave gofmt: /usr/lib/golang/bin/gofmt
Current `best' version is /usr/lib/golang/bin/go.

Now add your new golang installation as an alternative:

$ sudo update-alternatives --install /usr/bin/go go ~/sdk/go1.13.10/bin/go 1 --slave /usr/bin/gofmt gofmt ~/sdk/go1.13.10/bin/gofmt

Now --display should list both installations:

$ sudo update-alternatives --display go
go - status is auto.
 link currently points to /usr/lib/golang/bin/go
/usr/lib/golang/bin/go - priority 90
 slave gofmt: /usr/lib/golang/bin/gofmt
/home/jwon/sdk/go1.13.10/bin/go - priority 1
 slave gofmt: /home/jwon/sdk/go1.13.10/bin/gofmt
Current `best' version is /usr/lib/golang/bin/go.

Make the change to use the new installation via --config:

$ sudo update-alternatives --config go
There are 2 programs which provide 'go'.
  Selection    Command
-----------------------------------------------
*+ 1           /usr/lib/golang/bin/go
   2           /home/jwon/sdk/go1.13.10/bin/go
Enter to keep the current selection[+], or type selection number: 2

Now --display should say that we're pointing to our new installation:

$ sudo update-alternatives --display go
go - status is manual.
 link currently points to /home/jwon/sdk/go1.13.10/bin/go/usr/lib/golang/bin/go - priority 90
 slave gofmt: /usr/lib/golang/bin/gofmt
/home/jwon/sdk/go1.13.10/bin/go - priority 1
 slave gofmt: /home/jwon/sdk/go1.13.10/bin/gofmt
Current `best' version is /usr/lib/golang/bin/go.

Validate

At this point, go now points to our 1.13.10 installation!

$ go version
go version go1.13.10 linux/amd64