I like to install my Go from source so I can mess around and stay up to date. Here's how I do so.
I prefer an installation directory in $HOME/opt
. I like putting it here so that it's completely within my home directory, but feel free to change it.
For the purposes of these instructions, I'm assuming OSX/*nix (Windows instructions shortly).
Homebrew is a software repository that allows you to easily install applications and development tools on MacOS. It is constantly updating and comes highly recommended due to its ease of use, so the rest of this guide depends on it. Please file an issue if you want alternate instructions, and I will add them.
The Go project uses the Git version control software to allow distributed development on source code, so you need to install that first, so you can keep your copy of the Go source up to date and check out new releases.
brew install git
You need a C compiler if you want to build Go with cgo support, which will enable you to compile programs which import C libraries.
MacOS's default compilers which come with XCode should suffice, but you may need to install them first.
xcode-select --install
Curl is a useful tool to make HTTP requests from the command-line and it will help you to download the files you need.
brew install curl
Go is hosted on googlesource.com (mirrored to github), and its versions are always tagged, so you can easily check out any version you like.
git clone https://go.googlesource.com/go $HOME/opt/go
You probably want to start with the latest version of the code as well as the bootstrap binaries. To get the latest version, you can use the tags in the git repository. Putting this in an environment variable will help make the rest of these steps easier:
export LATEST_GO_VERSION=`git tag|grep ^go|grep -v 'beta\|rd'|sort -V|tail -1`
As of version 1.5, Go is now a self-hosted language, meaning the entirety of the code (including the runtime and compiler) are written in a mixture of Go and assembly that Go can compile itself.
This means you'll need Go to build Go, and the best way to do this is to just download a pre-compiled version for your architecture. I know this sounds weird, but don't worry, you only ever have to do this once, and you can re-use the same bootstrap if you ever want to build Go again.
First, download the latest binary distribution and unpack it into $HOME/opt/go-bootstrap
. The latest version can be obtained from https://golang.org/dl/, just make sure to pick the tar.gz version.
Note: you want the binary distro, not the src distro (we'll get the source via git later).
mkdir -p $HOME/opt/go-bootstrap
curl -sL https://dl.google.com/go/${LATEST_GO_VERSION}.darwin-amd64.tar.gz | tar -x --strip-components=1 -C $HOME/opt/go-bootstrap go
You now have a full binary go installation in $HOME/opt/go-bootstrap
, and you should never need to touch it again. Just let it hang out there and build go runtimes for you. Maybe update it once in awhile.
Now that you have a boostrap binary distro and the source, check out the latest release and build:
cd $HOME/opt/go/src
git checkout $LATEST_GO_VERSION
GOROOT_BOOTSTRAP=$HOME/opt/go-bootstrap ./all.bash
Now go get some coffee, take a walk, or amuse yourself for a bit; this will take a little while as Go builds and self-tests. When it's finally done (assuming no failures), you should have a shiny new go installation in $HOME/opt/go
. Should you ever wish to upgrade, simply check out another tag, run clean.bash
, and go to town.
First, you'll need to put the main go binaries in your $PATH
. These binaries are hard-coded to refer to the environment they were built in, which includes the source directory, so you'll need to build again if you want to move the Go core files to another location. To do this, add the following line to your .bashrc
(or whatever is appropriate for your environment).
export PATH=$HOME/opt/go/bin:$PATH
or something like it in your shell configuration.
It used to be that you needed to create a special directory (called your "GOPATH") for storing all go files to be built, including dependencies. As of go1.13 Go Modules are now the official method to specify and store dependencies, and the build tools use this system by default.
Packages will be automatically downloaded and stored in $HOME/go/pkg/mod
. This directory will be automatically created the first time it's needed, so if you are using go1.13 or later, you are now done!
Please make sure you read the link above as well as the relevant section in the go1.13 release notes for more information on where your packages will be coming from, and how to alter these settings if you need to. I would like to update this guide soon to include more information about this, but PRs are of course welcome in the meantime.