Tutorial

How To Install Go on Ubuntu 20.04

How To Install Go on Ubuntu 20.04
Not using Ubuntu 20.04?Choose a different version or distribution.
Ubuntu 20.04

Introduction

Go, sometimes referred to as “Golang”, is an open-source programming language that was released by Google in 2012. Google’s intention was to create a programming language that could be learned quickly.

Since its release, Go has become highly popular among developers and is used for various applications ranging from cloud or server-side applications, to artificial intelligence and robotics. This tutorial outlines how to download and install the latest version of Go (currently version 1.16.7) on an Ubuntu 20.04 server, build the famous Hello, World! application, and make your Go code into an executable binary for future use.

Deploy your Go applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus on scaling your app.

Prerequisites

This tutorial requires an Ubuntu 20.04 system configured with a non-root user with sudo privileges and a firewall as described in Initial Server Setup with Ubuntu 20.04.

Step 1 — Installing Go

In this step, you will install Go on your server.

First, connect to your Ubuntu server via ssh:

  1. ssh sammy@your_server_ip

Next, navigate to the official Go downloads page in your web browser. From there, copy the URL for the current binary release’s tarball.

As of this writing, the latest release is go1.16.7. To install Go on an Ubuntu server (or any Linux server, for that matter), copy the URL of the file ending with linux-amd64.tar.gz.

Now that you have your link ready, first confirm that you’re in the home directory:

  1. cd ~

Then use curl to retrieve the tarball, making sure to replace the highlighted URL with the one you just copied. The -O flag ensures that this outputs to a file, and the L flag instructs HTTPS redirects, since this link was taken from the Go website and will redirect here before the file downloads:

  1. curl -OL https://golang.org/dl/go1.16.7.linux-amd64.tar.gz

To verify the integrity of the file you downloaded, run the sha256sum command and pass it to the filename as an argument:

  1. sha256sum go1.16.7.linux-amd64.tar.gz

This will return the tarball’s SHA256 checksum:

Output
go1.16.7.linux-amd64.tar.gz 7fe7a73f55ba3e2285da36f8b085e5c0159e9564ef5f63ee0ed6b818ade8ef04 go1.16.7.linux-amd64.tar.gz

If the checksum matches the one listed on the downloads page, you’ve done this step correctly.

Next, use tar to extract the tarball. This command includes the -C flag which instructs tar to change to the given directory before performing any other operations. This means that the extracted files will be written to the /usr/local/ directory, the recommended location for installing Go… The x flag tells tar to extract, v tells it we want verbose output (a listing of the files being extracted), and f tells it we’ll specify a filename:

  1. sudo tar -C /usr/local -xvf go1.16.7.linux-amd64.tar.gz

Although /usr/local/go is the recommended location for installing Go, some users may prefer or require different paths.

Step 2 — Setting Go Paths

In this step, you will set paths in your environment.

First, set Go’s root value, which tells Go where to look for its files. You can do this by editing the .profile file, which contains a list of commands that the system runs every time you log in.

Use your preferred editor to open .profile, which is stored in your user’s home directory. Here, we’ll use nano:

  1. sudo nano ~/.profile

Then, add the following information to the end of your file:

sudo nano ~/.profile
. . .
export PATH=$PATH:/usr/local/go/bin

After you’ve added this information to your profile, save and close the file. If you used nano, do so by pressing CTRL+X, then Y, and then ENTER.

Next, refresh your profile by running the following command:

  1. source ~/.profile

After, check if you can execute go commands by running go version:

  1. go version

This command will output the release number of whatever version of Go is installed on your system:

Output
go version go1.16.7 linux/amd64

This output confirms that you are now running Go on your server.

Step 3 — Testing Your Install

Now that Go is installed and the paths are set for your server, you can try creating your Hello, World! application to ensure that Go is working.

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

  1. mkdir hello

Then move into the directory you just created:

  1. cd hello

When importing packages, you have to manage the dependencies through the code’s own module. You can do this by creating a go.mod file with the go mod init command:

  1. go mod init your_domain/hello

Next, create a Hello, World! Go file in your preferred text editor:

  1. nano hello.go

Add the following text into your hello.go file:

hello.go
package main

import "fmt"

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

Then, save and close the file by pressing CTRL+X, then Y, and then ENTER.

Test your code to check that it prints the Hello, World! greeting:

  1. go run .
Output
Hello, World!

The go run command compiles and runs the Go package from a list of .go source files from the new hello directory you created and the path you imported. But, you can also use go build to make an executable file that can save you some time.

Step 4 — Turning Your Go Code Into a Binary Executable

The go run command is typically used as a shortcut for compiling and running a program that requires frequent changes. In cases where you’ve finished your code and want to run it without compiling it each time, you can use go build to turn your code into an executable binary. Building your code into an executable binary consolidates your application into a single file with all the support code necessary to execute the binary. Once you’ve built the binary executable, you can then run go install to place your program on an executable file path so you can run it from anywhere on your system. Then, your program will successfully print Hello, World! when prompted and you won’t need to compile the program again.

Try it out and run go build. Make sure you run this from the same directory where your hello.go file is stored:

  1. go build

Next, run ./hello to confirm the code is working properly:

  1. ./hello
Output
Hello, World!

This confirms that you’ve successfully turned your hello.go code into an executable binary. However, you can only invoke this binary from within this directory. If you wanted to run this program from a different location on your server, you would need to specify the binary’s full file path in order to execute it.

Typing out a binary’s full file path can quickly become tedious. As an alternative, you can run the go install command. This is similar to go build but instead of leaving the executable in the current directory, go install places it in the $GOPATH/bin directory, which will allow you to run it from any location on your server.

In order to run go install successfully, you must pass it the install path of the binary you created with go build. To find the binary’s install path, run the following go list command:

  1. go list -f{{.Target}}

go list generates a list of any named Go packages stored in the current working directory. The f flag will cause go list to return output in a different format based on whatever package template you pass to it. This command tells it to use the Target template, which will cause go list to return the install path of any packages stored in this directory:

Output
‘/home/sammy/go/bin/hello

This is the install path of the binary file you created with go build. This means that the directory where this binary is installed is /home/sammy/go/bin/.

Add this install directory to your system’s shell path. Be sure to change the highlighted portion of this command to reflect the install directory of the binary on your system, if different:

  1. export PATH=$PATH:/home/sammy/go/bin/

Finally, run go install to compile and install the package:

  1. go install

Try running this executable binary by just entering hello

  1. hello
Output
Hello, World!

If you received the Hello, World! output, you’ve successfully made your Go program executable from both a specific and unspecified path on your server.

Conclusion

By downloading and installing the latest Go package and setting its paths, you now have a system to use for Go development. You can find and subscribe to additional articles on installing and using Go within our “Go” tag

Get Ubuntu on a hosted virtual machine in seconds with DigitalOcean Droplets! Simple enough for any user, powerful enough for fast-growing applications or businesses.

Learn more here

About the authors
Default avatar

Technical Writer

Educator and writer committed to empowering our community by providing access to the knowledge and tools for making creative ideas into a reality



Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
1 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!

Simple and easy to understand, as Digital Ocean tutorials are, thank you very much!

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!

Become a contributor for community

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

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.