Tutorial

How To Install Go on Debian 10

Published on July 15, 2019
How To Install Go on Debian 10
Not using Debian 10?Choose a different version or distribution.
Debian 10

Introduction

Go, also known as golang, is a modern, open-source programming language developed by Google. Go tries to make software development safe, fast and approachable to help you build reliable and efficient software.

This tutorial will guide you through downloading and installing Go from source, as well as compiling and executing a “Hello, World!” program on a Debian 10 server.

Prerequisites

To complete this tutorial, you will need access to a Debian 10 server and a non-root user with sudo privileges, as described in Initial Server Setup with Debian 10.

Step 1 — Downloading Go

In this step, we’ll install Go on your server.

First, ensure your apt package index is up to date using the following command:

  1. sudo apt update

Now install curl so you will be able to grab the latest Go release:

  1. sudo apt install curl

Next, visit the official Go downloads page and find the URL for the current binary release’s tarball. Make sure you copy the link for the latest version that is compatible with a 64-bit architecture.

From your home directory, use curl to retrieve the tarball:

  1. curl -O https://dl.google.com/go/go1.12.7.linux-amd64.tar.gz

Although the tarball came from a genuine source, it is best practice to verify both the authenticity and integrity of items downloaded from the internet. This verification method certifies that the file was neither tampered with nor corrupted or damaged during the download process. The sha256sum command produces a unique 256-bit hash:

  1. sha256sum go1.12.7.linux-amd64.tar.gz
Output
go1.12.7.linux-amd64.tar.gz 66d83bfb5a9ede000e33c6579a91a29e6b101829ad41fffb5c5bb6c900e109d9 go1.12.7.linux-amd64.tar.gz

Compare the hash in your output to the checksum value on the Go download page. If they match, then it is safe to conclude that the download is legitimate.

With Go downloaded and the integrity of the file validated, let’s proceed with the installation.

Step 2 — Installing Go

We’ll now use tar to extract the tarball. The following flags are used to instruct tar how to extract, view, and operate on the downloaded tarball:

  • The x flag tells it that we want to extract files from a tarball
  • The v flag indicates that we want verbose output, including a list of the files being extracted
  • The f flag tells tar that we’ll specify a filename to operate on

Now let’s put things all together and run the command to extract the package:

  1. tar xvf go1.12.7.linux-amd64.tar.gz

You should now have a directory called go in your home directory. Recursively change the owner and group of this directory to root, and move it to /usr/local:

  1. sudo chown -R root:root ./go
  2. sudo mv go /usr/local

Note: Although /usr/local/go is the officially-recommended location, some users may prefer or require different paths.

At this point, using Go would require specifying the full path to its install location in the command line. To make interacting with Go more user-friendly, we will set a few paths.

Step 2 — Setting Go Paths

In this step, we’ll set some paths in your environment.

First, set Go’s root value, which tells Go where to look for its files:

  1. nano ~/.profile

At the end of the file, add the following lines:

export GOPATH=$HOME/work
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin

If you chose a different installation location for Go, then you should add the following lines to this file instead of the lines shown above. In this example, we are adding the lines that would be required if you installed Go in your home directory:

export GOROOT=$HOME/go
export GOPATH=$HOME/work
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

With the appropriate lines pasted into your profile, save and close the file.

Next, refresh your profile by running:

  1. source ~/.profile

With the Go installation in place and the necessary environment paths set, let’s confirm that our setup works by composing a short program.

Step 3 — Testing Your Installation

Now that Go is installed and the paths are set for your server, you can ensure that Go is working as expected.

Create a new directory for your Go workspace, which is where Go will build its files:

  1. mkdir $HOME/work

Then, create a directory hierarchy in this folder so that you will be able to create your test file. We’ll use the directory my_project as an example:

  1. mkdir -p work/src/my_project/hello

Next, you can create a traditional “Hello World” Go file:

  1. nano ~/work/src/my_project/hello/hello.go

Inside your editor, add the following code to the file, which uses the main Go packages, imports the formatted IO content component, and sets a new function to print “Hello, World!” when run:

~/work/src/my_project/hello/hello.go
package main

import "fmt"

func main() {
   fmt.Printf("Hello, World!\n")
}

When it runs, this program will print Hello, World!, indicating that Go programs are compiling correctly.

Save and close the file, then compile it by invoking the Go command install:

  1. go install my_project/hello

With the program compiled, you can run it by executing the command:

  1. hello

Go is successfully installed and functional if you see the following output:

Output
Hello, World!

You can determine where the compiled hello binary is installed by using the which command:

  1. which hello
Output
/home/sammy/work/bin/hello

The “Hello, World!” program established that you have a Go development environment.

Conclusion

By downloading and installing the latest Go package and setting its paths, you now have a system to use for Go development. To learn more about working with Go, see our development series How To Code in Go. You can also consult the official documentation on How to Write Go Code.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
3 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

There’s a typo on the curl command above which means the SHA256 checksums won’t match. Users have to add the -L option to curl because the downloaded file otherwise is just a placeholder. Correct command should be:

curl -O -L https://dl.google.com/go/go1.12.7.linux-amd64.tar.gz

Hi, this was great and what I needed to do to install Go but what it didn’t mention was how to change permissions. I’m not sure if this is because of my system umask variable but the executables were not executable by default and I had to play around with the permissions. Perhaps next time I install Golang on Debian 10/11 I will have done it properly, thanks for the tutorial!

If you installed the latest version of Go (1.16 as of this comment), you may need to initialize Go modules in your directory to avoid getting this error:

go install: version is required when current directory is not in a module

all you need to do to initialize your directory to use Go modules, type this command:

go mod init

then type this command:

go mod tidy

it creates a single file called go.mod and then you should be able to carry on with the instructions.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel