22

I checked diverse forums but I still did not make it working. I like to install go (golang) on my Raspberry PI - Raspbian:

With

sudo apt-get install golang

I installed go and with

export GOPATH=$home/pi/gocode

i set the GOPATH so i tryed to install from a homepage a new program with (sudo go get -u github.com/....) but, I only get "cannot download, $GOPATH not set. For more details see: go help gopath".

I really get crazy for my studip simple mistake that i do not see.

I would be pleased if i get a very detailed "how to do" discription since I am new to Linux and Raspbian, so everything which is made for real dummys should be good enough for me. Thank you for your help.

tgogos
  • 23,218
  • 20
  • 96
  • 128
heiko
  • 267
  • 1
  • 2
  • 5
  • 1
    _Never_ build using `sudo`. just use `go get`, `go build`, `go install`, etc. – JimB Jan 19 '18 at 19:33
  • As @JimB said don't use sudo to run the go command. You just need it to install it and your already did it with `sudo apt-get`. Just to let you know, you have set the GOPATH to your user when doing that export. But when you run somethin with `sudo` you are running a command as the `root` user which has not set the GOPATH. `sudo` -> "Do as super user" – artberri Jan 19 '18 at 19:39
  • Also, of you're getting a `$GOPATH not set` error, you're using an old version of Go. You may want to use the binary install directly from golang.org. if it's available for your os/arch. – JimB Jan 19 '18 at 19:48
  • How to make go working: download the latest version of go in a new folder (like download) go1.9.linux-armv6l.tar.gz in a new directory like download install with sudo apt-get install golang an older version of go (golang) in my case it was 1.7.4 then use sudo tar -C /home/pi -xzf go1.9.linux-armv6l.tar.gz – heiko Jan 21 '18 at 12:30
  • to install go version 1.9 in a new directory in my case home/pi/go with sudo apt remove golang and sudo apt-get autoremove remove the older version of go to check unse go version which give you the actual version of go it should be go1.9 linux/arm please check with go env or go env GOPATH the GOPATH direction – heiko Jan 21 '18 at 12:31
  • with ls -a (used in /home/pi/ ) give you a list of all files and also shows you ~/.profile with sudo nano ~/.profile you can open that file an add the recomanded code for the go directory export GOROOT=/home/pi/go export GOPATH=/home/pi/go/bin close with STRG + O and ENTER and STRG + X check with go env GOPATH then use source ~/.profile Now go should be running on the latest version and should have the correct GOPATH directory For me helpful was https://tecadmin.net/install-go-on-debian/# – heiko Jan 21 '18 at 12:31

3 Answers3

38

This are detailed instructions about how to install Go on Raspbian Stretch from the repositories.

As of today, 2018-01-30, this will install Go 1.7. The most actual version for manual installation from the downloads is Go 1.9.3.

I. Login to your user on the Raspberry Pi (I'm using the default user pi).

II. Install Go (golang)

pi@pi3-2:~ $ sudo apt update 
pi@pi3-2:~ $ sudo apt install golang

III. Create a working directory for all your go projects in your $HOME directory. It's best to name it go, as this defaults to the GOPATH in future Go versions (starting with Go 1.8).

pi@pi3-2:~ $ mkdir go

IV. Append the $GOPATH environment variable and modified PATH settings to your .profile

pi@pi3-2:~ $ echo 'export GOPATH=$HOME/go' >> ~/.profile
pi@pi3-2:~ $ echo 'PATH="$HOME/go/bin:$PATH"' >> ~/.profile

V. Logout and Relog with the new settings then check your settings

pi@pi3-2:~ $ go env

GOARCH="arm"
GOBIN=""
GOEXE=""
GOHOSTARCH="arm"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/pi/go"
GORACE=""
GOROOT="/usr/lib/go-1.7"
GOTOOLDIR="/usr/lib/go-1.7/pkg/tool/linux_arm"
CC="gcc"
GOGCCFLAGS="-fPIC -marm -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build187598155=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

Especially make sure GOPATH points to your previously created Go working directory. Don't care about setting GOBIN as mentioned in some documentation. It's usually not necessary and Go will automatically use $GOPATH/bin/ for your Go installs.

However, you might also want to check the path settings (/home/pi/go/bin should be included) to make sure you can run the code you installed with go install.

pi@pi3-2:~ $ echo $PATH
/home/pi/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

VI. A few words about the Go working directory structure

Over time, the Go working directory will contain three sub-directories: bin, src and pkg. Except src they will be automatically created, when needed the first time. The structure for user pi will look like this:

/home
  /pi
    /go
      /src
      /pkg
      /bin

bin will contain all Go executable's you have installed using go install command.

pkg will contain all compiled packages that can be imported into your projects.

src will contain all your source files, either your own or sources downloaded from external repositories.

For eksample the command go get github.com/petergloor/hello-go will automatically fetch and place the source files from the corresponding external Github repository into the local directory $HOME/go/src/github.com/petergloor/hello-go.

As it's rather common to fetch external repositories either for reference or contribution it becomes important to keep your directory structure always well organized and clean.

Apart from that you are free to organize your projects as long they are hierarchically structured below the $HOME/go/src/ directory and follow the rules mentioned in the documentation.

However, to clearly organize my projects I personally always place my projects into $HOME/go/src/github.com/my-github-account even if I don't have an external repository for it.

If you don't have a github account you can likewise use any other external repository account.

As I mentioned, even it's not needed at all I prefere to use my Github account to clearly identify my projects. And even it's not needed I will use the username pi to distinct the user from other project maintainers in the following example.

VII. So let's add a "hello world" project to test our installation.

a) First let's create the project folder and cd into its directory.

