How to Install Golang on Ubuntu

How to Install Golang on Ubuntu
How to Install Golang on Ubuntu

Go, also called Golang or Go language, is an open-source programming language developed by Google. Software developers use Go in an array of operating systems and frameworks to build web applications, cloud and networking services, and other types of software.

Go language was inspired by the productivity and relative simplicity of Python. It uses goroutines, lightweight processes, and packages for efficient dependency management. Google mainly developed Go to address their problems when building large-scale applications. They needed a programming language that was very good at concurrent programming.  It is designed to solve several issues, including slow build time, uncontrolled dependencies, effort duplication, difficulty writing automatic tools, and cross-language development.

Ubuntu is among the many popular Linux distributions developers and programmers use daily. If that's you, you might be reassured to know that installing the programming language Go is a relatively straightforward task. Like many tasks on Linux, this is relatively easy since you only need a few terminal commands. You can install Go from the official Binary package, through the APT package manager, or from a Snap package.

Prerequisites

  • A server with a fresh installation of the latest Ubuntu version 22.04, with sudo privileges.
  • A root user account.

Step-by-step process to Install Go on Ubuntu

For the purpose of this tutorial, we will use a CPU-powered Virtual Machine offered by NodeShift; however, you can replicate the same steps with any other cloud provider of your choice.

In order to deploy a NodeShift Virtual Machine, follow the guidelines here:

Step 1: Connect to the Virtual Machine using SSH or Username/Password

  1. NodeShift Compute VMs can be connected and controlled through a terminal and SSH. 
  2. Open your terminal paste the IP with SSH and set up the password.
  3. And then you will get connected to the root user.

Step 2: Update the System, packages, and repositories

First, ensure your system is up to date. Open a terminal and run the command below:

sudo apt update

After executing the update command, execute the following command to upgrade your systems’ installed packages.

sudo apt upgrade

You can also combine both commands and run them simultaneously.

sudo apt update && sudo apt upgrade

Step 3: Install Go on Ubuntu
There are two common ways to install Go on your Ubuntu 22.04 system:
1. Install Go using the Ubuntu package manager
2. Install Go using the wget command

Go can be installed in a few ways. The most straightforward method is using the sudo apt install command or the package manager. However, this does not guarantee that the latest version of Go will be installed.

Alternatively, you can download remote files using the wget method, a command line utility.

Method 1: Install Go using the Ubuntu package manager

Run the following command to install Go using the Ubuntu package manager.

sudo apt install golang-go

This command will install the Golang very easily.

Method 2: Install Go using the wget command

Suppose you install the latest Go package using the binary package from the Golang official website. Then, you need to use the wget command to fetch it, specifying the URL of the latest version.

Fetch the binary package

Go to the official Go website and its ‘All releases’ page. Select the correct Linux binary package version and copy the link address.

Then, run the wget command, as in the following example. You can specify a custom file name to download the file.

wget https://go.dev/dl/go1.23.0.linux-amd64.tar.gz -O go.tar.gz
Extract the package
  • After downloading the required files, extract them using the following command.
sudo tar -xzvf go.tar.gz -C /usr/local

The command then extracts all the files to the /usr/local/go directory. This will take some time to extract the files.

Step 4: Set the PATH Variable and check the version

When you work with Go, you must be able to access the Go command from any of your systems’ directories. To enable this functionality, you need to configure the PATH environment variable by adding the path of Go executables in the ~/.profile or ~/.bashrc file. Use the following command to set the path.

echo export PATH=$HOME/go/bin:/usr/local/go/bin:$PATH >> ~/.profile

This command will insert the updated PATH variable in the profile file. Alternatively, you can open the file profile file using ‘vi’ or ‘nano’ and directly insert the above line.

Then, save the changes by sourcing the file using the following command:

source ~/.profile

After using any of the above methods to install Go on Ubuntu, you can verify the installation using the following command.

go version

Step 5: Run the Go examples

  • Navigate to your workspace (or create one if it doesn't exist) from the following command:
mkdir -p ~/workspace
cd ~/workspace
  • Create a folder for your project:
mkdir go-test
cd go-test
  • Open the Go file using the Vim text editor to add your code.

So, what is Vim?

Vim is a text editor. The last line of the text editor is used to give commands to vi and provide you with information.

Note: If an error occurs which states that Vim is not a recognised internal or external command then install vim using the steps below.

Step 1: Update the package list

Before installing any software, we will update the package list using the following command in your terminal:

sudo apt update

Step 2: Install Vim

To install Vim, enter the following command:

sudo apt install vim -y

This command will retrieve and install Vim and its necessary components.

Now, you need to run the command below to code in the Go file:

vi test.go

Entering the editing mode in Vim:

Follow the below steps to enter the editing mode in Vim

Step 1: Open a File in Vim

Step 2: Navigate to Command Mode

When you open a file in Vim, you start in the command mode. You can issue commands to navigate, save, and manipulate text in this mode. To ensure you are in command mode, press the Esc key. This step is crucial because you cannot edit the text in other modes.

  • Insert the following code into the test.go File:
package main

import "fmt"

func main() {
    fmt.Println("Go is installed correctly!")
}

Save and close the file.

  • Run the Go program:
go run test.go
  • If Go is installed correctly, you should see the following output:
Go is installed correctly!

Conclusion

Installing Go on an Ubuntu system, particularly on a NodeShift Virtual Machine, is a straightforward process that can be achieved using either the Ubuntu package manager or manually downloading the binary package. Following the steps outlined, from updating the system to setting the PATH variable and running a simple Go program, ensures that Go is correctly installed and ready for development. Whether you're using the package manager for ease or the wget method for the latest version, this guide provides all the necessary commands and instructions to get Go up and running efficiently on your Ubuntu system.

For more information about NodeShift:

Read more