How to build Go 1.4.2 from source using the new Go bootstrap process

If you really want to play with the latest or most recent version of Go you may have to install it from the source.

The benefit of this is that you get to see what’s the up coming feature from the Go team.

Build newer version of Go version 1.4 and above require a little bit of tricks as to build Go from source you need to have exiting version of Go running first.

Confuse?, don’t worry, I have exactly the same question. How or why do I need to have Go running in order to build Go from source you may ask.

After looking at the Go 1.5 Bootstrap Plan from the Go team it all make sense.

The reason for this is that the Go team is trying to migrate many bootstrap proces that are currently written in C to be written in Go itself. Thus, we need to have Go (binary) in order to make this happen.

Once I understand this, it is not as bad as it seem infact it is very easy if you understand how.

How to build new version of Go from source if you don’t have any previous version of Go installed.

There are 3 steps in order to build Go from source

How to run the script

$chmod +x ~/bin/install-go-from-source-ubuntu && sudo ~/bin/install-go-from-source-ubuntu

Detail of the script (Bash)

#!/bin/bash
## file name: ~/bin/install-go-from-source-ubuntu
set -e

## Install mandatory softwares
apt-get -qq update
apt-get -qq install build-essential curl git-core libpcre3-dev pkg-config zip

## Prepare to install the Go binary for a given platform
mkdir -p /tmp
export GO_BINARY=go1.4.2.linux-amd64.tar.gz

echo "Downloading the go binary please wait..."
curl -o /tmp/$GO_BINARY https://storage.googleapis.com/golang/$GO_BINARY
tar zxvf /tmp/$GO_BINARY -C $HOME

## Remove existing binary if any
rm -rf $HOME/go1.4
mv $HOME/go $HOME/go1.4

## Now we are ready to build Go from source
rm -rf /opt/go
git clone https://go.googlesource.com/go /opt/go

## Note if you want something more stable then try to checkout specific
## version of Go e.g. 1.4.2 in this case,
## if you like using the latest master version just comment out the next two lines
cd /opt/go
git checkout -b go1.4.2 go1.4.2

echo "Building Go from source please wait..."
export GOROOT_BOOTSTRAP=$HOME/go1.4
cd /opt/go/src && ./all.bash

## Setup the GOPATH globally for Ubuntu Linux
## Note should be skipped for OSX
dir -p /opt/gopath
cat <<EOF >/etc/profile.d/gopath.sh
export GOPATH="/opt/gopath"
export PATH="/opt/go/bin:$GOPATH/bin:\$PATH"
EOF

## Make sure the GOPATH is usable by current user
chown -R $USER:$USER /opt/go
chown -R $USER:$USER /opt/gopath
## End of script

I hope this script help to simplify the process of installing the new version of Go. If you find any issues or mistakes please do let me know.

Happy Hacking!

comments powered by Disqus