January 27, 2022
How to install Go golang on Ubuntu 20.04

How To Install Golang on Ubuntu 20.04

On this tutorial, we will learn how to install Go/Golang on Ubuntu 20.04 LTS operating system

Introduction

What is Go, or sometimes referred to as “Golang”? Go is an open-source programming language that was released by Google in 2009. The use of Go is getting more and more growing. Since its release, Golang has become highly popular among developers and is used for various applications ranging from cloud or server-side applications, to artificial intelligence and robotics. On this tutorial we will discuss how to install Go on Ubuntu 20.04 LTS operating system.

Installing Go/Golang on Ubuntu 20.04 LTS

Prerequisite

Before installing GO, there are several prerequisites that must be met, including:

  1. Ubuntu 20.04 system
  2. a non-root user with sudo privileges
  3. good network connection

The Go Installation task on this article will divided with subtopics as following :

  1. Download Go Archived Binary file
  2. Installing Go
  3. Testing Go language

1. Download Go Binary File

As of this tutorial crated, the Go latest release is go1.17.3. To install Go on an Ubuntu server we have to download it from the Go official website. We will go to the official Go downloads page and copy the URL for the current binary release’s tarball and select the Linux distribution file (https://dl.google.com/go/go1.17.3.linux-amd64.tar.gz).

Go 1.17.3 Source file
Go 1.17.3 Source file

By using curl command line, we will download the file.

$ curl -OL https://golang.org/dl/go1.17.3.linux-amd64.tar.gz
rapik@worker2:~$ curl -OL https://golang.org/dl/go1.17.3.linux-amd64.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    80  100    80    0     0    137      0 --:--:-- --:--:-- --:--:--   136
100    75  100    75    0     0     62      0  0:00:01  0:00:01 --:--:--    62
100  128M  100  128M    0     0  1650k      0  0:01:19  0:01:19 --:--:-- 1861k

We will verify the integrity of the file we’ve just downloaded, we will run the sha256sum command line.

rapik@worker2:~$ sha256sum go1.17.3.linux-amd64.tar.gz
550f9845451c0c94be679faf116291e7807a8d78b43149f9506c1b15eb89008c  go1.17.3.linux-amd64.tar.gz
Download Go 1.17.3 Source file
Download Go 1.17.3 Source file

2. Installing Go

After Go source file is downloaded, then we will extract the tarball file to /usr/local directory, creating a Go tree in /usr/local/go. This step will remove a previous installation at /usr/local/go, if any, prior to extracting. Please back up any data before proceeding.

$ sudo tar -C /usr/local -xvf go1.17.3.linux-amd64.tar.gz

After extracting, the we will will set the GO paths in our environment, add /usr/local/go/bin to the PATH environment variable. We can do this by adding the following line to your $HOME/.profile or /etc/profile (for a system-wide installation).

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

Changes made to a profile file may not apply until the next time you log into your computer. To apply the changes immediately, just run the shell commands directly or execute them from the profile using a command such as source $HOME/.profile.

$ sudo vi ~/.profile
$ source ~/.profile

Then we will verify GO installation by quyering its version with following command line :

$ go version
rapik@worker2:~$ go version
go version go1.17.3 linux/amd64
Go version 1.17.3
Go version 1.17.3

The GO installation is completed done successfully where th Go version 1.17.13 was installed on the system.

3 Testing Go Language

On this step, we will try to make a simple script to be compiled with Go and run it. We will use the ‘Hello World’ script for this testing. The series task for creating a simple testing is as follow :

  1. Create a new directory, called as gotest
  2. Manage the dependencies module
  3. Create a new file, named it as hello.go
  4. Test the code
  5. Make Go code Into a binary Executable
rapik@worker2:~$ mkdir gotest
rapik@worker2:~$ cd gotest/
rapik@worker2:~/gotest$ go mod init otodiginet/hello
go: creating new go.mod: module otodiginet/hello
rapik@worker2:~/gotest$ vi hello.go

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

rapik@worker2:~/gotest$ ls -ltr
total 8
-rw-rw-r-- 1 rapik rapik 33 Nov 27 02:40 go.mod
-rw-rw-r-- 1 rapik rapik 77 Nov 27 02:40 hello.go
rapik@worker2:~/gotest$ go run hello.go
Hello, World!
rapik@worker2:~/gotest$ go build
rapik@worker2:~/gotest$ ./hello
rapik@worker2:~/gotest$ Hello, World!
Testing GO with "Hello, World !"
Testing GO with “Hello, World !”

Conclusion

On this article we have shown you how to install GO/Golang on Ubuntu 20.04 operating system. For more detail about GO language can be discover the GO official website.

Share this article via :

Leave a Reply

Your email address will not be published. Required fields are marked *