Setup a basic golang build on ubuntu

One of the most simple ways to get started with serving a golang web application is to set-up the golang build environment on the server. This allows you to push your code to version control on your local development environment and then sync it down and build it on the server before deploying it. It should be noted that this is not the recommended approach in many cases and using a service like code ship is my preferred method but can be slightly more complex to configure so to get you up and running this method can be very convenient.

The following article is based around a 512mb vps from digitalocean.com using Ubuntu 14.04 but with only minor variations should work with any provider or version of Ubuntu.

The first step is to sign up to digitalocean.com and then deploy a server in your desired location. Anywhere will do and a 512mb RAM instance is more than sufficient. If you need help with this process checkout their document site which can be very helpful if you are just starting out.

Once logged in the first thing is to ensure everything is up to date on the server by running

sudo apt-get update
sudo apt-get upgrade

Once everything is up to date we can then install the latest version of golang from golang.org. This can be achieved using package management but you wont get the latest version so based on the guide on the golang site http://golang.org/doc/install we do the following steps:

Change the directory to /usr/local/src to download golang

cd /usr/local/src

Download the archive of the latest version of the code https://golang.org/dl/

wget https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz

Unzip and install golang to /usr/local/go

sudo tar -C /usr/local -xzf go1.4.2.linux-amd64.tar.gz

These commands have now installed golang onto the server and the executables can be found at /usr/local/go/bin so we need to add this to the path. Edit /etc/profile and add the export statement to the bottom of the file which adds the executables to the path.

sudo nano /etc/profile

Add:

export PATH=$PATH:/usr/local/go/bin

If you run the source command it will import your path and you should be able to run the go command and see the output:

source /etc/profile

Then:

root@backup:~#go
Go is a tool for managing Go source code.

Usage:

        go command [arguments]

Its now time to set-up the local development environment to compile the code. So following the start of the guide http://golang.org/doc/code.html we issue the following commands to set-up our environment at /usr/local/src/go

mkdir -p /usr/local/src/go/src
mkdir -p /usr/local/src/go/bin
mkdir -p /usr/local/src/go/pkg

This will have created the required directories so we now add the workspace to the configuration of the server. Edit /etc/profile and add the export statement line to the bottom of the file which defines the golang workspace to compile code.

sudo nano /etc/profile

Add:

export GOPATH=/usr/local/src/go

If you then run the source command the workspace path will now have taken effect.

sudo source /etc/profile

Your environment to build your application for deployment has now been set-up so the next in the series of these articles will outline how to set-up the build and deploy script.


See also