How to Use Goenv

Jaesang Kim
4 min readSep 22, 2024

--

The Go language is loved by many developers for its performance and efficiency. However, when working on Go projects, you may find the need to manage different versions of Go. A useful tool in such situations is goenv. In this post, we will go over how to install and use goenv.

What is goenv?

goenv is a tool for managing different versions of the Go language, allowing you to easily use different Go versions across multiple projects. It works similarly to rbenv for Ruby, providing functionality for setting Go versions for each project.

Installing goenv via Homebrew

Since I’m a macOS user, I’ll be explaining how to install goenv using Homebrew. If you’re not using Homebrew or if you’re on Linux, you can find installation instructions on the official goenv site.

1. Check if Homebrew is installed

First, ensure that Homebrew is installed by opening a terminal and entering the following command:

brew --version

If Homebrew is not installed, follow the instructions on the Homebrew official website to install it.

2. Install goenv

To install goenv using Homebrew, enter the following command:

brew install goenv

3. Set up environment variables

To use goenv, you need to set up environment variables. Add the following lines to your shell configuration file (e.g., ~/.bashrc, ~/.zshrc, etc.):

export GOENV_ROOT="$HOME/.goenv"
export PATH="$GOENV_ROOT/bin:$PATH"
eval "$(goenv init -)"

To apply the changes, either restart your shell or run the following command:

source ~/.bashrc # or source ~/.zshrc

4. Verify goenv installation

To verify that goenv is installed correctly, run the following command:

goenv --version

If installed properly, the version of goenv will be displayed.

How to Use goenv

1. Installing Go versions

You can install a specific version of Go using goenv by entering the following command:

goenv install 1.22.7 # Example: Installing version 1.22.7

To view the list of available Go versions, use the following command:

goenv install -l

2. Setting Go versions

To apply a specific Go version to your project, use the following commands:

  • Set the global version: This sets the default Go version for all projects.
goenv global 1.22.7
  • Set the local version: This applies a Go version only to the current directory (project).
goenv local 1.22.7

3. Checking the current Go version

To check the currently used version of Go, use the following command:

go version

You can also check it via goenv:

goenv version

4. Removing a Go version

To remove a version of Go that is no longer needed, run the following command:

goenv uninstall 1.22.7

How goenv local Works

When you set a specific Go version using the `goenv local` command, that version is only applied to the current directory (project). Let’s explain how this works in more detail.

1. Setting a local version:

When you use the command goenv local <version>, a file named .go-version is created in the current directory. This file contains the specified Go version. For example, if you run the following command:

goenv local 1.23.0

A .go-version file will be created in the current directory, and it will contain the version 1.23.0.

2. Checking the Go version:

To check the Go version in the current directory, use the following command:

go version

This command sets the Go runtime environment based on the .go-version file in the current directory. Therefore, when you run Go in this directory, version 1.23.0 will be used.

3. Behavior in subdirectories:

If you run Go in a subdirectory, it will reference the .go-version file from the parent directory. Thus, the same Go version will be used in the subdirectory. If you want to set a different Go version in the subdirectory, run the goenv local <version> command again in the subdirectory.

4. Relationship with global versions:

There is also a global Go version that you set using the goenv global <version> command. If there is no .go-version file in the current directory, the global version will be used. However, if a .go-version file exists, the local version specified in that file will take precedence.

Example

1. Setting a Go version:

cd my-go-project
goenv local 1.23.0

2. Checking the Go version:

go version

The output will display go version go1.23.0 darwin/amd64, showing the set version.

3. Checking in a subdirectory:

If you move to a subdirectory, the same version will still be used.

cd my-go-project/subdir
go version

It will still show go1.23.0.

4. Setting a different version:

To set a different Go version in a subdirectory:

goenv local 1.23.1

Now, running go version in the subdirectory will show go1.23.1.

Conclusion

goenv is a powerful tool that makes it easy to manage go versions. it is especially useful for developers who need different go versions for different projects. with this guide, you should now be able to install and use goenv to efficiently manage your go development environment. happy coding!

--

--