How to Build Go from Source on a Raspberry Pi

Since its beginning, Google’s Go programming language has supported multiple operating systems and processor architectures. As well as supporting the x86 32-bit and 64-bit CPUs on Windows, Mac OS X, Linux and FreeBSD, the project has always supported ARM’s microprocessor range including the ARMv5, ARMv6 and ARMv7 system architectures. The Raspberry Pi uses a System-on-a-Chip (SoC) by Broadcom that is based on the ARMv6 architecture. Since the Raspberry Pi can run Linux and FreeBSD, it can also use the Go language.

Google doesn’t yet offer any pre-built binary packages for the Raspberry Pi. While there is a golang package for Raspbian, at the time of writing, it is for an older version of Go. Therefore the best way to install Go on a Raspberry Pi is to build it from source. This may sound a bit daunting but it is actually quite easy.

The first step is to install Mercurial, the revision control system used by Google to manage the Go source code.

sudo apt-get install -y mercurial

The principle command line interface to the Mercurial system is hg and with it, you can download the source code to Go. The following command will download the source necessary to build Go into /usr/local/go:

sudo hg clone -u default https://code.google.com/p/go /usr/local/go

It will probably take about 10 minutes depending on your Internet connection. Once downloaded, you can start the build process. Change directory to the downloaded source code using cd /usr/local/go/src and then start the build process:

sudo ./all.bash

There will be lots of output during the build, mainly informational messages. The build will take somewhere between 90 minutes and two hours. Once completed, the final output will look like this:

raspberry-pi-build-go-final-output

The build process has prompted us to add the directory containing the Go binary to the PATH. To do this you need to edit .profile which the Bash processes whenever you create a new shell. Change directory back to your home directory by using cd without any parameters and then edit .profile using the nano editor:

nano .profile

At the end of the file, add the following line:

export PATH=$PATH:/usr/local/go/bin

Press CTRL + X to leave the nano editor and type Y to confirm that the file should be saved. Press ENTER to keep the current filename (i.e. .profile).

You will need to exit the terminal and restart another (if you are using a desktop environment) or log out and log back in again (if you are using the command line).

To check that Go is functioning correctly create a file called hello.go with the following lines:

package main
 
import "fmt"
 
func main() {
    fmt.Printf("Hello Make Tech Easier!\n")
}

And then run it using:

go run hello.go

The result will be a single line of text which reads: Hello Make Tech Easier!

Go is a very powerful programming language and it is designed for writing programs which work with multiple tasks at once. Known as concurrency, you can write a program to execute lots of tasks simultaneously but also allow these separate tasks to communicate and interact.

Look at our Writing Your First Concurrent Go Program article and try the code examples and see the power of a concurrent programming language on a multitasking operating system, all running on your Raspberry Pi!