Skip to content

Instantly share code, notes, and snippets.

@vsouza
Last active August 31, 2025 10:16
Show Gist options
  • Save vsouza/77e6b20520d07652ed7d to your computer and use it in GitHub Desktop.
Save vsouza/77e6b20520d07652ed7d to your computer and use it in GitHub Desktop.
Golang setup in Mac OSX with HomeBrew. Set `GOPATH` and `GOROOT` variables in zshell, fish or bash.
# Set variables in .bashrc file
# don't forget to change your path correctly!
export GOPATH=$HOME/golang
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
# Set variables in .zshrc file
# don't forget to set path correctly!
export GOPATH=$HOME/golang
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
# Set variables in config.fish file
# don't forget to set path correctly!
# GOLANG configurations
set -x GOPATH $HOME/golang
set -x GOROOT /usr/local/opt/go/libexec
set PATH $GOPATH/bin $GOROOT/bin $PATH
echo "Please enter your golang path (ex: $HOME/golang) :"
read gopath
echo "Please enter your github username (ex: vsouza) :"
read user
mkdir $gopath
mkdir -p $gopath/src/github.com/$user
export GOPATH=$gopath
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew update
brew install go
brew install git
go get golang.org/x/tools/cmd/godoc
go get golang.org/x/tools/cmd/vet
@0xlxy
Copy link

0xlxy commented Nov 9, 2022

@javaducky thanks man - super helpful!

@mteodori
Copy link

better get the prefix via brew --prefix

@cbstodd
Copy link

cbstodd commented Jan 25, 2024

Anyone on Apple silicon will need to instead use the updated prefix for Homebrew, i.e. /opt/homebrew instead of /usr/local. The remainder of the paths will be the same.

The .zshrc example would therefore be:

# Set variables in .zshrc file

# don't forget to set path correctly!

export GOPATH=$HOME/golang
export GOROOT=/opt/homebrew/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin

👍🏼

@Lewiscowles1986
Copy link

yeah I used go env first, and then used this as a guide to modify my .zshrc, trusting that go env was correct. Annoyingly GOBIN was '' though, so I'm not 100% sure the stability of homebrew golang recipe.

@AKSarav
Copy link

AKSarav commented Jan 1, 2025

For Apple M3 with .zshrc

Here is the path I have used.

Make sure to change the Go Version according to your local setup and validate the existence of these directories

export GOPATH=$HOME/go
export GOROOT=/opt/homebrew/Cellar/go/1.23.3/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin

@orkoden
Copy link

orkoden commented Aug 31, 2025

GOROOT does not need to be set with go versions after 1.0. Setting it can cause errors. Go knows where it lives.

If you want to install packages with go, then also set GOPROXY and GOSUMDB.

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
export GOPROXY=https://proxy.golang.org,direct
export GOSUMDB="sum.golang.org"

Apple silicon, macOS Sequoia, 15.6.1 (24G90), Homebrew 4.6.7, go 1.25.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment