[Golang] Use goenv to manage various versions of Golang

[Golang] Use goenv to manage various versions of Golang

2023-04-14 hit count image

let's see how to use goenv to manage various versions of Golang in the same machine.

Outline

When you use Golang to develop the projects, you need to use various versions of Golang on one machine(PC) for them.

In this blog post, I will introduce how to install goenv and how to use it for managing various versions of Golang in the same machine(PC).

goenv

goenv is a Go Version Management like the pyenv for Python, rben for Ruby to manage various versions for Golang in one machine.

In this blog post, I will show you how to install govenv and how to use it on macOS. If you want to know how to install Golang on macOS, see the following link.

Install goenv

Execute the following command to install goenv on macOS.

brew install goenv

After installing, execute the following command.

goenv --version

If there is no problem, you can see the version of goenv like the following.

goenv 2.0.6

Configure goenv

After installing, to use goenv with any problems, open the .zshrc file and modify it like the following.

...
# go
export GOROOT="$(brew --prefix golang)/libexec"
export GOENV_ROOT="$HOME/.goenv"
export PATH="$GOENV_ROOT/bin:$PATH"
eval "$(goenv init -)"
export PATH="$GOROOT/bin:$PATH"
export PATH="$PATH:$GOPATH/bin"
alias go=~/.goenv/shims/go

goenv version

When you execute the following command, you can see the Golang version which is configured by goenv.

goenv version

After executing, you can see the version of Golang configured by goenv like the following.

system (set by /Users/dev-yakuza/.goenv/version)

goenv versions

When you execute the following command, you can see all versions of Golang installed by goenv in the machine, and also you can see a version which is currently activated.

goenv versions

And then, you can see the installed Golang versions and configured Golang version by goenv.

* system (set by /Users/dev-yakuza/.goenv/version)

Now, we didn’t install other versions of Golang with goenv, so you can see only default version(system) is shown and it is selected.

goenv install –list

When you execute the following command, you can see the installable versions of Golang.

goenv install --list

After executing, you can see the versions of Golang like the following.

...
1.19.5
1.19.6
1.20.0
1.20rc1
1.20rc2
1.20.1

goenv install

Now, let’s try to install another Golang version, that you want, by using goenv. In this blog post, I will install the 1.20.1 version. To install a new version of Golang, execute the following command.

goenv install 1.20.1

And then, you can see the new Golang version is installed like the following.

Downloading go1.20.1.darwin-amd64.tar.gz...
-> go1.20.1.darwin-amd64.tar.gz
######################################################################################################################## 100.0%
Installing Go Darwin 64bit 1.20.1...
Installed Go Darwin 64bit 1.20.1 to /Users/dev-yakuza/.goenv/versions/1.20.1

After then, execute the following command to check the new version is installed well.

goenv versions

And then, you can see the 1.20.1 version of Golang is installed like the following.

* system (set by /Users/dev-yakuza/.goenv/version)
  1.20.1

goenv rehash

rehash is a command to make goenv recognize the installed version of Golang. So, when you install a new version of Golang, you need to execute the rehash command like the following.

goenv rehash

goenv global

Now, let’s see how to use the newly installed 1.20.1 version of Golang globally. Execute the following command to use the new Golang version for default.

goenv global 1.20.1

When you execute the following command, you can see the new version of Golang is configured well.

go versions

And then, you can see the new Golang version is configured well like the following.

  system
* 1.20.1 (set by /Users/dev-yakuza/.goenv/version)

Also, you can execute the following command to check the current Golang version.

goenv version

And then, you can see the Golang version like the following.

1.20.1 (set by /Users/dev-yakuza/.goenv/version)

Of course, you can check the Golang version with the basic command like the following.

go version

And then, you can see the Golang version like the following.

go version go1.20.1 darwin/amd64

The goenv global command sets the version for default. So, even if you terminate and reboot the terminal, the 1.20.1 version of Golang will be used.

goenv shell

The goenv shell command makes you set the specific version of Golang for the current session of the terminal only.

To check the differences of goenv global, execute the following command to install another version of Golang.

goenv install 1.19.6

After installing, execute the following command to set the version for the current session of the terminal.

goenv shell 1.19.6

And then, execute the following command to check the Golang version is changed well.

goenv versions

After it, you can see the Golang version is changed well like the following.

  system
* 1.19.6 (set by GOENV_VERSION environment variable)
  1.20.1

Also, you can execute the following command to check the current version of Golang.

goenv version

And then, you can see the Golang version like the following.

1.19.6 (set by GOENV_VERSION environment variable)

Of course, you can check the Golang version with the basic command.

go version

And then, you can see the Golang version is changed well like the following.

go version go1.19.6 darwin/amd64

When the session of the terminal is terminated, the version configured by goenv shell will be initialized and the default version of Golang will be reconfigured. In other words, when you terminate the terminal and open it again, the version configured by goenv global is activated.

goenv local

When you execute the goenv local command, the project (folder) in which you run the command will always use that version. You can use the goenv local command like the following.

goenv local 1.19.6

After executing, you can see the .go-version file is created and the 1.19.6 version is written in it.

Also, when you execute the following commands, you can see the Golang version is changed well to 1.19.6.

goenv versions
goenv version
go version

If you open the terminal in the folder which has the .go-version file, goenv automatically recognizes the version in the file and selects the version of Golang.

.go-version

As I mentioned in the goenv local section, you can manage the Golang versions for each project by using the .go-version file. So, if you work on the Golang project with the team members, it’s helpful to create the .go-version file and write the version on it.

There may be cases where the version recorded in the .go-version file is not installed on the current machine. To check this, modify the .go-version file as follows.

1.18.10

And then, terminate the terminal and reopen it, you can see the error message like the following.

goenv: version '1.18.10' is not installed (set by /Users/dev-yakuza/projects/temp/test/.go-version)

The error occurs because the Golang version recorded in the .go-version file has not yet been installed.

At this case, execute the following command to install the version recorded in the .go-version file.

goenv install

And then, you can see the Golang version recorded in the .go-version file is installed like the following.

Downloading go1.18.10.darwin-amd64.tar.gz...
-> go1.18.10.darwin-amd64.tar.gz
####################################################################################################################### 100.0%####################################################################################################################### 100.0%
Installing Go Darwin 64bit 1.18.10...
Installed Go Darwin 64bit 1.18.10 to /Users/dev-yakuza/.goenv/versions/1.18.10

Also, you can check the Golang version is configured well by running the following commands.

goenv versions
goenv version
go version

This ensures that the members who develop the project together use the same version of Golang.

Completed

Done! We’ve seen how to install goenv and how to use goenv to manage various versions of Golang for each project on one machine. If you work several projects with different versions fo Golang at the same time, try using goenv to manage the version of Golang.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts