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.
Now add $GOBIN to your $PATH. Open .bashrc in your $HOME directory
sudo vi $HOME/.bashrc
and add the line
Restart the machine for these changes to take effect.
Reload the files for the changes to take effect(thanks peter vahlu)
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
Make sure the $GOROOT directory does not exist or is empty. Then check out the repository:
hg clone -r release https://go.googlecode.com/hg/ $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
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!