How To Install Go (Golang) on Debian Linux

Go, also known as Golang, is a popular open-source programming language developed by Google. It’s known for its simplicity, efficiency, and strong support for concurrent programming. In this guide, we will walk you through the steps to install Go on a Debian Linux system.

Prerequisites

  • A Debian-based Linux system
  • Access to a terminal
  • Basic knowledge of Linux commands

Step 1: Update Your System

Before installing any new software, it’s a good practice to update your system. Open your terminal and run the following commands:

sudo apt update 
sudo apt upgrade 

This process will ensure that all your system’s packages are up-to-date.

Step 2: Downloading Golang

Visit the official Go website to find the latest version of Golang. You can download it directly using the wget command. Replace the URL in the command below with the latest download link:

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

Step 3: Extracting the Go Archive

After downloading, extract the archive to the /usr/local directory. This is the standard directory for user-installed software.

sudo tar -xvf go1.22.5.linux-amd64.tar.gz -C /usr/local 

Step 4: Setting Up Go Paths

For Go to function properly, you need to set up the GOROOT and GOPATH environment variables. GOROOT is the location where Go is installed, and GOPATH is the workspace for Go projects.

Add these lines to your ~/.profile or ~/.bashrc file:

export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH

COPY

After adding these lines, apply the changes with:

source ~/.profile 

or

source ~/.bashrc 

Step 5: Verifying the Installation

To ensure Go is installed correctly, you can run:

go version 

This command will display the installed version of Go.

Step 6: Creating Your First Go Project

Now that Go is installed, you can start your first project. Create a new directory for your project in the Go workspace:

mkdir $GOPATH/src/hello 
cd $GOPATH/src/hello 

Create a file named hello.go and add a simple Hello World program:

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

COPY

Run your program with:

go run hello.go 

You should see “Hello, World!” printed in the terminal.