Go is an open-source modern programming language that was first released by Google in 2009. The creators of Go sought to make it the first mainstream language characterized by efficient compilation, efficient execution, and ease of programming. Like scripting languages, Go uses syntax that is platform agnostic. Support for multiple processors and for networking are built into the language. Its efficiency, ease of use, and high-level syntax have led to Go’s use in a diverse set of applications and environments. Go has been deployed in over 130,000 GitHub repositories. This article will guide you through installing Go 1.6 on Ubuntu 14.04.
Getting Started
Confirm that you have the following before you follow this guide:
• A node (cloud server or dedicated server) with Ubuntu 14.04 installed
• Root access to the node or one sudo non-root user
Below, we assume that all commands are performed by the root user. However, we recommend that you run your scripts and applications from inside a regular user, using sudo privileges.
The official repository for Ubuntu 14.04 contains Go 1.2.1. You may obtain the most recent version by installing Go 1.6 from the source code.
Step-by-Step Guide to Installing Go 1.6
Before you install Go, make sure that your system is up to date. Execute the commands apt-get update and apt-get dist-upgrade.
apt-get update && apt-get upgrade -y
The update ensures you will install the most recent packages available. The upgrade will install the most recent security patches and fixes.
Once your system has been updated, you have two basic options for installing Go:
• Install Go 1.2.1 from the Ubuntu 14.04 repository
• Install Go 1.6 from source
Installation from the repository is extremely simple and requires relatively little knowledge. If you are confident you have the necessary skills to do so, you may obtain a more recent version of Go by installing it from the source code.
Install From Repository
To install from the repository, use the apt-get install command as follows:
apt-get install golang-go
Check the version of Go that has been installed with the command:
root@go-node:~# go version
go version go1.6.2 linux/amd64
Go is installed. You may skip the sections entitled “Install from Source” and “Configure Go Paths” and go directly to the section entitled “Testing Go.”
Install From Source
To install from source navigate to the root folder and download the source code for Go using curl as follows:
cd ~
wget https://storage.googleapis.com/golang/go1.6.linux-amd64.tar.gz
The file you have downloaded will have the extension .tar.gz. Use tar with the -xvf extension to extract the contents of the downloaded file. The following command will expand the gzipped file, open the resulting tar file, and create a folder that will contain all the files that had been stored in the tar file:
tar -zxvf go1.6.linux-amd64.tar.gz
Use the following command to change the permissions associated with the Go folder and its contents before you place that folder in your $PATH:
chown -R root:root go
Move the resulting secure Go directory to /usr/local.
mv go /usr/local
Configure Go Paths
By default, the Go package will be in /usr/local. Go will be in your $PATH for Linux.
If you choose to install Go in another location, you will need to make sure that location is in your $PATH. When you set paths related to Go, replace /usr/local with whatever location you chose.
Now, we will set the paths needed to make Go useable.
Create a directory to which you will deploy your applications, e.g. ~/go/bin:
mkdir /root/app
Navigate to your Go folder and create a .profile file using a text editor such as nano:
nano ~/.profile
At the end of the .profile file, set the Go root path and the path to the folder where you will deploy your applications by adding the following lines:
export GOPATH=$HOME/app
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
Save and close the file. Activate the changes you have made by running:
source ~/.profile
Go is installed and all related paths have been set.
Testing Go
To test whether Go is functional, create a simple “Hello World” file.
mkdir -p $GOPATH/src/hello
nano $GOPATH/src/hello/hello.go
Paste the content below into the “Hello World” file.
package main
import "fmt"
func main() {
fmt.Printf("Hello, world.\n")
}
Save and close the file. Invoke the Go command install to compile the program:
go install $GOPATH/src/hello
If the following command returns “Hello World,” Go is properly installed and ready to use.
./root/work/bin
Conclusion
Go 1.6 is now installed on your server. You are free to develop using Go on Ubuntu 14.04.