Installing Go on Ubuntu

When Google announced their newly developed system programming language Go, I decided to give it a try on my Ubuntu machine, the Mac would have to wait. The installation was supposed to be a walk in the park, but it wasn’t. I realized I didn’t have some other essential tools installed, and encountered one problem after another, frantically googling for solutions. So in this post I’ll detail the step by step process to install and run Go on your Ubuntu machine.

Step 1: Setting Up Environment Variables

Prior to installation you need to have these environment variables set up:

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

$GOOS – Name of the target operating system. Since this is Ubuntu the value would be ‘linux’

$GOARCH – Name of the target compilation architecture.  If you’re on a 32 bit x86 machine this would be ‘386’. Check out this section in Go online docs for other options.

$GOBIN – The location where binaries will be installed. You can set this to $HOME/bin or whatever path of your choice. Then you have to add this to your $PATH so that newly built Go-specific command such as the compiler can be found during the build.

Fire up the terminal and type the following command

sudo vi /etc/profile

This’ll open up the system-wide bash profile file in vi editor. You need to add the following lines at the end of the file.

export GOROOT=$HOME/go
export GOOS=linux
export GOARCH=386
export GOBIN=$HOME/bin

Now add $GOBIN to your $PATH. Open .bashrc in your $HOME directory

sudo vi $HOME/.bashrc

and add the line

export PATH=${PATH}:$GOBIN

Restart the machine for these changes to take effect.
Reload the files for the changes to take effect(thanks peter vahlu)

source /etc/profile
source ~/.bashrc

Step 2: Install Mercurial and clone Go repository

Google uses Mercurial to store Go source code, so you have to install it and fetch the repository.

Since 1.0, Mercurial has been installable by easy-install. So you need to get the python setuptools, header files and other essential  tools installed  first.

sudo apt-get install python-setuptools python-dev build-essential

Now install Mecurial

sudo easy_install -U mercurial

This part was quite confusing. Mercurial was throwing up some errors when I tried to clone Go’s repo.

*** failed to import extension hgext.hbisect: No module named hbisect

The extension bisect is a built-in command since version 1.0 and so should not be used. If you get this error, open the configuration file(/etc/mercurial/hgrc.d/hgext) and remove/comment out the line hbisect=

Make sure the $GOROOT directory does not exist or is empty. Then check out the repository:

hg clone -r release $GOROOT

Step 3: Build Go from source

The Go tool chain is written in C. To build it, you need to have GCC, the standard C libraries, the parser generator Bison, make and the text editor ed installed.

sudo apt-get install bison gcc libc6-dev ed make

Next, build Go from source

cd $GOROOT/src


If all.bash runs without trouble, it’ll finish by printing

--- cd ../test

N known bugs; 0 unexpected bugs

Where N is the number of bugs, changes from release to release.

You now have GO installed on your Ubuntu. Happy coding!

Further Reading

  1. The installation page at Go website.
  2. Article on installing Mercurial on Ubuntu and dealing with its tantrums.

19 thoughts on “Installing Go on Ubuntu

  1. Nicely written. Will try today. As yesterday’s attempt failed while building. Installing and fetching all the files were fine. But build had two errors.

  2. you can source the files instead of restarting the machine:
    source /etc/profile
    source ~/.bashrc
    thx for the great post

  3. @Rubayeet – Thanks man, Really a nice one. Well, I haven’t tried it in Mac yet. been busy with lots of other stuffs. But I will give it a shot very soon 🙂

    Thanks again for nice works. Keep it up.

  4. @petar – Thanks for the tip! I’ve updated the post.

    @hasin – My pleasure! Coming from you it means a lot. Good luck with Go.

  5. Pingback: Impressions on Go language « Conscious

  6. Should be “build-essential” not build-essentials. I copy/pasted the install command and it couldn’t find the package.

  7. Can someone help me please? I am trying to install go and every time during the ./all.bash step i get the following warning:
    /home/charlie/go/src/cmd/gc/yerr.h:17: Fehler: »loadsys« ist hier nicht deklariert (nicht in einer Funktion)
    make: *** [subr.o] Fehler 1
    make: *** Warte auf noch nicht beendete Prozesse…
    please help!!

  8. sudo vi $HOME/.bashrc

    Should be

    vi $HOME/.bashrc

    Thanks for the writeup. I shared a link to your writeup
    with my students at SJSU.

  9. Pingback: Blog stats 2010 « Glorified Geek

  10. it started whining about ot getting into /usr/local/include/
    had to do with rights, cleared it by providing admin as group for directory, so it could see there was no amd64_x86_linux( or somthing like that) directory there.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s