How to Install go with goenv on Mac OS

Category:
Last Updated: 2021/12/13 12:40:54

Recently I have been learning go, or golang, a programming language which was designed at Google in 2009.

goenv is a version management system of go. If you have ever used other language version systems like pyenv,rbenv, or nvm, it may be easy to understand what goenv is used for.

You can establish and manage other versions of go with goenv, and when you develop various systems, specific versions are often needed. So I recommend to use goenv to manage various versions of go as, in my case, I actually prefer pyenv for python, rbenv for ruby, nvm for nodejs because nowadays dependencies between modern libraries are getting in chaos anymore and specific versions are often necessary to resolve these complicated dependencies.

# Installation of goenv

# Homebrew (uninstalled)

You can install goenv with homebrew.

$ brew upgrade 
$ brew install goenv
$ goenv -v
goenv 1.23.3
$ eval "$(goenv init -)"
$ echo 'eval "$(goenv init -)"' >> ~/.bash_profile 
1
2
3
4
5
6

In my case, installed goenv version was 1.23.3, and I upgraded it referencing the Github Installation Page but I felt it would be easy and simple to re-install goenv itself from git repos, so I uninstalled goenv from homebrew and re-install it from git repos.

# Git Repos

I followed the Github Installation Page (Basic GitHub Checkout). Actually you have to do is simply to pull the git repos and add some commands to .bash_profile.

# 1. Check out goenv where you want it installed. 
$ git clone https://github.com/syndbg/goenv.git ~/.goenv

# 2. Define environment variable 
$ echo 'export GOENV_ROOT="$HOME/.goenv"' >> ~/.bash_profile
$ echo 'export PATH="$GOENV_ROOT/bin:$PATH"' >> ~/.bash_profile

# 3. Add goenv init to your shell to enable shims
$ echo 'eval "$(goenv init -)"' >> ~/.bash_profile

# 4. 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"' >> ~/.bash_profile
$ echo 'export PATH="$PATH:$GOPATH/bin"' >> ~/.bash_profile

# 5. Restart your shell so the path changes take effect. You can now begin using goenv.
$ exec $SHELL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Then I saw the version of goenv is the latest.

$ goenv -v
goenv 2.0.0beta11
1
2

# Installation of go

You can find installable versions with goenv install -l.

$ goenv install -l
Available versions:
  1.2.2
  1.3.0
  1.3.1
  1.3.2
  1.3.3
  1.4.0
  1.4.1
  1.4.2
  1.4.3
  1.5.0
  ....1.16.10
  1.17.0
  1.17beta1
  1.17rc1
  1.17rc2
  1.17.1
  1.17.2
  1.17.3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

If you want the version of 1.17.3 (the latest one when I'm writing now), you can install it like this:

$ goenv install 1.17.3
$ goenv global 1.17.3 
$ go version
go version go1.17.3 darwin/arm64
1
2
3
4

Category:
Last Updated: 2021/12/13 12:40:54
Copyright © Web Ninja All Rights Reserved.