How to Install Go (Golang) Compiler on Debian 11

Go, or Golang, is an open-source programming language that Google created. It’s statically typed and produces compiled machine code binaries. 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 guide, you will know how to install and configure Go (Golang) on your Debian 11 Bullseye operating system.

Prerequisites

  • Recommended OS: Debian 11 Bullseye.
  • User account: A user account with sudo privilages or root access (su command).

Updating Operating System

Update your Debian 11 operating system to make sure all existing packages are up to date:

sudo apt update && sudo apt upgrade

Root or Sudo Access

By default, when you create your account at startup with Debian compared to other distributions, it does not automatically receive sudoers status. You must either have access to the root password to use the su command or visit our tutorial on How to Add a User to Sudoers on Debian.

Install Dependencies for Installation

To install Golang, you will need to make sure the following packages are installed. Run the following command to verify or install the packages:

sudo apt install wget software-properties-common apt-transport-https -y

Install Golang

Download the latest version of Go from their official website using the following command:

wget https://golang.org/dl/go1.17.linux-amd64.tar.gz

Note, at the time of this guide, version 1.7 is the latest. Visit the download page to check version numbers and replace the URL with an updated one if available.

Once the Go is downloaded, extract the downloaded file to the /usr/local/ directory:

sudo tar -zxvf go1.17.linux-amd64.tar.gz -C /usr/local/

Note, replace go1.17.linux-amd64.tar.gz in the wget and tar commands with the current version from the downloads page.

Next, you need to configure the ${PATH} environment variable to include Go’s bin directory /usr/loca/go/bin.

You can do this by using the echo command as follows:

Option 1. Load system-wide installation and load the environment onto your current login session:

echo "export PATH=/usr/local/go/bin:${PATH}" | sudo tee /etc/profile.d/go.sh
source /etc/profile.d/go.sh

Option 2. Load a specific profile and load the environment onto your current login session:

echo "export PATH=/usr/local/go/bin:${PATH}" | sudo tee -a $HOME/.profile source
source $HOME/.profile

Next, verify the go installation and current build by using the following command:

go version

Example output:

go version go1.17 linux/amd64

Create Test Application

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 && go build

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

./mod

You should get the following output:

Hello, World!

Comments and Conclusion

In the guide, you have learned how to successfully install Go (Golang) from installing the source and successfully create your first Go program.

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!