Tutorial

How To Install Go on Ubuntu 18.04

Published on July 12, 2018
English
How To Install Go on Ubuntu 18.04
Not using Ubuntu 18.04?Choose a different version or distribution.
Ubuntu 18.04

Introduction

Go is a modern programming language developed at Google. It is increasingly popular for many applications and at many companies, and offers a robust set of libraries. This tutorial will walk you through downloading and installing the latest version of Go (Go 1.10 at the time of this article’s publication), as well as building a simple Hello World application.

Prerequisites

This tutorial assumes that you have access to an Ubuntu 18.04 system, configured with a non-root user with sudo privileges as described in Initial Server Setup with Ubuntu 18.04.

Step 1 — Installing Go

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

To begin, connect to your Ubuntu server via ssh:

  1. ssh sammy@your_server_ip

In order to install Go, you’ll need to grab the latest version from the official Go downloads page. On the site you can find the URL for the current binary release’s tarball, along with its SHA256 hash.

Visit the official Go downloads page and find the URL for the current binary release’s tarball, along with its SHA256 hash. Make sure you’re in your home directory, and use curl to retrieve the tarball:

  1. cd ~
  2. curl -O https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz

Next, you can use sha256sum to verify the tarball:

  1. sha256sum go1.10.3.linux-amd64.tar.gz
Sample Output
go1.10.3.linux-amd64.tar.gz fa1b0e45d3b647c252f51f5e1204aba049cde4af177ef9f2181f43004f901035 go1.10.3.linux-amd64.tar.gz

You’ll get a hash like the one highlighted in the above output. Make sure it matches the one from the downloads page.

Next, use tar to extract the tarball. 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. tar xvf go1.10.3.linux-amd64.tar.gz

You should now have a directory called go in your home directory. Recursively change go’s owner and group 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.

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. sudo nano ~/.profile

At the end of the file, add this line:

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

If you chose an alternate installation location for Go, add these lines instead to the same file. This example shows the commands if Go is installed in your home directory:

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

With the appropriate line pasted into your profile, save and close the file. Next, refresh your profile by running:

  1. source ~/.profile

Step 3 — Testing Your Install

Now that Go is installed and the paths are set for your server, you can test to 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 through this command in order for you to create your test file. You can replace the value user with your GitHub username if you plan to use Git to commit and store your Go code on GitHub. If you do not plan to use GitHub to store and manage your code, your folder structure could be something different, like ~/my_project.

  1. mkdir -p work/src/github.com/user/hello

Next, you can create a simple “Hello World” Go file.

  1. nano ~/work/src/github.com/user/hello/hello.go

Inside your editor, paste the code below, which uses the main Go packages, imports the formatted IO content component, and sets a new function to print “Hello, World” when run.

package main

import "fmt"

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

This program will print “hello, world” if it successfully runs, which will indicate that Go programs are compiling correctly. Save and close the file, then compile it by invoking the Go command install:

  1. go install github.com/user/hello

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

  1. hello

If that command returns “hello, world”, then Go is successfully installed and functional. You can see where the compiled hello binary is installed by using the which command:

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

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

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

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

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

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.

this was really really helpful that i was forced to comment and say how good it was

There is a much better way to do this that allows multiple versions to be installed so you can switch between them as needed.

I’ve detailed the process here https://github.com/jsnfwlr/install-guides/blob/main/ubuntu/go.md

Important Note: you must use flag -L when curl’ing. If you do not, you’re not actually getting any data and the checksum will not match.

If you do: curl -O -L <url_of_current_version>

…then once you sha256sum the file, it will match Google’s DL page.

hi sir,

I installed go 1.15 and choose my home directory for setup . added below in profile file

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

But go command works after running the below command when ever I open terminal newly

source ~/.profile

is there any to run go command without refreshing profile every time

Explaining some Linux’s apps such as tar is amazing. It is great tutorial like the majority of D.O tutorials.

Great guide, thank you

Great post thank you greatly!

I had to add a GOBIN path value for go1.11.5.linux-amd64

Thanks, a very good user guide. I had used installer_linux but always timeout.

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!

Featured on Community

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