Thursday, November 12, 2009

Installing Google's Go Language on Ubuntu

GO language promoted by google is a new system programming language said to be expressive, concurrent, garbage-collected. The language is still very young and there is no ready made package available for ubuntu. You can install it and try out the features from the version control repository of go .

Let us see how you can do this. I did it on ubuntu 9.04.

1) Install Pre-requisites on ubuntu.

You need gcc and some supporting software like bison to compile go. Install the following.
 $ sudo apt-get install bison gcc libc6-dev ed.

The go repository uses mercurial version controls system .
Install mercurial with the following command.

$ apt-get install mercurial

2 Set up the environment variables.

I am assuming that you are planning to install go under a folder
named go in your home directory.

In my case it is /home/fermi/go

Create it with
$mkdir go

Now create another direcory inside go .

$ mkdir go/bin

The above directory will contain your go compiler.
Next, you have to set u several variables.

$ export  GOROOT=/home/fermi/go/hg
$ export GOOS=linux

$ export GOARCH=386
$ export GOBIN=/home/fermi/go/bin

( Note: You need not create the folder hg. You can also add the above
four lines along with the PATH variable below to the .bashrc file
if you are planning to use go regularly.)

Update your PATH variable .

$ export PATH=$PATH:$GOBIN

Check the environment variables with.
 $ env | grep '^GO'

I got like this.

GOBIN=/home/fermi/go/bin/
GOARCH=386
GOROOT=/home/fermi/go/hg
GOOS=linux

3) Grab the source code from mercurial

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

It created
/home/fermi/go/hg and downloaded several files to it.
To build the Go distribution run.

4) Compile 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

5) Test go
Your go language system is ready to go.:D
Now let us write a hello world program and test it.

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")
}

To compile hello.go run.
 $ 8g hello.go

The above command produced and intermediate file hello.8.
Next you have to link it.
$ 8l hello.8

The executable is placed as 8.out. Finally run the executable.
$./8.out

14 comments:

roskoto said...

Thank you for these instructions. I done them successfully and have got 630k executable. That is too big for such a simple program. Do you share the same observation?

Fermi Level said...

@roskoto
I am getting around 581k See this.
-rwxr-xr-x 1 fermi ferm 581702 2009-11-12 23:09 8.out

Oscar / Sitmex said...

Hi, I followed your instructions and everything went well, now, when I'm trying to compile 6g can't be found.

everything listed in the post was installed, some were already in place, By the way, Using Ubuntu 9.10...

Oscar / Sitmex said...

never mind my previous comment, instead of compiling it with 6g, 8g was available.

So I did it, and it worked well.

Fermi Level said...

@Blogger Oscar / Sitmex
Thanks . It was a typo in the post. Corrected now

Daniel Gianni said...

No work!!!

this is error message:


http://pastebin.com/m4b8fa22f

rtalcott said...

Thank You! I tried to compile with 8g...it was not there but 6g was. Only change I made was to use
GOARCH=amd64

rt

rtalcott said...

OK....6g is for 64 bit...that's why 8g was not there!
rt

Vladimir said...

I had to:

1. add environment variables to /etc/profile
2. chown -R for bin and hg folders

After these all work.

tsakf said...

Good info about the anxious to try Go on Ubuntu. Added the article to my library.
Http://fosslib.tsakf.net/record/291

Anonymous said...

Get all this is as one bash file
http://digitalpbk.com/2009/11/installing-google-go-linux-ubuntu

Anonymous said...

helemese, kompilovani a slinkovani:
8g hello.go
8l hello.8

to mi velmi pripomina plan9 zpusob
a rob pike co delal na plan9 odesel
do google, ze by v tom mel prsty?!

http://plan9.bell-labs.com/sys/doc/comp.html

Anonymous said...

hey look, compiling and linking:
8g hello.c
8l hello.8

looks like compiling and linking in plan9
operating system and rob pike worked for plan9 and now he is in google :-)

http://plan9.bell-labs.com/sys/doc/comp.html

Taylor Braun-Jones said...

PPA now available:

http://comments.gmane.org/gmane.comp.lang.go.general/33122