How to Install Go (Golang) on Raspberry Pi 3 (Raspbian Stretch / Debian 9)

3067
Share:
how-to-install-go-golang-on-raspberry-pi-3-raspbian-stretch-debian-9

Go (also referred to as Golang) is a statically typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. Go is syntactically similar to C, but with memory safety, garbage collection, structural typing, and CSP-style concurrency.

Go was designed at Google in 2007 to improve programming productivity in an era of multicore, networked machines and large codebases. The designers wanted to address criticism of other languages in use at Google, but keep their useful characteristics:

  • Static typing and run-time efficiency (like C++)
  • Readability and usability (like Python or JavaScript)
  • High-performance networking and multiprocessing

The designers were primarily motivated by their shared dislike of C++.

Installation Methods

Just as old saying "all roads lead to Rome", many ways can be used to install Go (Golang) on Raspbian Stretch (Debian 9). However, I'll just mention two methods here:

  • Install from official Debian repository (version 1.7 by the time of this writing)
  • Install official binary from Golang website (latest version)

Install from Official Debian Repository

This is the most straightforward method, as you'll just need to type one command:

$ sudo apt install golang

After pressing enter, your terminal will shows something like this:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
golang-1.7 golang-1.7-doc golang-1.7-go golang-1.7-src golang-doc golang-go golang-src
Suggested packages:
bzr mercurial subversion
The following NEW packages will be installed:
golang golang-1.7 golang-1.7-doc golang-1.7-go golang-1.7-src golang-doc golang-go golang-src
0 upgraded, 8 newly installed, 0 to remove and 380 not upgraded.
Need to get 27.5 MB of archives.
After this operation, 144 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y

On the last line it will show "Do you want to continue?" prompt. Just press "Y" and it will continue with download and install Golang to your Raspbian Stretch system.

Do you want to continue? [Y/n] Y
Get:1 http://mirror.rise.ph/raspbian/raspbian stretch/main armhf golang-1.7-src armhf 1.7.4-2+rpi1 [7677 kB]
Get:2 http://mirror.rise.ph/raspbian/raspbian stretch/main armhf golang-1.7-go armhf 1.7.4-2+rpi1 [17.4 MB]                     
Get:3 http://mirror.rise.ph/raspbian/raspbian stretch/main armhf golang-1.7-doc all 1.7.4-2+rpi1 [2391 kB]                      
Get:3 http://mirror.rise.ph/raspbian/raspbian stretch/main armhf golang-1.7-doc all 1.7.4-2+rpi1 [2391 kB]                      
Get:4 http://mirror.rise.ph/raspbian/raspbian stretch/main armhf golang-1.7 all 1.7.4-2+rpi1 [27.0 kB]                          
Get:5 http://mirror.rise.ph/raspbian/raspbian stretch/main armhf golang-src armhf 2:1.7~5 [3560 B]                              
Get:6 http://mirror.rise.ph/raspbian/raspbian stretch/main armhf golang-go armhf 2:1.7~5 [22.7 kB]                              
Get:7 http://mirror.rise.ph/raspbian/raspbian stretch/main armhf golang-doc all 2:1.7~5 [3600 B]                                
Get:8 http://mirror.rise.ph/raspbian/raspbian stretch/main armhf golang armhf 2:1.7~5 [3538 B]                                  
Fetched 26.3 MB in 56s (464 kB/s)                                                                                               
Selecting previously unselected package golang-1.7-src.
(Reading database ... 122154 files and directories currently installed.)
Preparing to unpack .../0-golang-1.7-src_1.7.4-2+rpi1_armhf.deb ...
Unpacking golang-1.7-src (1.7.4-2+rpi1) ...
Selecting previously unselected package golang-1.7-go.
Preparing to unpack .../1-golang-1.7-go_1.7.4-2+rpi1_armhf.deb ...
Unpacking golang-1.7-go (1.7.4-2+rpi1) ...
Selecting previously unselected package golang-1.7-doc.
Preparing to unpack .../2-golang-1.7-doc_1.7.4-2+rpi1_all.deb ...
Unpacking golang-1.7-doc (1.7.4-2+rpi1) ...
Selecting previously unselected package golang-1.7.
Preparing to unpack .../3-golang-1.7_1.7.4-2+rpi1_all.deb ...
Unpacking golang-1.7 (1.7.4-2+rpi1) ...
Selecting previously unselected package golang-src.
Preparing to unpack .../4-golang-src_2%3a1.7~5_armhf.deb ...
Unpacking golang-src (2:1.7~5) ...
Selecting previously unselected package golang-go.
Preparing to unpack .../5-golang-go_2%3a1.7~5_armhf.deb ...
Unpacking golang-go (2:1.7~5) ...
Selecting previously unselected package golang-doc.
Preparing to unpack .../6-golang-doc_2%3a1.7~5_all.deb ...
Unpacking golang-doc (2:1.7~5) ...
Selecting previously unselected package golang.
Preparing to unpack .../7-golang_2%3a1.7~5_armhf.deb ...
Unpacking golang (2:1.7~5) ...
Processing triggers for man-db (2.7.6.1-2) ...
Setting up golang-1.7-src (1.7.4-2+rpi1) ...
Setting up golang-src (2:1.7~5) ...
Setting up golang-1.7-go (1.7.4-2+rpi1) ...
Setting up golang-1.7-doc (1.7.4-2+rpi1) ...
Setting up golang-go (2:1.7~5) ...
Setting up golang-1.7 (1.7.4-2+rpi1) ...
Setting up golang-doc (2:1.7~5) ...
Setting up golang (2:1.7~5) ...

