How to Install Go on Debian 9

/ Category: Tutorials / Comments: None

Spread the love

In this tutorial, we will guide you through the steps of installing Go on a Debian 9 Cloud VPS.

Go, also known as Golang, is a free and open-source programming language developed by Google. It can be used to build simple, fast, reliable, and efficient software applications. Go is the most widely used programming language in the world. Many popular applications such as Docker, Openshift, Graphana and Docker are written in Go language. Go also allow us to run multiple processes of the applications at the same time. Let’s begin with the installation.

Prerequisites

  • A Debian 9 Cloud VPS. For the purposes of this tutorial we will use our LC VPS-1 hosting plan.
  • System user with root privileges. We include this with our servers.

Step 1. Log in and Update Packages

Log in to your Debian 9 Cloud VPS via SSH as the root user (or as a user with sudo privileges). We can do that by entering this command:

ssh [email protected]_IP_Address -p Port_Number

Remember to replace ‘root’ with your username if you are not using the root user to log in. Change Server_IP_Address and Port_Number according to your server’s IP address and SSH port number.

Once logged in, you need to update all of the OS packages to their latest available version by running the following commands:

apt update 
apt upgrade

Step 2. Install Go

Go doesn’t come pre-installed on most of the Linux distributions. We will have to install the latest version of Go programming language from source.

Download the latest version of Go using the wget command:

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

Then, verify the downloaded file checksum with the sha256sum utility:

sha256sum go1.12.6.linux-amd64.tar.gz

The output should be similar to the one below:

dbcf71a3c1ea53b8d54ef1b48c85a39a6c9a935d01fc8291ff2b92028e59913c go1.12.6.linux-amd64.tar.gz

Run the following command to extract the tarball in the /usr/local directory.

tar -C /usr/local -xzf go1.12.6.linux-amd64.tar.gz

Step 3. Setup Go Environment

Once the Go tarball is extracted, we need to set the PATH environment variable in order to find Go executable binaries by the system.

We can do this by editing the /etc/profile file (for a system-wide installation) or the $HOME/.profile file (for a current user installation).

nano /etc/profile

Append the following line:

export PATH=$PATH:/usr/local/go/bin

Save and close the file. Then, apply the new PATH environment variable with the following command:

source /etc/profile

Step 4. Verify Go Installation

We have successfully installed and configured Go programming language on a Debian 9 Cloud VPS.

We can now check the installed Go version with the following command:

go version

The output should be similar to the one below:

go version go1.12.6 linux/amd64

We can also check all the Go environment variables by running the following command:

go env

The output should be similar to the following:

GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build527675922=/tmp/go-build -gno-record-gcc-switches"

A Simple Go Example: Hello World

We will create a simple program that simply outputs ‘Hello World’ to your terminal.

Open your favorite editor (in our example we are using nano as editor) and create a file named hello-world.go:

nano hello-world.go

and enter the code inside:

package main

import "fmt"

func main() {
    fmt.Println("Hello World")
}

This program is extremely simple and after you run:

go run hello-world.go

The output of this program should be:

Hello World

We can build this simple program into binary. We can do this with the go build command:

go build hello-world.go

Then, we can run the newly created binary directly using:

./hello-world

That’s it! Go is now installed and fully functional on your Debian 9 Cloud VPS, and you tried a test program to see how it works. For more details on how to use this platform, please check their official documentation.


Of course, you don’t have to install Go on Debian 9, if you use one of our Managed Debian Cloud VPS Hosting solutions, in which case you can simply ask our expert Linux admins to setup Go on Debian 9 for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post on how to install Go on Debian 9, please share it with your friends through the social networks by using the buttons below, or simply leave a comment in the comments section. Thanks.

Be the first to write a comment.

Your feedback