Introduction

This will just be a quick post on how to install Golang on Ubuntu 1804. There will be no earth shattering knowledge bombs, it's more of a documentation post for myself. Others may or may not find it useful.

Code versions used for this lab

  • Ubuntu - 1804
  • Golang - 1.12.9

Download

Download the Golang package for your system and architecture.

cmd

curl -O https://dl.google.com/go/go1.12.9.linux-amd64.tar.gz
              

Install

Once downloaded, extract the tar file to install Golang.

cmd

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

This will create a binary here: /usr/local/go/bin/go

Configure

In order to effectively utilise go you will need to setup a couple of environment variables. The first one adds the the go binary to your $PATH

cmd

echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.zshrc
              

Note

I am using zsh for my shell, update the correct rc file based on the shell you are using.

The next one defines the root directory for your $GOPATH. Your $GOPATH is the location of your Go workspace. All of your go projects will get built under this workspace.

cmd

echo 'export GOPATH=$HOME/go' >> ~/.zshrc
              

Note

The default $GOPATH is $HOME/go. I am explicitly defining it for my setup, you may want to change the default location.

Finally source your rc file to load the variables into your environment and create the $GOPATH workspace directory.

cmd

source ~/.zshrc
mkdir $GOPATH
              

Verify Install

Confirm that Go is installed correctly.

cmd

go version

# output
go version go1.12.9 linux/amd64
              

First Go Program

Create a project directory under your $GOPATH workspace.

cmd

cd $GOPATH
mkdir hello && cd hello
              

Create a hello.go file.

file

// hello.go
package main

import "fmt"

func main() {
	fmt.Printf("hello, world\n")
}
              

Compile the project into a binary.

cmd

go build
              

A binary hello will be created, execute the binary and bask in your glory.

cmd

./hello

# output
hello from go!

              

Summary

That's it. Go installed, configured and ready to be stretched to its limits.

golang linux