How To Install Golang on Ubuntu 22.04

In this short tutorial we will learn how to install Golang or GO version 1.19.4 on Ubuntu 22.04 Linux operating system. Golang is cross-platform and open-source programming language was designed by Google’s engineers, Robert Griesemer, Ken Thompson, and Rob Pike. Golang is an ideal programming language for creating efficient applications. The code syntax of GO is quite similar to C, but the language offers more features, including garbage collection, memory safety, structural typing and more.

The growth of Golang’s adoption in building applications is going to increase, the place where I work today, many applications are built with Golang.

Installing GO/Golang On Ubuntu 22.04

Prerequisites

Before we start the Golang installation process, there are several prerequisites that must be met so that the installation process can run smoothly. We will need to prepare any requirements as mention below :

  • an Ubuntu 22.04 system
  • a non-root user with sudo privileges
  • sufficient disk and good internet access

There are three installation methods that will be covered in this tutorial :

  1. Installation using APT command
  2. Installation using Binary file
  3. Installation using SNAP
  4. Installation using Aptitude

1. Golang Installation using APT command

APT stands for Advanced Package Tool. It is a set of core tools inside Debian and Ubuntu Linux system. This tool that works with core libraries to handle the setup, upgrade, and removal of software on Linux OS. The following are the steps to install Golang with the APT method:

1.1. Update and Upgrade Ubuntu 22.04 System

The first step of Go/Golang programming language installation is to update local packages software. This task will cut down the installation time and it also helps prevent zero-day exploits against outdated software. We will use command line :

$ sudo apt update
$ sudo apt -y full-upgrade

1.2 Installing Go/Golang

After system repository has been updated and upgraded to the latest version, then we will install GO by submitting command line :

$ sudo apt install golang-go

Output :

ramans@infodiginet:~$ sudo apt install golang-go
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libflashrom1 libftdi1-2
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu g++ g++-11 gcc gcc-11 golang-1.18-go golang-1.18-src golang-src
  libasan6 libbinutils libc-dev-bin libc-devtools libc6-dev libcc1-0 libcrypt-dev libctf-nobfd0 libctf0 libdpkg-perl
  libfile-fcntllock-perl libgcc-11-dev libitm1 liblsan0 libnsl-dev libquadmath0 libstdc++-11-dev libtirpc-dev libtsan0
  libubsan1 linux-libc-dev manpages-dev pkg-config rpcsvc-proto
Suggested packages:
  binutils-doc g++-multilib g++-11-multilib gcc-11-doc gcc-multilib make autoconf automake libtool flex bison gcc-doc
  gcc-11-multilib gcc-11-locales bzr | brz git mercurial subversion glibc-doc debian-keyring bzr libstdc++-11-doc
  dpkg-dev
The following NEW packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu g++ g++-11 gcc gcc-11 golang-1.18-go golang-1.18-src golang-go
  golang-src libasan6 libbinutils libc-dev-bin libc-devtools libc6-dev libcc1-0 libcrypt-dev libctf-nobfd0 libctf0
  libdpkg-perl libfile-fcntllock-perl libgcc-11-dev libitm1 liblsan0 libnsl-dev libquadmath0 libstdc++-11-dev
  libtirpc-dev libtsan0 libubsan1 linux-libc-dev manpages-dev pkg-config rpcsvc-proto
0 upgraded, 35 newly installed, 0 to remove and 8 not upgraded.
Need to get 135 MB of archives.
After this operation, 618 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
. . .
update-alternatives: using /usr/bin/g++ to provide /usr/bin/c++ (c++) in auto mode
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...

1.3. Verify Golang Installation

After Golang installation was completed done, then we will verify it by querying it version.

$ go version

Output :

ramans@infodiginet:~$ go version
go version go1.18.1 linux/amd64
Go version 18.1

2. Installing Golang Using Binnary File

If we want to install the latest version of Golang, then this method will be the right choice because we can choose which version to install. By using Golang Binnary file, we will get the latest software version which is available from official Golang website. The latest stable version of Go when this tutorial is being made is 19.4. Here are the ways to install it.

2.1. Download The Latest Go Binary File

In this stage, we will download the latest stable version of the Go binary file by issuing the following command :

$ wget https://go.dev/dl/go1.19.4.linux-amd64.tar.gz

Output :

ramans@infodiginet:~$ wget https://go.dev/dl/go1.19.4.linux-amd64.tar.gz
--2022-12-19 07:44:02--  https://go.dev/dl/go1.19.4.linux-amd64.tar.gz
Resolving go.dev (go.dev)... 216.239.36.21, 216.239.38.21, 216.239.32.21, ...
Connecting to go.dev (go.dev)|216.239.36.21|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://dl.google.com/go/go1.19.4.linux-amd64.tar.gz [following]
--2022-12-19 07:44:08--  https://dl.google.com/go/go1.19.4.linux-amd64.tar.gz
Resolving dl.google.com (dl.google.com)... 172.217.194.91, 172.217.194.136, 172.217.194.190, ...
Connecting to dl.google.com (dl.google.com)|172.217.194.91|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 148931745 (142M) [application/x-gzip]
Saving to: ‘go1.19.4.linux-amd64.tar.gz’

go1.19.4.linux-amd64.tar.gz    100%[===================================================>] 142,03M   357KB/s    in 9m 40s  

2022-12-19 07:53:50 (251 KB/s) - ‘go1.19.4.linux-amd64.tar.gz’ saved [148931745/148931745]

2.2. Unpacking Go Source File

In this stage, we will unpack Go source file to /usr/local directory. For this purpose we will submit following command line :

$ sudo tar -xf go1.19.4.linux-amd64.tar.gz -C /usr/local

2.3. Setting Go Profile

In this stage, we will setup Go profile by updating /etc/profile file. We will append this file with following text file.

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

The complete content of /etc/profile will be as shown below :

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "${PS1-}" ]; then
  if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
    # The file bash.bashrc already sets the default PS1.
    # PS1='\h:\w\$ '
    if [ -f /etc/bash.bashrc ]; then
      . /etc/bash.bashrc
    fi
  else
    if [ "$(id -u)" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
  fi
fi

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

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

After all are set, then we will activate the environment variable of the PATH by issuing the following command line :

$ source /etc/profile

2.4. Verify Golang Version

This is the final step to ensure if we have already installed the latest version of Golang. We will verify it by submitting command line :

$ go version

Output :

ramans@infodiginet:/$ go version
go version go1.19.4 linux/amd64
go version 19.4

Conclusion

In this short tutorial we have learned how to install Golang on Ubuntu 22.04 LTS operating system. We used two option, namely : installing Golang using APT and installing Golang using binnary file. I hope this tutorial will be helpful.

(Visited 239 times, 1 visits today)

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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