Installing Go on Ubuntu 18.04

Go is a Google Developed modern open-source programming language. Go also known as Golang and offers a robust set of libraries. Popular applications like Docker, Graphana etc. are written in Golang. In this tutorial, you are going to learn how to install Go on Ubuntu 18.04 and to build a simple real-world application.

Prerequisites

Before you start to install Go on Ubuntu 18.04. You must have the root user account credentials of your system.

Download and Extract Go on Ubuntu

The current latest version of Golang available on the official website is 1.11.5 at the time writing this tutorial. You can get the current latest version URL by visiting their download page.

First, go to home directory and download Go tarball by using curl command:

cd ~ && curl -O https://dl.google.com/go/go1.11.5.linux-amd64.tar.gz

Now verify the downloaded tarball by using the following command:

sha256sum go1.11.5.linux-amd64.tar.gz

The output should look like this:

cde4af177ef9f218f51f5e12041f43004f9fa1b0e45d3b647c252aba04901035 go1.11.5.linux-amd64.tar.gz

The hash value printed in above output should match with the hash value provided on download page.

Now extract the downloaded tarball using following command inside /usr/local directory.

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

Set Go Path

Now you should set PATH in order to find Go executable binaries by the system.

Open /.profile file using below command:

sudo nano ~/.profile

And paste following lines inside it:

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

Now save and exit the file then reload the profile running below command:

source ~/.profile

Verify Go Installation

To confirm and verify the go installation type following command:

go version

the output should be:

go version go1.11.5 linux/amd64

Build Hello world Program in Go

Here we will build a simple Hello World program in Golang. Use following steps to build the program.

Create a new directory for go workspace by using following command, to learn more about go workspace directory visit this page:

mkdir $HOME/work

Now create src directory inside it typing below command:

mkdir -p $HOME/work/src/hello

Now create hello.go file inside src/hello directory using below command:

sudo nano ~/work/src/hello/hello.go

With following content:

package main import "fmt" func main() { fmt.Printf("Hello, World

") }

Now go to the

/work/src/hello

directory:

cd ~/work/src/hello

And build the program using typing:

go build

You can run the program using following command:

./hello

The output should be:

Hello, World

Conclusion

You have successfully learned how to install Go on Ubuntu 18.04. If you have any queries please don’t forget to comment below.