Home Featured Setup/Install Golang on Ubuntu 18.04

Setup/Install Golang on Ubuntu 18.04

Setup/Install Golang on Ubuntu 18.04


Go is an open source and statically type language by Google. Go is a compiled language, that needs to compile source code that results in generating the executable file. The generated executable file is used to run the program. Many popular applications like Docker, Kubernetes, Prometheus, Grafana and Terraform are written in Go. In this article, we will learn on how to setup/install golang on Ubuntu, as I am using Ubuntu OS. You can choose the right binary for your system from the official Go Download Page.

Download the Go binary:

At the time of writing the article the latest stable version of Go was 1.13.4. Please check for latest version from the Go Download Page.

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

sam@S:~$ curl -O https://dl.google.com/go/go1.13.4.linux-amd64.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  114M  100  114M    0     0  30.7M      0  0:00:03  0:00:03 --:--:-- 30.7M

Next extract using tar command as seen below:

sam@S:~$ sudo tar -xvf go1.13.4.linux-amd64.tar.gz 

The above commmand will extract under your home directory. Then change the ownership and move go package under usr/local/

sam@S:~$ sudo chown -R root:root ./go

Setting GOPATH:

It’s important to setup the required environment variable correctly. Now edit the .profile file by executing the below command:

sam@S:~$ sudo nano ~/.profile

Add to the end of the .profile with the below commands:

export GOROOT=$HOME/go
export GOPATH=$HOME/goLang
export GOBIN=$GOPATH/bin

To reflect the environment variable changes. Now refresh your profile by running:

sam@S:~$ source ~/.profile

Test your installation:

To test the installation is done correctly, we will create a folder under the GOPATH 
sam@S:~$ mkdir $HOME/goLang

In order to create a Hello file, execute the command replace github-username with your github-account-username:

sam@S:~$ mkdir -p goLang/src/github.com/github-username/hello

Now create and edit file “hello.go” by the following command:

sam@S:~$ nano ~/goLang/src/github.com/github-username/hello/hello.go

Insert the below content:

package main

import "fmt"

func main() {
    fmt.Printf("Hello World!!!\n")

Now save and exit. Compile the file with Go install command:

sam@S:~$ go install github.com/github-username/hello

Once compiled successfully, you can run the generated file by just using filename “hello”:

sam@S:~$ hello
Hello World!!!

If it prints hello, world, then your installation is successful. In this article, we have learned how to setup/install golang on Ubuntu, I hope it helps!


Installing go tools: https://golang.org/doc/install

Next Tutorial:

If you are interested to learn about the basics of gRPC, please check out my articles on gRPC : Introduction to gRPC