How to Install Go on Debian 12
Introduction
Go also known as Golang is an open-source programming language designed for simplicity, reliability, and efficiency. Go is a fast, statically typed compiled language suitable for developing scalable web servers, cloud-native applications, and command-line tools.
This guide explains how to install Go on Debian 12. You'll also manage multiple Go versions to meet different project requirements.
Prerequisites
Before you begin:
- Deploy a Debian 12 instance on Vultr.
- Access the instance using SSH.
- Create a non-root user with sudo privileges and switch to the user.
Install Go on Debian 12
Go is available in the default APT package repositories on Debian 12. Follow the steps below to install Go on your server.
Update the server's package information index.
console$ sudo apt update
Install Go.
console$ sudo apt install golang -y
View the installed Go version.
console$ go version
Output:
go version go1.19.8 linux/amd64
Test the Go Installation
Follow the steps below to create a sample application to verify that Go runs correctly on your server.
Create a new
go-sample
project directory.console$ mkdir go-sample
Switch to the
go-sample
directory.console$ cd go-sample
Initialize a new
example.com/hello
Go module in the directory,console$ go mod init example.com/hello
Create a new
main.go
application file using a text editor likenano
.console$ nano main.go
Add the following contents to the
main.go
file.gopackage main import "fmt" func main() { fmt.Println("Greetings from Vultr") }
Save and close the file.
The above Go application displays a
Greeting from Vultr
message when you run it.Run the
main.go
application file.console$ go run main.go
Output:
Greetings from Vultr
Install Multiple Go Versions
Maintaining multiple Go versions on your system allows you to meet different project requirements. You can use the Go version manager (GVM) or manually install specific Go versions in your project environment. Follow the steps below to install and manage multiple GO versions using both methods.
Install Multiple Go Versions From Source
Follow the steps below to install multiple Go versions from source.
Visit the Go releases webpage and download a specific Go version's archive file. For example, download the Go version
1.23.0
.console$ wget https://go.dev/dl/go1.23.0.linux-amd64.tar.gz
Run the following command to download another Go version such as
1.21.0
.console$ wget https://go.dev/dl/go1.21.0.linux-amd64.tar.gz
Create a new
/usr/local/go1.23.0
directory to store the1.23.0
Go version files.console$ sudo mkdir -p /usr/local/go1.23.0
Create another
/usr/local/go1.21.0
directory to store the1.21.0
Go version files.console$ sudo mkdir -p /usr/local/go1.21.0
Extract the
1.23.0
Go archive files to the/usr/local/go1.23.0
directory.console$ sudo tar -C /usr/local/go1.23.0 -xzf go1.23.0.linux-amd64.tar.gz
Extract the
1.21.0
Go archive files to the/usr/local/go1.21.0
directory.console$ sudo tar -C /usr/local/go1.21.0 -xzf go1.21.0.linux-amd64.tar.gz
Create a new
PATH
environment variable and set the1.23.0
Go version to activate it as the default on the server.console$ export PATH=/usr/local/go1.23.0/go/bin:$PATH
Verify the active Go version.
console$ go version
Output:
go version go1.23.0 linux/amd64
Create a new
go123
alias for the1.23.0
Go version to access it using a single command.console$ echo "alias go123='/usr/local/go1.23.0/go/bin/go'" >> ~/.bashrc
Create another
go121
alias for the1.21.0
Go version to access it using a single command.console$ echo "alias go121='/usr/local/go1.21.0/go/bin/go'" >> ~/.bashrc
Reload your shell configuration to apply the changes.
console$ source ~/.bashrc
Run the alias command associated with the installed Go version. For example, run the
main.go
application you created earlier using the1.23.0
Go version.console$ go123 run main.go
Output:
Greetings from Vultr
Run the
main.go
application file using thego121
alias command.console$ go121 run main.go
Output:
Greetings from Vultr
Install Multiple Go Versions Using GVM
Go Version Manager (GVM) allows you to manage and switch between Go versions. You can use GVM to install and use specific Go versions without manually setting up a PATH
variable or downloading any binary packages. Follow the steps below to install GVM and manage multiple Go versions.
Update the server's package information index.
console$ sudo apt update
Install all required GVM dependency packages.
console$ sudo apt install -y curl git make binutils bison gcc
Download and install GVM using the latest installation script.
console$ bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
Reload your shell configuration to apply the changes.
console$ source ~/.gvm/scripts/gvm
Use GVM to install a specific Go version. For example, Go version
1.20.8
.console$ gvm install go1.20.8
Output:
go1.20.8 successfully installed!
Set the
1.20.8
Go version as the default.console$ gvm use go1.20.8 --default
Output:
Now using version go1.20.8
View the active Go version.
console$ go version
Output:
go version go1.20.8 linux/amd64
Install another Go version. For example, Go version
1.21.5
.console$ gvm install go1.21.5
List all installed Go versions.
console$ gvm list
Output:
gvm gos (installed) go1.20.8 go1.21.5 system
Run the following command to switch to a specific Go version such as
go1.21.5
in your shell environment.console$ gvm use go1.21.5
Conclusion
You have installed Go on Debian 12 and managed multiple versions. You can use Go with popular libraries, frameworks, and databases to create modern applications. For more information, visit the Go documentation.