Cliff
Cliff

Reputation: 11248

Multiple versions of go

I am trying to learn go-lng following the official docs here: https://golang.org/doc/install

I am stuck on the step installing extra go versions. Apparently this line should install a different version of go and make the executable available in my $PATH but it's not happening:

go get golang.org/dl/go1.10.7

Instead what I see is:

c.craig$ go get golang.org/dl/go1.10.7
c.craig$ go1.10.7 download
-bash: go1.10.7: command not found

Where am I going wrong? I've tried it with a space assuming this was just a typo in the docs but even that doesn't work:

c.craig$ go get golang.org/dl/go1.10.7
c.craig$ go 1.10.7 download
go 1.10.7: unknown command

Upvotes: 30

Views: 56618

Answers (12)

Sergey Bushmanov
Sergey Bushmanov

Reputation: 25199

You may wish to try Go Version Manager (gvm):

curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer | bash
gvm listall
gvm install go1.10.7 [-B] 
gvm use go1.10.7 [--default]
gvm list

In case you want to compile from source and don't have go installed:

gvm install go1.4 -B
gvm use go1.4
#CGO_ENABLED=0
export GOROOT_BOOTSTRAP=$GOROOT
gvm install go1.10.7
gvm use go1.10.7

In case you're getting compiling errors, you may wish to uncomment CGO_ENABLED=0 prior to compiling from the source.

And you can always see what's available:

gvm list

or rely on gvm

gvm install latest

Upvotes: 20

kolypto
kolypto

Reputation: 35414

In Ubuntu, the packages are surprisingly fresh:

$ sudo apt install golang-1.23-go
$ sudo update-alternatives --install /usr/bin/go go /usr/lib/go-1.23/bin/go 20

Upvotes: 0

rgoel
rgoel

Reputation: 540

After setting the following path variables in .bash_profile (do not forget to run: source ~/.bash_profile)

$ export GOPATH=$HOME/go
$ export GOBIN=$HOME/go/bin

Install go version in one terminal:

$ go install golang.org/dl/go1.13.15@latest
$ go1.13.15 download

Now you also want to install another go version say go1.15.13 by executing the below commands (remember to set the path as told above)

$ go install golang.org/dl/go1.15.13@latest
$ go1.15.13 download

Now you have two go versions installed; 1.13.15 and 1.15.13.

Say, in one terminal, you want to use version 1.13.15 so you can create an alias as below in that terminal window:

$ alias go="go1.13.15"
$ go version
go version go1.13.15 darwin/amd64

In another terminal you can switch to different version

$ alias go="go1.15.13"
$ go version
go version go1.15.13 darwin/amd64

Upvotes: 44

Stephen Jiang
Stephen Jiang

Reputation: 1

what about goup-rs, it wirte in rust, single binary but only depend git.

$ goup install
[2024-01-30T00:38:48Z INFO ] Installing go1.21.6 ...
[2024-01-30T00:38:48Z INFO ] Unpacking /home/thinkgo/.goup/go1.21.6/go1.21.6.linux-amd64.tar.gz ...
[2024-01-30T00:38:48Z INFO ] go1.21.6 installed in /home/thinkgo/.goup/go1.21.6
[2024-01-30T00:38:48Z INFO ] Default Go is set to 'go1.21.6'
$ goup list
| VERSION | ACTIVE |
|---------|--------|
| 1.21.6  |   *    |
$ go env GOROOT
/home/thinkgo/.goup/current
$ go version
go version go1.21.6 linux/amd64
$ GOUP_GO_HOST=https://golang.google.cn goup install 1.21.6

Upvotes: 0

dongocanh96
dongocanh96

Reputation: 129

I faced the same issue when trying to install version 1.17.11, my base version is 1.18.2.

Followed instructions from here https://go.dev/doc/manage-install but it didn't work for me. Then, i realized, after ran go install golang.org/dl/go<version>@latest (where <version> is the version you need), a binary file name go<version> will be downloaded in $HOME/go/bin/.

Move to $HOME/go/bin/, run ./go<version> download, and version you need will be downloaded at $HOME/sdk/

To use the version you had just downloaded, add this line to the end of ~/.bashrc file:

alias go<version>=$HOME/sdk/go<version>/bin/go

Save and exit. Run source ~/.bashrc to execute it!

Upvotes: 10

Vasileios Pallas
Vasileios Pallas

Reputation: 4877

I know I am quite late to the party, but being really fed up with how multiple versions should be managed in Go, I decided to build a simple CLI that does the trick easily.

https://github.com/VassilisPallas/gvs/

I released it a couple of days ago, which means there are a few things that need to be added as features. But feel free to reach out to me if you see any required improvements/new features.

Upvotes: 0

hide1nbush
hide1nbush

Reputation: 935

I recommend https://github.com/stefanmaric/g as an alternative of gvm in the field of go version manager.

Upvotes: 0

Ma Hansen
Ma Hansen

Reputation: 31

Referring to the link: https://go.dev/doc/manage-install, step by step. And if come up with this error:

$ c.craig$ go get golang.org/dl/go1.10.7
$ c.craig$ go1.10.7 download
-bash: go1.10.7: command not found

try to add these codes to your shell profile(like.zshrc):

export PATH=$PATH:/usr/local/go/bin
export PATH=$PATH:$(go env GOPATH)/bin

the first path is where your base go installed, mostly is /usr/local/go/bin. And then the error would be fixed.

Upvotes: 3

thweekkomputer
thweekkomputer

Reputation: 1

I got the same problem as yours, after I knew that go is back-compatible, I copy the /usr/local/go/bin/go to /usr/local/go/bin/go{version}.

Upvotes: 0

workaround
workaround

Reputation: 528

I also found useful using this tool to switch between versions: Freshgo - https://nikfot.github.io/freshgo/ It is a go binary and easy to install (linux).

I installs go versions from the source easily and can keep a backup of your go source files so if you switch between versions you save some little time from go pkg downloads.

Easy as that:

freshgo select -v 1.19

I also installed it in my shell to check for latest versions, with a ready script:

echo "${PATH to freshgo repo}/check_ver.sh" >>  ~/.profile

Upvotes: 0

David I. Rock
David I. Rock

Reputation: 125

For Mac users it's easier and I guess for Linux users should be similar, you just need to:

  1. use a modern version of GO i.e. >= 1.12
  2. make sure you can run go version
  3. browse the version you need: https://go.dev/dl/ lets say you want to download go1.13.15 you can run the following command(I think the @latest is optional): go install golang.org/dl/go1.13.15@latest, if nothing happens then everything is ok.
  4. beacuse you need to update your PATH you can either do it or open a new terminal.
  5. run the command: go1.13.15 download
  6. Now you can find a new directory in $HOME/sdk/go1.13.15 thats where your new instalation of GO resides.
  7. update your GOROOT env var to work with your workspace with the following command: export GOROOT=$HOME/sdk/go1.13.15
  8. Finally type go version you should see
go version go1.13.15 darwin/amd64

Upvotes: -4

Paul Hankin
Paul Hankin

Reputation: 58271

The binary is installed $HOME/go/bin (or more accurately the bin directory under the path you get from go env GOPATH). The go get command doesn't update your $PATH, so you need to add the install directory to your $PATH yourself.

Upvotes: 18

Related Questions