How to Install Golang 1.8 on Debian 9 Stretch

golang debian9Go(Golang) is an open source programming language developed by Google. It was released in late 2009. It makes it easy to build simple, reliable, and efficient software. Docker, Kubernetes, Openshift, InfluxDB and many other popular tools are written in GO.

Golang doesn't come pre-installed on most of the Linux distribution, though its installation packages are most likely available in official repositories or external PPA of distribution. But, Generally, These packages are behind one or two version behind the latest one. It's better to install the latest version from the source, and it is quite easy.

This tutorial will walk you through downloading and installing Go 1.8 on debian9, as well as building a simple Hello World application.

Installation of GO 1.8

Update and upgrade packages on your machine by executing below command. The first command will update the packages list available in the repositories. The second command will ensure that you have the latest security patches and fixes. This step is optional.

$ sudo apt-get update
$ sudo apt-get -y upgrade

Download the GO package by use of "wget" or "curl -O" command, which will pull the go binary archive and save it in your current working directory. To find latest version of GO, visit official download page. I will use wget command to download go binary archive.

$ wget https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz

Now extract the downloaded archive using tar command and install it on the desired location. I will use /usr/local as installation directory, you can use prefer some location accessible to multiple users for shared hosting. Below first command will extract the go archive, and the second command will move the extracted go directory to /usr/local.

$ tar -xvf go1.8.3.linux-amd64.tar.gz
$ mv go/ /usr/local

You have successfully installed go 1.8. Now, let's setup go environment.

Setup Go Environment

To setup go environment, you need to set 3 environment variable.

1. GOROOT
- Location where Go package is installed on the system.

2. GOPATH
- Location of work directory or workspace.

3. PATH
- To access go binaries system wide.
Set GOROOT environment variable. This is GO installation directory which we have discussed in the previous step, In my case, It is /usr/local/go.

Set GOROOT environment variable. This is GO installation directory which we have discussed in the previous step, In my case, It is /usr/local/go.

$ export GOROOT=/usr/local/go

GOPATH is the location of GO workspace. In my case, It is $HOME/work/goproj

$ export GOPATH=$HOME/work/goproj

Now set the PATH variable to access go binary system wide.

$ export PATH=$GOROOT/bin:$GOPATH/bin:$PATH

These 3 environment variables are set for the current session only, to make it permanent add above command in ~/.profile file.

Test GO installation

After successful installation and setup of Go environment, It's time to test it. Execute the following command to check the installed go version.

$ go version
go version go1.8.3 linux/amd64

You can also check all the set go environment variable by execution below command.

$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/pradip/work/goproj/"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

Now let's run simple hello world program using GO. Create directory $GOPATH/src/hello.

$ mkdir -p  $GOPATH/src/hello

Create new hello.go file under $GOPATH/src/hello and paste below content.

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

Now, Compile Above file by executing below command.

 $ go install hello

After successful compilation, You can run it by simply referring to the file at your Go path.

$ $GOPATH/bin/hello
hello, world

Here, You "GO". You have successfully installed, setup golang 1.8 and run hello world program. If you find any difficulties following any of above steps, please let me know. I will try to address it. Your feedback in the comment section is valuable. Thanks for reading.

About Pradip Sakhavala

Pradip is a Linux system administrator, an ardent believer in sharing and contributing to open source community. He has a keen interest in DevOps And IoT. He likes to discuss technical stuff anytime. He has started sharing his experience by blogs recently.

Author Archive Page

Have anything to say?

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

All comments are subject to moderation.

2 Comments