install multiple versions of GO on the same machine [linux]

This quick tutorial shows how to do install multiple versions of go manually.

I’ll try to prepare a shell script that will do it automatically for you 🙂

  1. download selected GO releases
  2. create directories for specific version of GO
  3. unpack downloaded GO releases
  4. install `go` alternatives
  5. install `godoc` alternatives
  6. install `gofmt` alternatives
  7. configure version of go, godoc & gofmt interactively
  8. alternatively you can configured it in a non-interactive way
  9. set the GOROOT env variable to match the location of selected version of GO
# 1
wget https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz
wget https://storage.googleapis.com/golang/go1.4.3.linux-amd64.tar.gz
wget https://storage.googleapis.com/golang/go1.5.linux-amd64.tar.gz
wget https://storage.googleapis.com/golang/go1.5.1.linux-amd64.tar.gz

# 2
sudo mkdir -p /usr/local/go/1.4.2/
sudo mkdir -p /usr/local/go/1.4.3/
sudo mkdir -p /usr/local/go/1.5/
sudo mkdir -p /usr/local/go/1.5.1/

# 3
sudo tar -C /usr/local/go/1.4.2/ --strip-components=1 -xzf go1.4.2.linux-amd64.tar.gz
sudo tar -C /usr/local/go/1.4.3/ --strip-components=1 -xzf go1.4.3.linux-amd64.tar.gz
sudo tar -C /usr/local/go/1.5/ --strip-components=1 -xzf go1.5.linux-amd64.tar.gz
sudo tar -C /usr/local/go/1.5.1/ --strip-components=1 -xzf go1.5.1.linux-amd64.tar.gz

# 4
sudo update-alternatives --install /usr/bin/go go /usr/local/go/1.4.2/bin/go 2
sudo update-alternatives --install /usr/bin/go go /usr/local/go/1.4.3/bin/go 3
sudo update-alternatives --install /usr/bin/go go /usr/local/go/1.5/bin/go 4
sudo update-alternatives --install /usr/bin/go go /usr/local/go/1.5.1/bin/go 5

# 5
sudo update-alternatives --install /usr/bin/godoc godoc /usr/local/go/1.4.2/bin/godoc 2
sudo update-alternatives --install /usr/bin/godoc godoc /usr/local/go/1.4.3/bin/godoc 3
sudo update-alternatives --install /usr/bin/godoc godoc /usr/local/go/1.5/bin/godoc 4
sudo update-alternatives --install /usr/bin/godoc godoc /usr/local/go/1.5.1/bin/godoc 5

# 6
sudo update-alternatives --install /usr/bin/gofmt gofmt /usr/local/go/1.4.2/bin/gofmt 2
sudo update-alternatives --install /usr/bin/gofmt gofmt /usr/local/go/1.4.3/bin/gofmt 3
sudo update-alternatives --install /usr/bin/gofmt gofmt /usr/local/go/1.5/bin/gofmt 4
sudo update-alternatives --install /usr/bin/gofmt gofmt /usr/local/go/1.5.1/bin/gofmt 5

# 7 - If you want to configure version of go, godoc & gofmt interactively then use:
sudo update-alternatives --config go
sudo update-alternatives --config godoc
sudo update-alternatives --config gofmt

# 8 - If you want to configure version non-interactively then use:
sudo update-alternatives --set go /usr/local/go/1.4.2/bin/go
sudo update-alternatives --set godoc /usr/local/go/1.4.2/bin/godoc
sudo update-alternatives --set gofmt /usr/local/go/1.4.2/bin/gofmt

sudo update-alternatives --set go /usr/local/go/1.4.3/bin/go
sudo update-alternatives --set godoc /usr/local/go/1.4.3/bin/godoc
sudo update-alternatives --set gofmt /usr/local/go/1.4.3/bin/gofmt

sudo update-alternatives --set go /usr/local/go/1.5/bin/go
sudo update-alternatives --set godoc /usr/local/go/1.5/bin/godoc
sudo update-alternatives --set gofmt /usr/local/go/1.5/bin/gofmt

sudo update-alternatives --set go /usr/local/go/1.5.1/bin/go
sudo update-alternatives --set godoc /usr/local/go/1.5.1/bin/godoc
sudo update-alternatives --set gofmt /usr/local/go/1.5.1/bin/gofmt

# 9 - set GOROOT to your current version of GO
export GOROOT=$(dirname $(dirname $(readlink -f $(which go | cut -d" " -f3))))

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s