Tutorial

How To Build Go from Source on Ubuntu 16.04

Published on January 4, 2017
How To Build Go from Source on Ubuntu 16.04

Introduction

Go is a modern, open-source programming language developed by Google. It’s simple and has a robust set of libraries and tools, which makes it easy to build reliable and efficient applications.

If you want to test your Go application against the latest version of the language, contribute to the Go code base, or have better Go version management, you’ll need to build Go from source. This tutorial will show you how to build Go, cover some practical considerations, and build a “Hello, World” test application.

Prerequisites

To follow this tutorial, you will need:

Step 1 — Installing Build Dependencies

Before starting, make sure your repository cache is up to date.

  1. sudo apt-get update

By default, Ubuntu doesn’t come with all the packages necessary to build Go, like its compilers, libraries, and tools. It’s easy to install the build and compilation dependencies by installing build-essential, a package which includes gcc for compiling C, g++ for compiling C++ and make, a build automation tool.

  1. sudo apt-get install build-essential

Once the installation finishes, you will have all necessary tools to build Go from source. Next, you’ll need the Go 1.4 binaries.

Step 2 — Installing Go 1.4.3

Building Go 1.5+ requires you to have Go 1.4. This is because the tool chains for Go 1.5+ are written in Go itself. You can use Go 1.4 or any point release (1.4.1, 1.4.2, or 1.4.3); in this tutorial, we’ll use Go 1.4.3.

Go binaries with SHA1 checksums can be found on Go’s download page. Download the file called go1.4.3.linux-amd64.tar.gz to your home directory.

  1. cd ~
  2. curl -O https://storage.googleapis.com/golang/go1.4.3.linux-amd64.tar.gz

While this file was downloaded from a genuine source, it’s still a good security practice to verify the integrity of what you’ve downloaded. This is most easily done by comparing the file hash provided on the downloads page and the hash of the downloaded file.

First, get the hash of the file you downloaded.

  1. sha1sum go1.4.3.linux-amd64.tar.gz

Compare the output from this command with the provided checksum on the Go download page.

  1. Output
    332b64236d30a8805fc8dd8b3a269915b4c507fe go1.4.3.linux-amd64.tar.gz

If the checksums match, you can unpack the files.

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

The x flag stands for eXtract, v tells tar to use Verbose output (i.e. to list the files being extracted), and f lets us specify the Filename.

Go 1.4 is now unpacked in a directory called go, but the Go build script looks for Go 1.4 binaries in ~/go1.4 by default. Rename the directory using the mv command.

  1. mv go go1.4

Now you have the necessary dependencies to build Go from source. If you want to move Go 1.4 out of your home directory to another location, you can follow the next step. If not, you can move on to Step 4 to clone the Go sources.

Step 3 — Relocating Go 1.4.3 (Optional)

When the Go build script runs, it will look for Go 1.4 based on an environment variable called GOROOT_BOOTSTRAP. If that variable is unset, which it is by default, the build script will assume that the go1.4 directory is in your home directory. You can use the GOROOT_BOOTSTRAP variable if you want to move Go 1.4 to another location. Here, we’ll move it to /usr/local/go1.4.

First, move the directory itself.

  1. sudo mv go1.4 /usr/local

Next, you need to set the path in your environment by modifying the ~/.profile file.

  1. sudo nano ~/.profile

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

~/.profile
. . .
export GOROOT_BOOTSTRAP=/usr/local/go1.4

Save the file and exit. To have this change take effect, refresh your profile.

  1. source ~/.profile

Next, you will clone the Go sources.

Step 4 — Obtaining Go Sources

The recommended location to store Go binaries is /usr/local, which is what we’ll use in this tutorial. However, you can store it wherever you prefer.

Move into /usr/local and use Git to clone the repository.

  1. cd /usr/local
  2. sudo git clone https://go.googlesource.com/go

We need to execute git clone with root privileges because /usr/local is owned by root. If you are cloning Go to a directory where your user has write permissions, you can run this command without sudo.

Note: Storing Go in a location other than /usr/local will require you to set up GOROOT environment variable. Learn more about Go environment variables in Step 7.

Once the clone completes successfully, it will create a go directory in /usr/local. Move into that directory.

  1. cd go

Before building, we need to choose which Go version we want.

Step 5 — Choosing a Go Version

Go sources have a different Git branch for every version available. The names of branches are:

  • master for the latest version in development
  • goversion for stable versions

Here, we’ll use Go 1.7.4, which means we want to use the go1.7.4 branch.

Warning: It’s not recommended to use the development version (i.e. the master branch) of Go in production. It’s not tested and there are there could be bugs. Development versions are good for application testing, but only use released versions for production.

To change branches, use the git checkout command. You need to run these commands with root privileges because /usr/local and /usr/local/go1.4 are owned by root.

  1. sudo git checkout go1.7.4

With this step completed, you have downloaded the Go source code of your preferred version. You’re ready to proceed onto the key part of this tutorial: building Go itself.

Step 6 — Building Go

