DEV Community

Maria Yudina
Maria Yudina

Posted on

How To Install Go on Ubuntu 20.04

Check Ubuntu version

First, run this command to make sure what version of Ubuntu you have:

lsb_release -a 
Enter fullscreen mode Exit fullscreen mode

Output example:

No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focal 
Enter fullscreen mode Exit fullscreen mode

Install GoLang

Download the latest GoLang archive:

curl -OL https://golang.org/dl/go1.17.3.linux-amd64.tar.gz 
Enter fullscreen mode Exit fullscreen mode

Check SHA256 Checksum just in case:

sha256sum go1.17.3.linux-amd64.tar.gz 
Enter fullscreen mode Exit fullscreen mode

Extract everything to the usr/local directory:

sudo tar -C /usr/local -xvf go1.17.3.linux-amd64.tar.gz 
Enter fullscreen mode Exit fullscreen mode

Update PATH variable in ~/.profile file:

sudo nano ~/.profile 
Enter fullscreen mode Exit fullscreen mode

Add new row with export at the end of the ~/.profile file:

export PATH=$PATH:/usr/local/go/bin 
Enter fullscreen mode Exit fullscreen mode

Save the changes and exit nano editor. Now we have to refresh your profile. Run this command:

source ~/.profile 
Enter fullscreen mode Exit fullscreen mode

Installation and setup is done. Let's check if everything works.

Make sure everything works

We will check Go version and create and run a simple program.

Check the version

Run this command to check Go version:

go version 
Enter fullscreen mode Exit fullscreen mode

Output:

go version go1.17.3 linux/amd64 
Enter fullscreen mode Exit fullscreen mode

Create and run 'Hello, World!'

Let's create a simple Go program and run it.

Create a directory and switch to it.

mkdir hello_go cd hello_go 
Enter fullscreen mode Exit fullscreen mode

Now we have to create go.mod file with the go mod init command:

go mod init test/hello_go 
Enter fullscreen mode Exit fullscreen mode

Create a file where we'll write the program code in Go.

nano hello.go 
Enter fullscreen mode Exit fullscreen mode

Copy this example code to the file and save changes.

package main import "fmt" func main() { fmt.Println("Hello, World!") } 
Enter fullscreen mode Exit fullscreen mode

Save the changes and exit nano editor.

Run the program:

go run . 
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, World! 
Enter fullscreen mode Exit fullscreen mode

Done!

Top comments (1)

Collapse
 
acim profile image
Boban Acimovic • Edited

It's much easier to use apt:

sudo add-apt-repository ppa:longsleep/golang-backports sudo apt update sudo apt install golang-go 
Enter fullscreen mode Exit fullscreen mode

or snap:

sudo snap install --classic go 
Enter fullscreen mode Exit fullscreen mode