When you want to switch go versions for each project, gvm (Go Version Manager) is useful.
Here we will show you “how to install gvm on your Mac” and “how to use gvm”.
Preparation for Use
Preparation for Mac only
Execute the following command as described in the MacOSX requirements.
xcode-select --install
brew update
brew install mercurial
Installation of gvm
We use zsh for the shell.
$ echo $SHELL
/bin/zsh
For zsh, you can install as follows.
$ zsh < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
Cloning from https://github.com/moovweb/gvm.git to /Users/user_name/.gvm
No existing Go versions detected
Installed GVM v1.0.22
Please restart your terminal session or to get started right away run
`source /Users/user_name/.gvm/scripts/gvm`
The following line was added to the last line of ~/.zshrc
[[ -s "/Users/user_name/.gvm/scripts/gvm" ]] && source "/Users/user_name/.gvm/scripts/gvm"
Restart the terminal and the gvm command will be available.
$ gvm help
Usage: gvm [command]
Description:
GVM is the Go Version Manager
Commands:
version - print the gvm version number
get - gets the latest code (for debugging)
use - select a go version to use (--default to set permanently)
diff - view changes to Go root
help - display this usage text
implode - completely remove gvm
install - install go versions
uninstall - uninstall go versions
cross - install go cross compilers
linkthis - link this directory into GOPATH
list - list installed go versions
listall - list available versions
alias - manage go version aliases
pkgset - manage go packages sets
pkgenv - edit the environment for a package set
How to use gvm
Check the version that can be installed
You can use gvm listall
to see which versions are available for installation.
$ gvm listall
gvm gos (available)
go1
go1.0.1
go1.0.2
go1.0.3
go1.1
go1.1rc2
go1.1rc3
:
Install go by version specification
I am using M1 Mac, and when I executed the following command, it resulted in an ERROR.
$ gvm install go1.16.15 -B
Installing go1.16.15 from binary source
ERROR: Binary Go unavailable for this platform
$
$ gvm install go1.17.5 -B
Installing go1.17.5 from binary source
ERROR: Binary Go unavailable for this platform
I did the following with reference to this page and was successful.
$ brew install go
==> Downloading https://ghcr.io/v2/homebrew/core/go/manifests/1.18
######################################################################## 100.0%
:
$
$ gvm install go1.16.15
Installing go1.16.15...
* Compiling...
go1.16.15 successfully installed!
$
$ gvm use go1.16.15 --default
Now using version go1.16.15
$
$ go version
go version go1.16.15 darwin/arm64
I was then able to uninstall go installed with brew and still install another version of go.
$ brew uninstall go
Uninstalling /opt/homebrew/Cellar/go/1.18... (11,947 files, 595.3MB)
$
$ gvm install go1.17.5
Installing go1.17.5...
* Compiling...
go1.17.5 successfully installed!
I installed 1.16.15
and 1.17.5
, but folders were generated for each version as follows.
$ ls ~/.gvm/gos
go1.16.15 go1.17.5
Switch the version of go to use
We are currently using go1.16.15
.
$ gvm list
gvm gos (installed)
=> go1.16.15
go1.17.5
$
$ echo $GOPATH
/Users/user_name/.gvm/pkgsets/go1.16.15/global
$
$ echo $GOROOT
/Users/user_name/.gvm/gos/go1.16.15
$
$ go version
go version go1.16.15 darwin/arm64
$
$ which go
/Users/user_name/.gvm/gos/go1.16.15/bin/go
Switch to 1.17.5
.
$ gvm use go1.17.5 --default
Now using version go1.17.5
$
$ gvm list
gvm gos (installed)
go1.16.15
=> go1.17.5
$
$ echo $GOPATH
/Users/user_name/.gvm/pkgsets/go1.17.5/global
$
$ echo $GOROOT
/Users/user_name/.gvm/gos/go1.17.5
$
$ go version
go version go1.17.5 darwin/arm64
$
$ which go
/Users/user_name/.gvm/gos/go1.17.5/bin/go