pi@pi3-2:~ $ mkdir -p $HOME/go/src/pi/helloworld
pi@pi3-2:~ $ cd $HOME/go/src/pi/helloworld
pi@pi3-2:~/go/src/pi/helloworld $

b) With an editor of your choice create a file main.go with the following content

// helloworld project main.go.
package main

import ("fmt")

// main is the entrypoint of the application.
func main() {
  fmt.Println("Hello world! Greetings from Raspberry Pi")
}

Spacing doesn't matter at this point. Go provides a nice tool to do this for you.

c) Now try to run the program.

pi@pi3-2:~/go/src/pi/helloworld $ go run main.go
Hello world! Greetings from Raspberry Pi
pi@pi3-2:~/go/src/pi/helloworld $

In case you get an error, fix it! Carefully, check the spelling and cases (Go is case-sensitive).

d) Next lets format the code:

pi@pi3-2:~/go/src/pi/helloworld $ go fmt

Without a file name this will properly (re-)format all source files within this directory and below.

e) Next let's build helloworld as an executable procram, within this directory.

pi@pi3-2:~/go/src/pi/helloworld $ go build
pi@pi3-2:~/go/src/pi/helloworld $ ls
helloworld  main.go
pi@pi3-2:~/go/src/pi/helloworld $

f) Now you can run it.

pi@pi3-2:~/go/src/pi/helloworld $ ./helloworld
Hello world! Greetings from Raspberry Pi
pi@pi3-2:~/go/src/pi/helloworld $

g) Finally let's install the program into the $HOME/go/bin/ directory.

pi@pi3-2:~/go/src/pi/helloworld $ go install
pi@pi3-2:~/go/src/pi/helloworld $ ls $HOME/go/bin
hello-go  helloworld
pi@pi3-2:~/go/src/pi/helloworld $

h) If everything is done right it can be run by our pi user from anywhere by just entering the name of the command.

pi@pi3-2:~/go/src/pi/helloworld $ helloworld
Hello world! Greetings from Raspberry Pi
pi@pi3-2:~/go/src/pi/helloworld $ cd ~
pi@pi3-2:~ $ helloworld
Hello world! Greetings from Raspberry Pi
pi@pi3-2:~ $

Congratulations!

Peter Gloor
  • 917
  • 6
  • 19
  • 2
    Thank you @Peter. I've followed your well-written steps and everything works! this should be the accepted answer. – Mohammed Swillam Sep 23 '18 at 15:34
  • as awesome as the instructions are, and their great for testing out a new install of the go lang tooling, putting that helloworld directory within the `$HOME/go/src` directory will prevent packages from updating using `go get -u [github.com/user/go-pkg]` the `go get` cmd will complain about having a directory within the `src` dir that hasn't been initialized with a VCS. see [this](https://github.com/tools/godep/issues/116) – ipatch Apr 28 '20 at 19:25
3

as of Nov 2019. (Please note if you need version 1.13+ please wget go1.13.3.linux-amd64.tar.gz manually) OR sudo apt-get install golang-go - source

You just have to type following commands on your RPi

sudo apt-get update
sudo apt-get install golang --fix-missing

and when you type now go version following should be your output

pi@rpi1:~ $ go version
go version go1.11.6 linux/arm

Additionally, I also had the following installed before running everything sudo apt-get install make gcc g++

enter image description here

STEEL
  • 8,955
  • 9
  • 67
  • 89
-2

How to install golang:

Download the latest tarball, go1.9.linux-armv6l.tar.gz, to a directory such as /home/pi/downloads.

Then use:

sudo tar -C /home/pi -xzf go1.9.linux-armv6l.tar.gz

to extract the go installation to the directory

/home/pi

creating /home/pi/go

to check use

go version

Which will give you the actual version of go.

It should be go1.9 linux/arm.

Please check with:

go env

or

go env GOPATH

the GOPATH direction

with

ls -a # (used in /home/pi/ )

give you a list of all files and also shows you ~/.profile

with

sudo nano ~/.profile

you can open that file an add the reccommended code for the go directory

 export GOROOT=/home/pi/go
 export GOPATH=/home/pi/go/bin

close with STRG + O and ENTER and STRG + X

check with

go env GOPATH

then use

source ~/.profile

then you can check again with

go env

Now go should be running on the latest version and should have the correct GOPATH directory

For me helpful was this link: https://tecadmin.net/install-go-on-debian/#

I really hope some others also give detailed descriptions of how to install a program in a brief and detailed way. Instead of 3 lines of single code.

bschlueter
  • 3,817
  • 1
  • 30
  • 48
heiko
  • 267
  • 1
  • 2
  • 5
  • 1
    ok, one more comment on that export GOPATH =/home/pi/go export PATH=$PATH:/home/pi/go/bin in the ~/.profile document otherwise i got an error wenn trying to run go but now it works perfectly, no errors or anything even with the older version 1.7.4 of golang. so i really hope this helps others or me later on, when setting up again – heiko Jan 21 '18 at 14:58