As the world of software development constantly advances, it’s vital for developers and system administrators to keep their tools up-to-date. This article provides a detailed walkthrough of two scripts designed specifically for Debian 12 x64 systems. The first script covers the download and extraction of Go (Golang) version 1.21.4, and the second script sets up the Go environment for a user named ‘debian’.

Script 1: Downloading and Extracting Golang 1.21.4 on Debian 12 x64

This script is tailored for Debian 12 x64 users. It automatically downloads and extracts Go 1.21.4 to the directory /usr/lib/go-1.21. Execution with root privileges is assumed, achievable either as the root user or using sudo.

#!/bin/bash

# Ensuring wget is installed
apt-get update
apt-get install -y wget

# Downloading Golang 1.21.4
wget https://go.dev/dl/go1.21.4.linux-amd64.tar.gz -P /tmp

# Extracting the tarball to /usr/lib/go-1.21
tar -C /usr/lib -xzf /tmp/go1.21.4.linux-amd64.tar.gz
mv /usr/lib/go /usr/lib/go-1.21

# Cleaning up the downloaded tarball
rm /tmp/go1.21.4.linux-amd64.tar.gz

echo "Golang 1.21.4 has been successfully downloaded and extracted on Debian 12 x64."

This script ensures a clean and conflict-free installation in a dedicated directory, ideal for version management and system stability.

Script 2: Configuring the Go Environment for the ‘debian’ User on Debian 12 x64

The second script is designed to configure the Go environment for a specific user, in this case, ‘debian’. It sets the necessary environment variables and updates the user’s PATH to include the Go binary.

#!/bin/bash

# Target user for Go configuration
TARGET_USER=debian

# Adding Go binary to PATH in .bashrc
echo "export PATH=/usr/lib/go-1.21/bin:\$PATH" | sudo tee -a /home/$TARGET_USER/.bashrc

# Setting Go environment variables
echo "export GOROOT=/usr/lib/go-1.21" | sudo tee -a /home/$TARGET_USER/.bashrc
echo "export GOPATH=/home/$TARGET_USER/go" | sudo tee -a /home/$TARGET_USER/.bashrc
echo "export GOBIN=\$GOPATH/bin" | sudo tee -a /home/$TARGET_USER/.bashrc

# Applying changes
source /home/$TARGET_USER/.bashrc

echo "Golang environment has been successfully set for user 'debian' on Debian 12 x64."

This script makes Go readily accessible for the ‘debian’ user, enabling them to start using Go immediately after the setup. It’s an essential step for maintaining a clean and efficient development environment.


Together, these scripts demonstrate a practical approach to setting up and managing Go environments on Debian 12 x64 systems. They are designed to provide a straightforward and error-free installation and configuration process, enhancing productivity and ensuring consistency across different setups.