Setup Golang on an Ubuntu server

In a previous post I talked about how I setup a Ubuntu server using LXC on my QNAP NAS server. The plan was to host a small web application I am developing using the Go programming language. In this post I’m going to outline the steps I used to install Go on my server but these same steps should apply to any Ubuntu system. Of course you can use the official installation instructions on the Getting Started page. These steps include some extra detail for those of you running on bare bones installation over ssh, so if the official instructions aren’t working, maybe these can help.

First ssh into your server. If you following along from the last post you should be in, if not use ssh from the terminal on your desktop like this.

> ssh username@serveraddress -p hostport

Once you’re in it’s time to get hold of a copy Go. First though install a tool called curl which we’ll use to download what we need. Run apt-get update if you haven’t done that recently and then install curl using the following commands.

> sudo ap-get update
> sudo apt-get install curl

Also while we’re installing stuff, install nano if you don’t already have it, so you have something to edit text files with (VIM and EMACS are horrible IMHO).

> sudo apt-get install nano

Once curl is installed you can use it to download the latest Go archive from the golang website. Copy the link for the latest Linux binaries and the download them using curl. Here’s how I downloaded Go version 1.9.

> sudo curl -O

You should now have the binaries as an archive in your current folder. Unpack them and put them in /usr/local which is where these sort of things should live.

> tar -xvf go1.9.linux-amd64.tar.gz
> sudo mv go /usr/local

Next we need to setup our environment so we can actually use the Go tools. Open your user profile script in nano to edit it.

> nano ~/.profile

Then add the following lines to the bottom of the file. These set the GOROOT variable that is the location of your Go installation and adds its bin folder to PATH so you can launch the “go” command from anywhere. We also set the GOPATH variable which locates your Go workspace in your home directory.

export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=$HOME/go

Once the lines are added, press ctrl+x to exit and “y” when prompted to save your changes.

Now create the workspace folder, which is where all your Go development should done.

> mkdir ~/go

You can test this by running through any basic Hello World tutorial. The official Test Your Installation instructions should be good enough.

That’s it, that’s how I got my installation working. In a future post I may demonstrate how I got my web server running on this infrastructure. Thanks for reading.