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:
Decompress the downloaded package and move it to
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
Scroll all the way down to the end of the file and add the following:
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.
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.