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:

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:

Then at the command line type

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.

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:

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:

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

Now at the command line…

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

And you’re ready to start coding!

24 comments

  1. In short, compiling builds the source into library files (somewhat like Windows' DLLs), and linking combines them into an executable program.

    If you like, you can create a small shell script and call it something like “gobuild” that will compile, link, and run your program in one step.

  2. Good tutorial. I will add a link to my site http://www.gohelp.wordpress.com. Also, I give extensive instructions for making Gedit into a Go IDE. It can be made quite capable with syntax highlighting, compiling, running, formatting, version control, etc.

    To answer the previous comment, '8.out' is the default name of the compiled and linked file in the example. I don't know why the Go team chose to call the runnable '.out', probably so that it does not conflict with other extensions. './8.out' runs it in the current directory.

    I will note one caveat. One should consider putting the Go environment variables in '%HOME/.profile'. If they are put in '%HOME/.bashrc' they will not be seen by some scripts, especially non-interactive scripts. It will save you some grief.

    John

  3. It worked well , but I think you did not mention the need of creating:
    gobin in the home directory.

    Thanks a lot anyway

  4. 'a.out' is standard output of linkers (if not specified with -o)
    for go it makes sense to call it x.out (where x corresponds to whatever arch is used and hence compiler chain)

    now why was there 'a.out' at the beginning of time?

  5. from Plan9 linker documentation (which GO is based on):
    –o out Place output in file out. Default is O.out, where O is the first letter of the loader name.

  6. Keep getting an error during install:

    > /home/patrick/gosource/pkg/linux_386/runtime.a(_go_.8): object is [linux 386 release.r56 9424] expected [linux 386 release.r59 9199+]

    Any ideas?

    • It appears the working copy in your repository is not up to date.

      You can update it by executing these commands:

      % cd $GOROOT
      % hg pull
      % hg update release

      If you need a specific release, you can specify it with:

      % hg update release.rNN

      (where NN is the release number)

  7. Thx…but can you tell me where I will find the gocode Binary then…i need it for installing this one https://github.com/DisposaBoy/GoSublime#readme but i am just ways to silly atm to find it :D

    • I’ve never tried GoSublime, but it looks like there’s a link to the GoCode source in that readme.  Have you tried installing it that way?  

  8. Well…looks like my last comment was deleted so…can you tell me where I will find the gocode Binary, when i do the installation like this? I need the absolute path of the gocode binary…

  9. thanks for this, I thought I was having a path issue, turns out I just hadn’t finished a full install(both compiler and linker). 

  10. i would like to run go lang on windows and mac. And i am not comfortable with bash shell or terminal (i don’t know what to call).

  11. Ran the scripts, but it can’t find 8g and 8l for some mysterious reasons… The $GOROOT and $GOBIN env. vars have been set…

    • Go has changed quite a bit since this article was released, you might have better luck with a more updated guide.  

Comments are closed.

Sponsored Stories