How to Install Golang (Go) on Debian 12, 11 or 10

In the dynamic landscape of programming languages, Go, commonly referred to as Golang, emerges as a modern, efficient, and powerful contender. Below are a highlight of some of the key features of Golang:

  • Intuitive Syntax: Drawing parallels with the C programming language, Golang’s syntax is expressive and straightforward, facilitating easy adoption for developers acquainted with C or C++.
  • Automated Memory Management: Golang’s built-in garbage collector autonomously oversees memory allocation and deallocation, allowing developers to focus on crafting code rather than micromanaging memory.
  • Inherent Concurrency: Golang stands out with its native support for concurrency. It harnesses Goroutines—lightweight threads capable of concurrent execution—to manage concurrent operations adeptly.
  • Comprehensive Standard Library: Golang’s expansive standard library equips developers with many functionalities, from web application development and system tools to networking utilities.
  • Versatility Across Platforms: As a cross-platform language, Golang can be adeptly compiled to function on many operating systems, including Windows, Linux, and macOS.
  • Rigorous Static Typing: Golang is a statically typed language that emphasizes precision. This mandates that variables are declared with their designated type during compilation, enhancing error detection at the compile stage rather than during runtime.
  • Efficiency of Compilation: Being a compiled language, Golang translates code into machine code, which the computer’s processor can directly execute. This attribute bolsters the speed and efficiency of Golang programs, setting them apart from interpreted languages.

With its blend of simplicity, speed, and robust features, Golang is an invaluable asset for developers, especially those operating on Debian systems. Now, let’s move on to the main article on installing Go.

Update Debian Before Go Installation

Before installing Golang on Debian, update your system to ensure all system packages are up-to-date and avoid potential conflicts. To do this, open your terminal and run the following command:

sudo apt update && sudo apt upgrade

Download Golang Tarball

To download the latest version of Go, visit the official website at https://go.dev/dl/. At the time of writing, the latest version of Go is 1.20.2

Once you have identified the latest version of Go for Linux, copy the URL of the corresponding download link.

In the terminal, navigate to the directory where you want to download the Go package. For example, you can navigate to the Downloads directory by running the following command:

cd ~/Downloads

Use the wget command to download the Go package. Replace the URL in the following command with the URL you copied earlier:

## Example Only ###

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

Note: This is just an example command. Remember to get the latest link, as this will eventually become outdated.

Extract the downloaded package using the tar command:

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

Add Golang PATH environment

Add the Go binary directory to your PATH environment variable to use Go. To add the Go binary directory to your PATH environment variable in your specific profile, run the following command:

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

This command appends the Go binary directory to the PATH environment variable in your ~/.profile file.

To load the new PATH configuration onto your current login session, run the following command:

source $HOME/.profile

This command applies the changes made to the PATH environment variable in your ~/.profile file.

To verify that Go is installed correctly and that the PATH environment variable is configured correctly, run the following command in the terminal:

go version

This should output the version of Go you just installed, for example:

go version go1.20.2 linux/amd64

Create a Go Test Application

In this step, we will create a small program that prints “Hello, World” using Go. This is a simple example to verify that Go is installed correctly and that you can create and execute a Go program

Create a new directory named go-hello using the following command:

mkdir go-hello

This directory will be used to store the Go program.

Create a new file named hello.go inside the go-hello directory using the following command:

nano go-hello/hello.go

Add the following code to the hello.go file:

package main

import "fmt"

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

This code creates a simple Go program that prints “Hello, World” to the console.

Save the file by pressing Ctrl+O, then exit the editor by pressing Ctrl+X.

Create a new file named go.mod inside the go-hello directory using the following command:

nano go-hello/go.mod

Add the following line to the go.mod file:

module example.com/mod

This line is required to build the program.

Save the file by pressing Ctrl+O, then exit the editor by pressing Ctrl+X.

Change to the go-hello directory using the following command:

cd go-hello

Build the program by running the following command:

go build

This command will generate an executable file named mod.

Now execute the program by running the following command:

./mod

This command should output the following text:

Hello, World!

Conclusion

In conclusion, installing Golang on Debian is a straightforward process that can be done using the command line terminal. Following the steps outlined in this guide, you can successfully install Golang on Debian 12 Bookworm, Debian 11 Bullseye, or Debian 10 Buster distributions. Once installed, you can develop efficient, reliable, and scalable software using Go. With its simplicity, speed, and robustness, Golang has become increasingly popular among developers and is used in various fields, including web development, system tools, and networking applications.

Joshua James
Follow me
Latest posts by Joshua James (see all)