How to Install Google Go in Ubuntu

What do you get when you mix Python and C? According to Google, it’s Go – a new programming language developed in-house and later open sourced. Go was created by a small team inside Google, including the well known Ken Thompson, co-inventor of Unix and major influence on C. It was created out of a lack of satisfaction with existing languages, mainly the excessively (in the minds of Go’s developers) long compile times needed for other languages. With Go, even a very large and complex application can compile in a few seconds, often less. Additionally, Go has built in concurrency support, so you can code for multiple CPUs without resorting to outside libraries of unknown quality. While we don’t usually cover much programming here at MakeTechEasier, Go is such an interesting language that we just had to dip in a bit, and where better to start than by covering the system setup needed to get Go up and running on your Linux box.

Note: This guide will cover the steps needed to get the Go compiler working in Linux. It will not address how to write programs for Go, however if there is reader interest, we certainly could create such a tutorial.

Quick Overview

Go is a compiled language like C or C++, not interpreted like Python. This is course means that to do anything you need a Go compiler. Since Go is only officially available in source form, that means we’ll need to compile the Go compiler with gcc. When everything is done, you will have two Go compilers available, 6g and 8g. The only difference between them is that 6g creates 64-bit binaries and 8g creates 32-bit. Once you know which compiler you want, just code and compile for Go much like you would for gcc.

There are technically other compiler options, but 6g and 8g are the official compilers and will be used exclusively in this guide.

Update: A PPA is now available for Google Go. To install, open a terminal and type:

sudo add-apt-repository ppa:gophers/go
sudo apt-get update
sudo apt-get install golang

Preparing the System

There are a few quick easy things we’ll need to do in order to get our environment prepared. First, Go requires a few environment variables to be set in the shell so that it knows where to find and place files. You could type all the following commands directly into the terminal, but since some of them may be needed later, we’ll put them all in our .bashrc file.

Open your ~/.bashrc file, and enter the following lines at the end:

#This line will tell the Go installer where to place the source code before compilation
export GOROOT=$HOME/gosource
 
#With this line, you choose the architecture of your machine.  
#Those with 64 bit CPUs should enter "amd64" here.  
export GOARCH=386
 
#Your operating system
export GOOS=linux
 
#And now the location where the installer will place the finished files
#Don't forget to create this directory before installing
export GOBIN=$HOME/gobin
 
#Include Go binaries in command path
export PATH=$PATH:$GOBIN

Then at the command line type

source ~/.bashrc

to make sure it reads the file and creates those environment variables.

Installing Build Dependencies

We need a C compiler (such as gcc) along with some other utilities to create the Go compilers (6g and 8g). At your command prompt, run the following commands to install all needed build dependencies.

#If you normally use aptitude instead of apt-get, use that here as well
sudo apt-get install bison gcc libc6-dev ed gawk make python-setuptools python-dev build-essential
sudo easy_install mercurial

Getting and Compiling the Go Source Code

Now that we’ve got everything we need to install Go, we’re ready to get the code itself. To fetch the files, enter the following command:

hg clone -r release https://go.googlecode.com/hg/ $GOROOT

Your output should approximately match the following:

googlego-fetchsource

And now we’re ready to compile Go into a usable language. To begin, enter the following in your terminal:

cd $GOROOT/src
./all.bash

Remember, this part is gcc compiling Go, so this will not have the speed benefits associated with writing and compiling actual Go code and will likely take a few minutes.

If you get a message about $GOBIN, make sure you remembered to create the directory that you put in your .bashrc file. Similarly, for other errors, double check the variables you put in your .bashrc file.

Test Your Installation

Save the following into a file called test.go

package main
 
import "fmt"
 
func main() {
	fmt.Printf("Hello, Go!\n")
}

Now at the command line…

#These commands are for 32-bit builds
#Replace the "8" with "6" in all examples to build for 64.  
 
#Compile...
8g test.go
#Link
8l test.8
#And run..
./8.out

If all went well, you should see something like this:
googlego-hello

And you’re ready to start coding!

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Joshua Price

Josh Price is a senior MakeTechEasier writer and owner of Rain Dog Software