How to Install Go (Golang) Compiler on Linux Mint 20

Go, or Golang, is an open-source programming language that Google created. It’s statically typed and produces compiled machine code binaries, and go language is a compiled language. This is popular amongst developers as it means you do not need to compile the source code to create an executable file. Developers that use Google’s Go language say it is the C for the twenty-first century when it comes to syntax.

In the following tutorial, you will learn how to install and configure Go (Golang) on Linux Mint 20.

Prerequisites

Update Operating System

Update your Linux Mint operating system to make sure all existing packages are up to date:

sudo apt update && sudo apt upgrade -y

The tutorial will be using the sudo command and assuming you have sudo status.

To verify sudo status on your account:

sudo whoami

Example output showing sudo status:

[joshua@linuxmint ~]$ sudo whoami
root

To set up an existing or new sudo account, visit our tutorial on Adding a User to Sudoers on Linux Mint.

To use the root account, use the following command with the root password to log in.

su

The tutorial will be utilizing the terminal, and for those unfamiliar, this can be found in your show applications menu.

Example:

How to Install Go (Golang) Compiler on Linux Mint 20

Option 1. Install Go (Goland) with APT

You can install Go (Golang) in two ways with APT, the first is using the default APT repository, which is lagging from the latest versions, or you can install the PPA Golang Backports, which boasts the most up to date version at all times.

To install Golang using these two options, use the following command.

Install Golang with PPA Golang Backports (Recommended):

First, in your terminal, add the Golang backports PPA.

sudo add-apt-repository ppa:longsleep/golang-backports -y

Update your APT repository to reflect the new addition.

sudo apt-get update

Next, install Golang.

sudo apt install golang

Install Golang from Standard Repository:

sudo apt install golang

Example output:

How to Install Go (Golang) Compiler on Linux Mint 20

Type Y, then press the ENTER KEY to proceed with the installation.

Once installed, verify the version of Golang you installed.

go version

Example output:

go version go1.17.5 linux/amd64

Note, at the current time of this tutorial, the PPA and default repository versions were version 1.13.8 and the PPA version 1.17.5. This shows how far apart they are and why it is recommended to use the PPA version preferably.

Option 2. Install Golang from Source

The second option is to install Go manually from the source, which may be more suitable for developers needing a specific version. To do this, download Golang from their official website. You can also do this in the terminal when you get the link to the version you require using the following command.

Example:

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

Once the Golang is downloaded, extract the downloaded file to the /usr/local/ directory.

sudo tar -xzf go1.17.5.linux-amd64.tar.gz -C /usr/local/

Now, you will need to add the path of the Go directory to the $PATH variable in the /etc/profile directory using any text editor.

sudo nano /etc/profile

Once the file is open, add the following line at the end of the file, then (CTRL+O) to save, and (CTRL+X) to exit once done.

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

Next, activate the PATH environment variable that you just added. Type the following command.

source /etc/profile

Next, as before with using the apt package manager installation, check the version.

go version

Example:

go version go1.17.5 linux/amd64

How to use Go – Create a program.

Now that you have installed Go, we will create a small program that will print (hello world).

First, create a directory:

sudo mkdir go-hello

Now, you will create a (.go) file. The quickest way to do this is using the nano editor:

sudo nano go-hello/hello.go

Next, add the following text below to create the (Hello World) Go program:

package main

import "fmt"

func main() {
     fmt.Printf("Hello, World\n") 
}

Save the file (CTRL+O), then exit (CTRL+X).

Now, you need to build (go.mod) file so you can execute the Go file you just created:

sudo nano go-hello/go.mod

Add the following line:

module example.com/mod

Now save the file (CTRL+O) and exit (CTRL+X).

Next, cd to the directory, then build the program by entering the following command:

cd go-hello && sudo go build

Finally, execute the (Hello World) program by entering the following command:

./mod

You should get the following output:

How to Install Go (Golang) Compiler on Linux Mint 20

Comments and Conclusion:

In the tutorial, you have learned how to install Go (Golang) successfully using multiple methods on your Linux Mint 20 system. Overall, Go is worth installing even if you want to experiment with the language as it’s easy to learn, purpose-built, rapidly expanding, backed up by giant Google, which is then being led to be picked up by large companies like Docker and many others.

For more information on using and developing with Go, visit the official documentation.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!