Skip to content
Blog How to Install Google’s Go language on Ubuntu Linux

How to Install Google’s Go language on Ubuntu Linux

Go is an open source project, distributed under a BSD-style license. This document explains how to check out the sources, build them on your own machine, and run them.

First of all you need to set some Environment variables

The Go compilation environment depends on three environment variables that you should set in your .bashrc or equivalent, plus one optional variable:

$GOROOT
The root of the Go tree. Typically this is $HOME/go but it can be any directory.
$GOOS and $GOARCH

The name of the target operating system and compilation architecture. Choices for $GOOS are linux, darwin (Mac OS X 10.5 or 10.6), and nacl (Native Client, an incomplete port). Choices for $GOARCH are amd64 (64-bit x86, the most mature port), 386 (32-bit x86), and arm (32-bit ARM, an incomplete port). The valid combinations are linux/amd64, linux/arm, linux/386, darwin/amd64, darwin/386, and nacl/386.
$GOBIN (optional)
The location where binaries will be installed. If you set $GOBIN, you need to ensure that it is in your $PATH so that newly built Go-specific command such as the compiler can be found during the build. The default, $HOME/bin, may already be in your $PATH.
Note that $GOARCH and $GOOS identify the target environment, not the environment you are running on. In effect, you are always cross-compiling.
Set these variables in your .bashrc. For example:
$ export GOROOT=$HOME/go
$ export GOARCH=amd64 (replace amd64 with your system architecture eg: arm, 386
)
$ export GOOS=linux
$ export GOBIN=
$HOME/go/bin
$ export PATH=$PATH:$GOBIN

Add the PATH variable to the .bashrc file only if you are planning to use go

regularly.
Double-check them by listing your environment.
$ env | grep '^GO'

Install Pre-requisites

$ sudo apt-get install bison gcc libc6-dev ed make 
$ sudo apt-get install python-setuptools python-dev 
$ sudo apt-get install mercurial

Fetch the repository

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

Install Go


$ cd $GOROOT/src
$ ./all.bash

Now wait for some time. The compilations will proceed and will be
completed with the following message

--- cd ../test
N known bugs; 0 unexpected bugs
 

Test Go

Create the following program in your favourite editor and save it as hello.go

package main

import "fmt"

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

compile it using

$ 6g hello.go

6g is the Go compiler for amd64; it will write the output in hello.6. The ‘6’ identifies files for the amd64 architecture. The identifier letters for 386 and arm are ‘8’ and ‘5’. That is, if you were compiling for 386, you would use 8g and the output would be named hello.8.

To link the file, use

$ 6l hello.6

6l is the Go linker for amd64; it will write the output in 6.out. The ‘6’ identifies files for the amd64 architecture. The identifier letters for 386 and arm are ‘8’ and ‘5’. That is, if you were linking for 386, you would use 8l and the output would be named 8.out.

to run it

$ ./6.out ( or ./8.out )

To build more complicated programs, you will probably want to use a Makefile. There are examples in places like $GOROOT/src/cmd/godoc/Makefile and $GOROOT/src/pkg/*/Makefile.

1 thought on “How to Install Google’s Go language on Ubuntu Linux”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.