Linux install guide for Ubuntu and other Debian systems
Start Here…
These instructions are written for installing Go on Ubuntu or another Debian derived Linux Distribution. If you are using a different Linux distribution then you will need to adapt these instructions.
Installing Go is straightforward. We are going to show you how to install the latest version of Go.
We need to install Go using the Terminal also called the shell. So your first task is to start a new Terminal.
Notes
You need to type the commands in the boxes like this:
ls
Download and Install the Programs that Go needs
Go needs four other programs to work correctly. These programs must be installed on your computer first. These programs are called dependancies because another program, in this case Go, depends upon them to work correctly.
The four programs are “Git”, “Mercurial”, “Bazaar” and “Subversion”. They are all version control tools.
To download and install these on your computer type the following command into your terminal.
sudo apt-get install git mercurial bzr subversion
Download Go
Next you need to download Go itself. You need to visit Go’s download page. The version you should download is the 64-bit Linux archive. Currently the latest Go version is 1.4.2. if there is a later version there when you read this you should download that instead.
Notes
Once you have downloaded the file then it is time to install Go.
Installing Go
Now you have Go this is really easy. First you need to change directory into your Downloads directory where your web browser download Go to.
cd ~/Downloads
ls
sudo tar -C /usr/local -xzf go1.4.2.linux-amd64.tar.gz
go1.4.2.linux-amd64.tar.gz
is the filename of the
file you downloaded. If your filename is different you need to make sure you use
the filename of the file you downloaded and not go1.4.2.linux-amd64.tar.gz
Now you need to tell your Terminal where to find Go. To do this you need to edit
a file called .bashrc
in your home directory. We’ll show you how to do this using the
built-in text editor called gedit, but you can use any text editor you prefer to follow
these steps. So, from the Terminal
by typing
gedit ~/.bashrc
Be Careful
.bashrc
file is an important file. It controls how your Terminal works.
We are going to add two lines to the bottom of the file. Be careful not to
change any of the other lines in the file. If you think you have made a mistake
quit gedit without saving the file. You can then start gedit again with the
original .bashrc
by retyping the above command.
.bashrc
file you need to add these two lines.
# Tell the terminal where to find Go
export PATH=$PATH:/usr/local/go/bin
Next you need to tell your terminal to read your updated .bashrc
file. You do
this with the source command.
source ~/.bashrc
go
command
go version
go version go1.4.2 linux/amd64
Notes
Now you need to set up your workspace.
Setting up a Go Workspace
Go code must be kept inside a workspace. A workspace is a directory on your computer. A directory is the correct name for the folders that you see when you use the file manager. Inside the workspace directory there must be three more directories.
src
contains the Go source code, organised into packages. There is one directory per package. More on packages later.pkg
contains the package objectsbin
contains your executable programs.
The go
tool builds source packages it finds in the src
directory and
installs the resulting binaries in the pkg
and bin
directories.
This is not a complicated as it sounds. Creating a work space is easy. You can call your Go workspace anything, but we will choose to call ours “go-workspace”. To create your workspace you need to type the following into you Terminal
cd ~
mkdir go-workspace
cd go-workspace
mkdir src
mkdir pkg
mkdir bin
ls -l
drwxr-xr-x 2 pi pi 4096 Feb 11 23:33 bin
drwxr-xr-x 3 pi pi 4096 Aug 14 2014 pkg
drwxr-xr-x 11 pi pi 4096 Jan 21 16:13 src
We are nearly finished now. There is just one more step to…umm…go. We need to tell
Go where to find your workspace. To do this we need to edit your .bashrc
file
again. So in you terminal type
gedit ~/.bashrc
GOAPTH
environmental variable. As before go the the
end of the .bashrc
file and add these five lines. These five lines should
appear below the lines we added earlier.
# Set the value of the GOPATH environmental variable to the go-workspace
# This tells go which directory is your workspace
export GOPATH=$HOME/go-workspace
# Add the workspace's bin directory to the PATH
export PATH=$PATH:$GOPATH/bin
The last step is to tell your terminal to read your updated .bashrc
file. Once
again we do this with the source command.
source ~/.bashrc
You now have Go installed on your computer. Now you need to test it to make sure that everything is working. To find out how to do this you need to read testing your install guide.