How to Install Go on Debian

July 18, 2024

Introduction

Go, or Golang, is a relatively new, open-source language created by Google. Its purpose is to streamline software development and enable users to create simple and reliable apps. 

As a modern language, Go offers memory allocation, concurrency support, garbage collection, coordination avoidance, etc.

This article explains how to install Go on Debian 12 in a few simple steps.

How to Install Go on Debian

Prerequisites

  • Debian system (this tutorial uses Debian 12).
  • A sudo user.
  • Access to the command line.

How to Install Go on Debian 12

Before starting the installation, ensure the Debian server is up to date. Update the repository with the following:

sudo apt update -y
sudo apt update -y terminal output

Step 1: Download Go

To start the installation process:

1. Visit the Go downloads page.

2. Click the Linux box under Featured downloads to start the download.

Download Go from the website

Note: Use wget or curl to download Go without accessing the browser.

Step 2: Extract Files

Extract files to the /usr/local directory. To do that, take the following steps:

1. Navigate to Downloads using the cd command:

cd ~/Downloads
cd ~/Downloads terminal output

2. Run the following command to extract files:

sudo tar -C /usr/local -xzf go1.22.5.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.22.5.linux-amd64.tar.gz terminal output

The command doesn't print any output. However, tar extracts the specified file (go1.19.2.linux-amd64.tar.gz) to the desired directory.

Step 3: Set the Environment

To set the environment variable, add /usr/local/go/bin to PATH.

Take the following steps:

1. Access .profile in Vim or another text editor.

vim .profile

2. At the end of the file, paste the following lines:

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

export GOPATH=$HOME/go

export PATH=$PATH:$GOPATH/bin
vim profile terminal output

3. Save and close the file.

4. Reload your shell configuration to apply the changes with:

source ~/.profile

The command has no output.

Note: To install Go system-wide, edit /etc/profile. To install it for the current user, access $HOME/.profile.

Step 4: Test the Installation

After installing Go and setting the paths, verify that Go is functioning properly. To do that, set up a basic Go development environment and create a simple "Hello, World!" program.

Take the following steps:

1. Create a new directory for your Go workspace where Go will compile its files. Use the mkdir command:

mkdir $HOME/go_workspace

The command has no output.

2. Set up a directory structure within this workspace. In this example, we use a directory called my_project:

mkdir -p $HOME/go_workspace/src/my_project/hello

The command consists of:

  • mkdir. The command that creates the necessary directory.
  • -p. The option that ensures any necessary parent directories are created along with the target directory.
  • go_workspace. The main workspace directory for Go projects.
  • src. A common subdirectory in Go workspaces where source code files are stored.
  • my_project. A project-specific directory within the src directory.
  • hello. A subdirectory within my_project where the "Hello, World!" Go program will be placed.

3. Open a new file to place the new Go program:

vim $HOME/go_workspace/src/my_project/hello/hello.go

4. Paste the following into the file:

package main

import "fmt"

func main() {
fmt.Println("Hello, World!")
}
create a new go program terminal output

The code includes the main Go package, imports the formatted I/O package, and defines a function to print "Hello, World!" when executed.

5. Save and exit the file.

6. Go to the project directory:

cd $HOME/go_workspace/src/my_project/hello
cd command terminal output

7. Compile the program with the following:

go install
terminal output for go install

8. Run the compiled program with:

hello
terminal output for hello

The output shows that Go is installed, and you can run Go programs.

Conclusion

This tutorial explained how to install Go on Debian, set environment variables, and test the installation using a simple example.

Next, read about the differences between a Debian and a Ubuntu server.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How to Install Node.js on Debian
June 27, 2024

Node.js is an open-source JavaScript runtime used for creating fast and scalable network applications. This guide will show you how to install Node.js on Debian...
Read more
Fail2Ban Installation & Setup: Ubuntu, CentOS, Fedora & Debian
May 13, 2021

Introduction Fail2ban is a software that protects your server from brute force attacks. It does this by monitoring server logs and...
Read more
How to Use apt-get reinstall on Debian and Ubuntu
October 22, 2020

When packages are accidentally removed or modified on Debian or Ubuntu, reinstalling them usually resolves the problem. The --reinstall flag...
Read more
How to Change Hostname in Debian 10
August 11, 2021

You can change the hostname in Debian 10 (Buster) as the root user by using the built-in hostname command or editing related system files. learn how to change your hostname in this...
Read more