Compiling Go apps for the Raspberry Pi with a Mac

Go is a fantastic language and if you care at all about fast, efficient applications that make handle concurrency a doddle, then you really should take a look at Go.

Download Go and install the package. The package you need is go1.1.darwin-amd64.pkg. You can opt to install from source, but the pre-built packages come complete with the source code anyway, so it saves an extra step or two.

Open up Terminal and change to /usr/local/go/. This is where the installer places the binaries and source code. if you look in /usr/local/go/pkg, you will find the Darwin pkgs in the darwin_amd64 directory.

The processor on the Raspberry Pi is an ARM-based chip, so we need to compile the Go packages specifically for Linux targeting the ARM processor. To do this change to /usr/local/go/src and issue the following command:

sudo GOOS=linux GOARCH=arm CGO_ENABLED=0 ./make.bash --no-clean

Enter your password when prompted. When the Go packages have finished compiling, you should see this message:

Installed Go for linux/arm in /usr/local/go
Installed commands in /usr/local/go/bin

Now you have the necessary packages to compile Go applications for the Raspberry Pi on your Mac. So let’s do that.

So that we have something to compile, let’s write a simple app that when run on a Raspberry Pi, outputs

Hello, linux

Create new text file and paste in the following code:

package main

import (

func main() {
    fmt.Println("Hello,", runtime.GOOS);

Save the program as hello.go in a directory of your choice. In Terminal, change to the directory where you saved the file. In order to compile the application for the Raspberry Pi, you have to set the GOOS and GOARCH environment variables so that go build can link against the correct packages. I also like to specify a platform-specific output path for the compiled application. Compile hello.go with this command:

GOOS=linux GOARCH=arm go build -o ./linux/arm/hello hello.go

This will create a linux directory containing an arm directory which contains an executable called hello. You can try to run it on your Mac, but it will fail with the following message:

exec format error: ./linux/arm/hello

That’s because the instruction set is different between the ARM processor on the Raspberry and the 64-bit Intel processors found in Macs.

Copy the compiled application your Pi using scp as follows:

scp ./linux/arm/hello pi@<ip_address_of_your_pi>:

If you haven’t set up password-less login via SSH, you’ll be prompted to enter your Pi password. The default for the pi user is raspberry.

SSH into your Pi and you will find the Go application in your home directory. To run it, enter this into Terminal:


and you should see

Hello, linux

So there it is. It’s as easy as that. There’s no need to install the Go runtime on your Raspberry Pi in order to run Go applications.

You can use the same technique to create Go applications for other Linux machines running 32-bit or 64-bit processors by replacing arm in the above instructions with 386 or amd64, respectively.