Skip to content

Instantly share code, notes, and snippets.

@Rezwanul-Haque
Last active January 18, 2021 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rezwanul-Haque/4c533b048a8e628487439ecb1b933604 to your computer and use it in GitHub Desktop.
Save Rezwanul-Haque/4c533b048a8e628487439ecb1b933604 to your computer and use it in GitHub Desktop.
Installing Multiple Version of Golang Using GoEnv

Often we need a different version of go according to specific projects. There are different options we have like we can use Docker for our specific project's need(we can talk about that in a different blog post). There are several other options but in this blog, we will talk about goenv.

Prerequisites: git

We're using Ubuntu 20.04 so below instruction will work in ubuntu. On Mac too. Not sure about windows.

Official Installation Guide GOENV Install readme

Goto github

git clone https://github.com/syndbg/goenv.git ~/.goenv

if this particular git code syntax you(readers) are not familiar with we will explain it, it just cloning the repo and place it on .goenv folder on the home/<username>/ directory.

Define environment variable GOENV_ROOT

echo 'export GOENV_ROOT="$HOME/.goenv"' >> ~/.bashrc
echo 'export PATH="$GOENV_ROOT/bin:$PATH"' >> ~/.bashrc

for zsh/oh-my-zsh users, use zshrc or respective config files according to your terminal settings.

Add goenv init to your shell

echo 'eval "$(goenv init -)"' >> ~/.bashrc

If you want goenv to manage GOPATH and GOROOT (recommended), add GOPATH and GOROOT to your shell after eval "$(goenv init -)"

echo 'export PATH="$GOROOT/bin:$PATH"' >> ~/.bashrc

echo 'export GOPATH="$HOME/<workspaces_path>/golang"' >> ~/.bashrc

GOPATH Folder Structure

|-golang
|--|-bin
|--|-pkg
|--|-src
|--|--|-github.com

Restart your shell so the path changes take effect.

exec $SHELL

Install Go versions into $GOENV_ROOT/versions

goenv install <version_number>

# check all version which can be installed
goenv install -l

# Example
goenv install 1.15.6

To upgrade to a specific release of goenv

cd $(goenv root)
git pull

Uninstalling goenv

rm -rf goenv root

Disable goenv

To disable goenv managing your Go versions, simply remove the goenv init line from your shell startup configuration. This will remove goenv shims directory from PATH, and future invocations like goenv will execute the system Go version, as before goenv.

Uninstalling Go Versions

goenv uninstall

Run this command after you install a new version of Go to activate

goenv rehash

If you like, you can read the same article at our official blog

How to use a specified version of Go (Globally or Locally)

# set go version globally
goenv global 1.15.6

# set go version specific to your project
goenv local 1.15.6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment