Tutorial

How To Install Go 1.6 on Ubuntu 14.04

Published on December 4, 2015
How To Install Go 1.6 on Ubuntu 14.04
Not using Ubuntu 14.04?Choose a different version or distribution.
Ubuntu 14.04

Introduction

Go is a modern programming language developed by Google that uses high-level syntax similar to scripting languages. It is popular for many applications and at many companies, and has a robust set of tools and over 90,000 repos. This tutorial will walk you through downloading and installing Go 1.6, as well as building a simple Hello World application.

Prerequisites

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

Once connected, update and upgrade the Ubuntu packages on your server. This ensures that you have the latest security patches and fixes, as well as updated repos for your new packages.

  1. sudo apt-get update
  2. sudo apt-get -y upgrade

With that complete, you can begin downloading the latest package for Go by running this command, which will pull down the Go package file, and save it to your current working directory, which you can determine by running pwd.

  1. sudo curl -O https://storage.googleapis.com/golang/go1.6.linux-amd64.tar.gz

Next, use tar to unpack the package. This command will use the Tar tool to open and expand the downloaded file, and creates a folder using the package name, and then moves it to /usr/local.

  1. sudo tar -xvf go1.6.linux-amd64.tar.gz
  2. sudo mv go /usr/local

Some users prefer different locations for their Go installation, or may have mandated software locations. The Go package is now in /usr/local which also ensures Go is in your $PATH for Linux. It is possible to install Go to an alternate location but the $PATH information will change. The location you pick to house your Go folder will be referenced later in this tutorial, so remember where you placed it if the location is different than /usr/local.

Step 2 — Setting Go Paths

In this step, we’ll set some paths that Go needs. The paths in this step are all given are relative to the location of your Go installation in /usr/local. If you chose a new directory, or left the file in download location, modify the commands to match your new location.

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 PATH=$PATH:/usr/local/go/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 PATH=$PATH:$GOROOT/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

Now you can point Go to the new workspace you just created by exporting GOPATH.

  1. export GOPATH=$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 in the content 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 file will show “Hello, World” if it successfully runs, which shows that Go is building files correctly. Save and close the file, then compile it invoking the Go command install.

  1. go install github.com/user/hello

With the file compiled, you can run it by simply referring to the file at your Go path.

  1. sudo $GOPATH/bin/hello

If that command returns “Hello World”, then Go is successfully installed and functional.

Conclusion

By downloading and installing the latest Go package and setting its paths, you now have a Droplet to use for Go development.

Next, be sure to [learn some Go tips from our development team] (https://www.digitalocean.com/company/blog/get-your-development-team-started-with-go/), and how to host your project using Martini. The Go development landscape is growing every day, and we hope you help make it robust and exciting.

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

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?
 
10 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!

I’m confused as why you’re using sudo to curl and tar a file… seems like bad practice to run everything under sudo if you don’t need to. You could do some major damage to your system if you continue to use this practice.

One of the best explanations of install go!!!

Best of best!

I’d like to offer an alternative path to the latest stable (I’ve just used on trusty for 1.7.3 so those are the URL’s I’ll use)

wget https://storage.googleapis.com/golang/go1.7.3.linux-amd64.tar.gz
tar -xvf go1.7.3.linux-amd64.tar.gz
sudo mv go /opt/go
sudo cat > /etc/profile.d/go.sh <<EOF
export GOROOT=/opt/go
export PATH=$PATH:$GOROOT/bin

EOF

then logout, login and you are done with the latest go installed for all users (the benefit of this script is it can be run unattended if user is in admin group with the following /etc/sudoers entry last)

%admin ALL=(ALL) NOPASSWD:ALL

Very nice tutorial, but there are a few too many un-needed uses of sudo. You only need sudo for the apt-get {update,upgrade} and the sudo mv go /usr/local/go commands. Editing those files and curling/untaring the tar.gz don’t need sudo.

I would suggest using real deb packages for Go:

$ sudo apt-add-repository ppa:ubuntu-lxc/lxd-stable
$ sudo apt-get update
$ sudo apt-get install golang

This will install Go 1.6 as of today.

Third and hopefully final update but no guarantee. New Ubuntu users should note that the file ~/.profile is not loaded automatically by Ubuntu. The file that you should be editing with your path changes is ~/.bash_profile otherwise next time you start up Ubuntu you’ll have go issues again.

For anyone else reading this and wondering why it’s not working on Ubuntu. You need to unset GOROOT, to fix the “cannot import absolute path” error and you need to make a ‘bin’ folder inside your $GOPATH which is /home/user/work in this tutorial. Just ‘mkdir bin’ and you’ll fix the error “no install location (GOBIN not set)”

There were a couple issues I had with this guide:

  1. /usr/local is not part of Ubuntu 14.04’s default path:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

  1. If this is a fresh install of Ubuntu for go development like mine, then ~/.nano_history will be owned by root and give errors each time it’s subsequently run without sudo. sudo chown username ~/.nano_history resolves that issue

  2. Nowhere in the guide is there a CD command or does it tell you which directory to execute from. This could be very confusing for newer people. I lost context towards the bottom when I was moving around on my own. The only indication of where to be is that the first step is to SSH in and there are no CD commands later so you should be in the home directory I’m assuming. But again, assuming. There are no CD commands or working directory confirmation notices in the guide.

Right now I’m stuck because:

$ go install github.com/user/hello
cannot find package "fmt" in any of:
(from $GOROOT)
(from $GOPATH)
package github.com/user/hello
imports runtime: cannot find package "runtime" in any of:
(from $GOROOT)
(from $GOPATH)

This comment has been deleted

    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.