Congratulation, you've installed Go (Golang) successfully.

Install Official Binary from Golang Website

If you want to get the latest version of Golang and know the commands you're about to type, then read on.

Official binary distributions are available for the FreeBSD (release 10-STABLE and above), Linux, macOS (10.10 and above), and Windows operating systems and the 32-bit (386) and 64-bit (amd64) x86 processor architectures.

First, you need to download the archive from here.

Golang Website

As we're about to install Golang on Raspberry Pi (which uses ARM processor), then choose go1.12.5.linux-armv6l.tar.gz.

After the archive downloaded successfully, extract it to your preferred directory. For example, I'm using /opt directory.

$ sudo tar -C /opt -xzf go1.12.5.linux-armv6l.tar.gz

Next, you'll need to add /opt/go/bin to the PATH environment variable. You can do this by adding this line to your /etc/profile (for a system-wide installation) or $HOME/.profile:

export PATH=$PATH:/opt/go/bin

Note: changes made to a profile file may not apply until the next time you log into your computer. To apply the changes immediately, just run the shell commands directly or execute them from the profile using a command such as $ source $HOME/.profile.

That's it, Go should've successfully installed on your system.

Testing

First, check for installed Go version

$ go version
go version go1.12.5 linux/arm

Next step to make sure we've installed Go correctly, we can set up a workspace and building a simple program, as follows.

Create your workspace directory, $HOME/go. (If you'd like to use a different directory, you will need to set the GOPATH environment variable.)

Next, make the directory src/hello inside your workspace, and in that directory create a file named hello.go that looks like:

package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n")
}

Then build it with the go tool:

$ cd $HOME/go/src/hello
$ go build

The command above will build an executable named hello in the directory alongside your source code. Run it to see the greeting:

$ ./hello
hello, world

If you see the "hello, world" message then your Go installation is working. You can run go install to install the binary into your workspace's bin directory or $ go clean -i to remove it.

Final Words

I hope that you now know how to install Go (Golang) on Raspbian Stretch (Debian 9). If you run into any issues or have any feedback feel free to drop a comment below.

Tags
Share:

1 comment

  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?

    • Hello, Make sure you have the correct permissions on your extraction directory.

Leave a reply

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