Better way to install Golang (Go) on Raspberry Pi

I recently want to install Golang(Go) on my Raspberry Pi for evaluating the performance benefits of Golang, and realised that there is an easy way and there is a better way to install Golang on Rasperry Pi.

If you search the internet you are likely see some of the old tutorials for installing some very old version of Golang from some third-party repo or cross-compiling it by yourself, some of it dated back to the early release of Rasperry Pi, that’s very long ago in software evolution, don’t follow those instructions. When I search on internet, I always looks at the date of the instruction/article, and if it is a few years old or without the date of when the instruction is published, I will generally ignore the instruction and look for more recent information.

The Easy Way

If you search “Golang Raspberry Pi”, you are likely come across this link “Install Golang the easy way” on Raspberry Pi Stack Exchange.

Golang is included in Debian (and therefore Raspbian) linux distribution, so the easy way to install Golang is using the simple and familiar linux command:


sudo apt-get install golang

However, a quick look at Raspbian Stretch package (as at the writing of this article, my installation is based on 2019-04-08-raspbian-stretch-lite.img image), the current Golang version that is packaged with the Raspbian is v1.8, and this can be confirmed by running command sudo apt-cache search golang|grep golang-1.*:


hcheung@e-tinkers:~ $ sudo apt-cache search golang|grep golang-1.*
golang-1.6 - Go programming language compiler - metapackage
golang-1.6-doc - Go programming language - documentation
golang-1.6-go - Go programming language compiler, linker, compiled stdlib
golang-1.6-src - Go programming language - source files
golang-1.7 - Go programming language compiler - metapackage
golang-1.7-doc - Go programming language - documentation
golang-1.7-go - Go programming language compiler, linker, compiled stdlib
golang-1.7-src - Go programming language - source files
golang-1.8 - Go programming language compiler - metapackage
golang-1.8-doc - Go programming language - documentation

The Better Way

The current stable version available at Golang official website is v1.12.6, and there is a distribution packaged for ArmV6 CPU available that is suitable for Raspberry Pi 3 (and some earlier models). Right click on the link that has armv6l on it to copy the link url, and type wget on Raspberry Pi terminal and paste the link to download the golang:


wget https://dl.google.com/go/go1.12.6.linux-armv6l.tar.gz

Decompress the downloaded package and move it to /usr/local directory:


sudo tar -C /usr/local -xzf go1.12.6.linux-armv6l.tar.gz
rm go1.12.6.linux-armv6l.tar.gz

The following steps are required even you choose to install Golang using “The Easy Way”.

We now need to add the PATH environment variable that are required for the system to recongize where the Golang is installed. To do that, edit the ~/.profile file:


nano ~/.profile

Scroll all the way down to the end of the file and add the following:


PATH=$PATH:/usr/local/go/bin
GOPATH=$HOME/golang

I’m going to store my Golang projects at the ~/golang directory, if you like to store it somewhere else or want to have the directory named go instead of golang, feel free to change the GOPATH=$HOME/golang to something else. Finally we need to make the system aware of the new profile.


source ~/.profile

Type which go to find out where the Golang installed and go version to see the installed version and platform.


hcheung@e-tinkers:~ $ which go
/usr/local/go/bin/go
hcheung@e-tinkers:~ $ go version
go version go1.12.6 linux/arm

Golang organizes its code files based on a pre-defined code organization structure. So let’s create the project directories:


mkdir golang
mkdir golang/src

It’s all done, and ready to write the first Go programming. I’m interested to test the shortest path algorithm that I wrote a while back using Golang, so stay tune for my next blog post.

4 comments by readers

  1. Hi, i was trying to install golang on my RPI4, but get stucked on the step
    $ go version
    it gives me
    bash: /usr/local/go/bin/go: No such file or directory
    even though it does exist.
    i can get the go version using sudo, but it does not seems to be a correct behaviour.
    Any suggestion?

    1. Assuming you’ve added PATH=$PATH:/usr/local/go/bin
      to your ~/.profile. This is probably because the files in /usr/local/go/bin belong to root:wheel. In my case, I don’t use the default pi user for my access, but use a username that I created that have the sudo group access. What you could try is to add the username that you are using to the sudo group:

      sudo usermod -a -G sudo username
      

      Remember to replace the username with the user that you are using for accessing the RPI.

  2. When I try to run go1.13.1 on my Raspi4 I get the following error: “bash: /usr/local/go/bin/go: cannot execute binary file: Exec format error”

    I installed go 1.11.x on my Raspi3 and that seems to run OK. It looks like Go has not been updated to Raspi4 yet.

    Is any one else having this problem?

    Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.