How to install multiple versions of Go

Introduction

This post presents one technique for installing and using multiple versions of Go on a machine. This is a technique I use often as we have standardised on Go 1.2.1 for developing Juju, but develop on the tip of Go itself.

You may find this technique useful for do comparisons between various Go versions for performance or validation.

This procedure mainly applies to Unix installations of Go, however assuming you have the correct toolchain, Windows users can apply it also.

Prerequests

There are two prerequisite before you begin

  1. Do not set GOROOT.
    You must not set $GOROOT. Unset it in your environment.
  2. Remove any existing versions of Go on your system.
    If you have installed Go via your operating system’s package manager, or via a tool like Homebrew, uninstall it before proceeding.

Installation

In this example I’m going to build the latest release of Go, 1.2.1, and the previous stable release 1.1.2. You can extend this pattern to handle other versions.

  1. Clone the Go sources.
    hg clone https://code.google.com/p/go $HOME/go
  2. Clone release working copies.
    Using the clone from the previous step, clone each version of Go using its specific release tag.

    hg clone $HOME/go -r go1.1.2 $HOME/go-1.1.2
    hg clone $HOME/go -r go1.2.1 $HOME/go-1.2.1
  3. Build each version of Go.
    cd $HOME/go-1.1.2/src && ./make.bash
    cd $HOME/go-1.2.1/src && ./make.bash

    If you prefer you can use ./all.bash to run the test suite.

  4. Setup aliases.
    Now you have built Go 1.2.1 and Go 1.1.2, you need to add the go tool for each version to your $PATH. A good way to add them to your path is to use an alias

    alias go-1.1.2=$HOME/go-1.1.2/bin/go
    alias go-1.2.1=$HOME/go-1.2.1/bin/go

    As an alternative, you could use ln -s to setup a symlink.

Usage

Now you can use these versions of the go tool anywhere you would use the normal go tool. For example

$ go-1.2.1 test $PACKAGE # compile and test $PACKAGE with Go 1.2.1
$ go-1.1.2 build $PACKAGE # build $PACKAGE with Go 1.1.2 (results in $CWD)

Caveats

There are several caveats when using this method.

  • Older versions of Go may not work with your system compiler.
    As you are building older software with newer tools you may encounter compilation failures.
    For example, Go versions less than 1.2 probably won’t work with XCode 5 due to the switch to clang.
  • Older versions of Go may not work with the Go subrepositories, or may not support newer features.
    For example the code.google.com/p/go.net/crypto/ssh package requires additional cipher suites added in Go 1.2 and won’t compile with Go 1.1.