To build go, you need to execute a bash script, src/all.bash, that comes with the source code. The script will check that you have all necessary dependencies, run some tests, and complete the build.

Execute the script, but note that if you changed the location of the Go 1.4.3 binaries, you will need to add the -E flag (i.e. sudo -E bash ./all.bash) to preserve environment variables.

  1. cd src
  2. sudo bash ./all.bash

The script will take a short time to finish. Once the build is done, you’ll see the following output:

Output of src/all.bash
ALL TESTS PASSED --- Installed Go for linux/amd64 in /usr/local/go Installed commands in /usr/local/go/bin *** You need to add /usr/local/go/bin to your PATH.

Warning: In some cases, the build can fail on Go 1.7 due to a time test failing. This is the result of a bug.

Failed test output
--- FAIL: TestLoadFixed (0.00s) time_test.go:943: Now().In(loc).Zone() = "-01", -3600, want "GMT+1", -3600 FAIL FAIL time 2.403s ... 2016/12/09 22:16:40 Failed: exit status 1

The workaround for this issue is to manually apply the bug fix. To do this, create a new branch and cherry pick the commit with the fix. Cherry picking in Git is process of applying changes from specific commit to your branch.

This bug was fixed in commit c5434f2, so add it to your newly created branch.

  1. cd ..
  2. sudo git checkout -b go1.7.4-timefix
  3. sudo git cherry-pick c5434f2973a87acff76bac359236e690d632ce95

After you do this, you can run the script again. (Don’t forget the -E flag if you moved the Go 1.4.3 binaries.)

  1. cd src
  2. sudo bash ./all.bash

Now that Go is built, you need to set some environment variables for it.

Step 7 — Setting Go Variables

Environmental variables are a powerful way to customize your Go installation. We will walk through most important and useful ones.

To start using Go, you need to add the Go binaries path, /usr/local/go/bin, to the PATH and GOPATH environment variables. GOPATH is where the Go application code and binaries are stored, which we’ll specify as ~/work, though you’re free to customize this.

Add these variables to the ~/.profile file. Open it with your favorite text editor:

  1. nano ~/.profile

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

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

If you installed Go in a non-default path, i.e. something other then /usr/local/go, you’ll need to define the GOROOT variable. In that case, add the following lines as well:

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

Save the file and exit. For these changes to take effect, refresh your profile.

  1. source ~/.profile

With this step completed successfully, you have a fully functional Go installation. Next, we’ll test it to be sure everything is working as expected.

Step 8 — Testing the Go Installation

A common way to test is that Go is working is to write and run a simple “Hello, World” application.

To do this, first we’ll create and move into a directory for the app files based on your GOPATH. You can choose whatever you like within your Go path, but as an example here, we’ll set up the kind of hierarchy you’ll need if you use GitHub to manage your code. This hierarchy will work even without a GitHub account, though.

  1. mkdir -p $GOPATH/src/github.com/your_github_username/test_project
  2. cd $GOPATH/src/github.com/your_github_username/test_project

Next, create a “Hello, World” Go file called hello.go.

  1. nano hello.go

Add the following code to the file:

hello.go
package main

import "fmt"

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

This example uses the fmt package, which implements I/O functions like Printf(), and defines the main() function which will print Hello World when the program is run.

Save it and exit the file, then compile it by using the Go command install.

  1. go install github.com/your_github_username/test_project

When compilation finishes, you can run it with the following command:

  1. hello

You’ll see Hello World! printed to the terminal, which means your Go installation is working.

Step 9 — Changing Go Versions (Optional)

This tutorial set up Go 1.7.4. If you want to use a different version, you need to change the active Git branch and rebuild Go.

To check your current Go version, you can use go version.

  1. go version
Output
go version go1.7.4 linux/amd64

As an example, we’ll switch to Go version to 1.6.4. This version is located in the go1.6.4 branch, so switch to it.

  1. cd /usr/local/go
  2. sudo git checkout go1.6.4

To make sure everything is up to date, run git pull to get the latest changes for the selected branch.

  1. sudo git pull origin go1.6.4

Now, run the build script as before, making sure to add the -E flag if you relocated Go 1.4 in Step 3.

  1. cd src
  2. sudo bash ./all.bash

This will take some time to finish. When the build is finished, you’ll see the following output:

Output of src/all.bash
ALL TESTS PASSED --- Installed Go for linux/amd64 in /usr/local/go Installed commands in /usr/local/go/bin *** You need to add /usr/local/go/bin to your PATH.

You already have Go variables set up in ~/.profile from Step 5, so this is all you need to do to change versions.

Conclusion

By building from source and setting up paths, you now have a great base for developing and testing Go applications or contributing to the Go code base.

To learn more, you can explore how to get started with Go or how to serve Go applications with Martini, which is a Go web framework. If you’re looking for inspiration to contribute to Go open source, you can read about the go-qemu and go-libvirt projects.

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

Default avatar

staff technical writer

hi! i write do.co/docs now, but i used to be the senior tech editor publishing tutorials here in the community.


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


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!

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