How to Install Go Lang on Ubuntu

By on July 10th, 2020

Go is an open-source, compiled programming language designed and developed at Google. Google, in the year 2007, developed Go and launched in November 2009. The Go programming language is also known as Golang, and it is simple, efficient, and reliable to build software. Go language is a statically typed language similar to that of C. When compared to C, it provides features like memory safety, CSP-style concurrency, structural typing, and garbage collection. It also provides a rich standard library and many built-in types, such as key-value maps and variable-length arrays. In this tutorial, We are going to see How to install the Go program on Ubuntu.

The two significant implementations of Go are Google’s self-hosting compiler toolchain and a GCC front-end called gccgo. Go is a general-purpose language designed for system programming, and in Go, the programs are constructed using packages to manage the dependencies efficiently. A Go program can vary from 3 to million lines, and written into a text file with ‘.go’ extension. In a Linux system, you can use any of the text editors, such as vi, vim, or nano to write the Go program. Some of the features of Go programming are listed below:

  1. The compilation time of a Go program is fast.
  2. Go program offers dynamic-typing capability and type safety.
  3. The inbuilt concurrency support of Go makes the process of programming lightweight.
  4. Go programs are simple, reliable, brief, and safe.
  5. Go program supports interfaces and type embedding.

Install Golang on Ubuntu 18.04

Some of the examples of applications written in Golang are Docker, Kubernetes, Hugo, etc. Before you start to install Go on your Ubuntu server, make sure that you have root user account credentials. To install and setup Go on Ubuntu, follow the below steps.

1) Download and Extract Go

Download the latest version of Go tarball by using the curl command. The current latest of Go is 1.13.4, and you can check the latest available version by visiting their download page.

Go to the home directory and download the latest Go tar file by using the following command.

$ cd ~
$ curl -O

Run the following command to verify the downloaded file.

$ sha256sum go1.13.4.linux-amd64.tar.gz

The above command prints the hash value and version installed.

692d17071736f74be04a72a06dab9cac1cd759377bd85316e52b2227604c004c go1.13.4.linux-amd64.tar.gz

The hash value printed in the output should be similar to that provided on the download page.

3) Now, extract the downloaded file using the following command inside the ‘/usr/local’ directory.

$ tar -C /usr/local -xyz go1.13.4.linux-amd64.tar.gz

 2) Set the Go Path

After the installation, you need to set the path to find the Go executable binaries by the system.

1) Open the /.profile file.

$ nano ~/.profile

2) Copy and paste the below lines at the end of the /.profile file

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

3) Now, save and close the file.

4) Reload the /.profile file by running the following command.

$ source ~/.profile

3) Verify the Installation

After the verification, if you want to check the version details of Go, run the following command.

$ go version

This above command should print the output similar to the one shown below.

go version go1.13.4 linux/amd64

4) Build a Basic Program in Go

To build a simple and basic Welcome program in Golang, follow the below steps.

First, you need to create a Go workspace directory. A workspace directory is a hierarchy of directories with two other directories as its root. The two other directories that act as the root of the workspace directory are src and bin. The src directory contains all the source files of Go, and the bin directory contains all the executable commands. The Go program builds and installs binaries to the bin directory. You can create a Go workspace directory by running the following command.

$ mkdir $HOME/work

Now, create the src directory inside the ‘work’ directory.

$ mkdir $HOME/work/src

Create a ‘welcome’ directory inside the ‘src’ directory. You can use a -p option with mkdir to make sure that the directory gets created even if the parent directory is not available in the system. This command creates the parent directory if it does not exist and then creates the desired directory.

$ mkdir -p $HOME/work/src/welcome

Next, create the go executable file named welcome.go inside the src/welcome directory by running the following command.

$ nano ~/work/src/welcome/welcome.go

Add the following content or code into the welcome.go file.

package main
import "fmt"

func main() {
    fmt.Println("Welcome to Go Programming")

Now, go to the /work/src/welcome directory.

$ cd ~/work/src/welcome

Build the created program by running the following command inside the /work/src/welcome directory.

$ go build

The above command creates a binary executable file ‘welcome’ in the current directory. You can execute or run the file by using the following command.

$ ./welcome

The output of the above program should be as the following.

Welcome to Go Programming

You can also run a go program using the following command.

$ go run welcome.go

To run multiple Go files at once, you can give a glob pattern or mention all the file names in single run command. You can run multiple Go files by using any of the following commands.

$ go run dir/*.go
$ go run dir/**/*.go
$ go run file1.go file2.go file3.go

If you want to deploy the binary files to the bin directory, run the following command.

$ go install welcome.go

The above command installs the welcome binary file in the bin directory of your Go workspace. Since the bin directory is in the Go Path, you can execute the program from anywhere.

$ welcome
Welcome to Go Programming

For further queries or questions, please reach out to our support team.

Leave a Reply