How to Install Go (Golang) Compiler on Ubuntu 20.04

Go also called Golang is an open-source programming language developed by Google. It is used to build reliable and efficient applications. It is cross-platform and can be installed on Linux, Windows, and macOS. It is a compiled programming language that means you don't need to compile the source code to create an executable file. It is used by many organizations including, Mangodb, Soundcloud, Netflix, Uber, etc.

In this post, I will show you how to install the Go programming language on Ubuntu 20.04 server.

Prerequisites

  • A server running Ubuntu 20.04.
  • A root password is configured on the server.

Install Go

First, download the latest version of Go from their official website using the following command:

wget https://golang.org/dl/go1.16.5.linux-amd64.tar.gz

Once the Go is downloaded, extract the downloaded file to the /usr/local directory:

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

Next, you will need to add the path of the Go directory to the $PATH variable in /etc/profile directory.

nano /etc/profile

Add the following line:

export PATH=$PATH:/usr/local/go/bin

Save and close the file then activate the PATH environment variable using the following command:

source /etc/profile

Next, run the following command to check the installed version of Go:

go version

You should get the following output:

go version go1.16.5 linux/amd64

How to Use Go

In order to test the Go installation, we will write and build a sample program.

First, create a directory for your program with the following command:

mkdir hello

Next, create a hello.go file:

nano hello/hello.go

Add the following codes:

package main

import "fmt"

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

Save and close the file then create a go.mod file:

nano hello/go.mod

Add the following line:

module example.com/mod

Save and close the file then change the directory to hello and build the program with the following command:

cd hello
go build

Next, run your program with the following command:

./mod

You should see the following output:

Hello, World!

Conclusion

Congratulations! you have successfully installed Go on Ubuntu 20.04. You can now start developing your first application with Go. For more information, visit the Go documentation. 

Share this page:

Suggested articles

1 Comment(s)

Add comment

Comments

By: Taleman at: 2021-07-01 07:40:00

Why not

apt install golang