How To Install Golang On Ubuntu 20.04 LTS

On this short tutorial, we will learn how to install Golang version 1.17.3 on Ubuntu 20.04 operating system

Introduction

Go, sometimes referred to as “Golang”, is an open-source programming language that was released by Google in 2012. Google’s intention was to create a programming language that could be learned quickly. Since its release, Go has become highly popular among developers and is used for various applications ranging from cloud or server-side applications, to artificial intelligence and robotics. This tutorial outlines how to download and install the latest version of Go (currently version 1.16.7) on an Ubuntu 20.04 server, build the famous Hello, World! application, and make your Go code into an executable binary for future use.

This tutorial requires an Ubuntu 20.04 system configured with a non-root user with sudo privileges and a firewall as described in Initial Server Setup with Ubuntu 20.04.

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

  • Prerequisites
  • Download Go binary file
  • Install Go
  • Testing Golang Installation

1. Prerequisites

This tutorial requires an Ubuntu 20.04 system configured with a non-root user with sudo privileges and a firewall as described in Initial Server Setup with Ubuntu 20.04.

2. Download Go Binary File

On this stage, we will download Golang source file by using curl command line.

$ curl -OL https://golang.org/dl/go1.17.3.linux-amd64.tar.gz

Output :

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 and pass it to the filename as an argument.

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

Output:

rapik@worker2:~$ sha256sum go1.17.3.linux-amd64.tar.gz
550f9845451c0c94be679faf116291e7807a8d78b43149f9506c1b15eb89008c go1.17.3.linux-amd64.tar.gz

Then we will extract the tarball by using tar command line.

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

3. Setting Go Paths

After files are extracted, then we will set the Golang path. First, set Go’s root value, which tells Go where to look for its files. We can do this by editing the .profile file, which contains a list of commands that the system runs every time you log in.

The configuration file is located on ~/.profile file. We will use the vi editor to edit this file.

rapik@worker2:~$ sudo vi ~/.profile

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi
export PATH=$PATH:/usr/local/go/bin
 $ source ~/.profile

Verify the Golang version by querying its version.

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

4. Testing Golang Installation

On this stage, we will test the Golang installation.

We will use the Golang reserved word and create a new file called as hello.go.

& go mod init otodiginet/hello
& vi hello.go

Output :

<!-- wp:paragraph -->
<p>apik@worker2:~/gotest$ go mod init otodiginet/hello<br>go: creating new go.mod: module otodiginet/hello<br>rapik@worker2:~/gotest$ vi hello.go</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>package main</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>import "fmt"</p>
<!-- /wp:paragraph -->

<!-- wp:preformatted {"fontSize":"small"} -->
<pre class="wp-block-preformatted has-small-font-size">func main() {
fmt.Println("Hello, World!")
}
~
~</pre>
<!-- /wp:preformatted -->
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

Testing hello.go, by submitting command line :

$ go run hello.go
rapik@worker2:~/gotest$ go run hello.go
Hello, World!

Conclusion

On this short tutorial, we have learnt how to install Go language on Ubuntu 20.04 LTS operating system and take a simple testing.

Add a Comment

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