DEV Community

Cover image for Installing Go on Debian 12
Sanskriti Harmukh for Vultr

Posted on with Aashish Chaurasiya Originally published at docs.vultr.com

Installing Go on Debian 12

Go (Golang) is an open-source programming language designed for simplicity, reliability, and efficiency — a fast, statically typed compiled language suited to scalable web servers, cloud-native applications, and command-line tools. This guide installs Go on Debian 12 using APT, verifies the installation with a sample application, and manages multiple Go versions with both a manual source install and the Go Version Manager (GVM). By the end, you'll have a working Go installation with the ability to switch between multiple Go versions on demand.

Prerequisites: a Debian 12 server, SSH access, and a non-root user with sudo privileges.


Install Go on Debian 12

Go is available in the default APT package repositories on Debian 12.

1. Update the server's package information index:

$ sudo apt update
Enter fullscreen mode Exit fullscreen mode

2. Install Go:

$ sudo apt install golang -y
Enter fullscreen mode Exit fullscreen mode

3. View the installed Go version:

$ go version
Enter fullscreen mode Exit fullscreen mode

Output:

go version go1.19.8 linux/amd64
Enter fullscreen mode Exit fullscreen mode

Test the Go Installation

Create a sample application to verify that Go runs correctly on the server.

1. Create a new go-sample project directory:

$ mkdir go-sample
Enter fullscreen mode Exit fullscreen mode

2. Switch to the go-sample directory:

$ cd go-sample
Enter fullscreen mode Exit fullscreen mode

3. Initialize a new example.com/hello Go module in the directory:

$ go mod init example.com/hello
Enter fullscreen mode Exit fullscreen mode

4. Create a new main.go application file using a text editor like nano:

$ nano main.go
Enter fullscreen mode Exit fullscreen mode

5. Add the following contents to the main.go file:

package main

import "fmt"

func main() {
    fmt.Println("Hello from Go")
}
Enter fullscreen mode Exit fullscreen mode

Save and close the file. The application prints a greeting message when you run it.

6. Run the main.go application file:

$ go run main.go
Enter fullscreen mode Exit fullscreen mode

Output:

Hello from Go
Enter fullscreen mode Exit fullscreen mode

Install Multiple Go Versions

Maintaining multiple Go versions on your system lets you meet different project requirements. You can install specific versions manually from source, or use the Go Version Manager (GVM).

Install Multiple Go Versions From Source

1. Visit the Go releases webpage and download a specific Go version's archive file. For example, Go version 1.23.0:

$ wget https://go.dev/dl/go1.23.0.linux-amd64.tar.gz
Enter fullscreen mode Exit fullscreen mode

2. Download another Go version, such as 1.21.0:

$ wget https://go.dev/dl/go1.21.0.linux-amd64.tar.gz
Enter fullscreen mode Exit fullscreen mode

3. Create a new /usr/local/go1.23.0 directory to store the 1.23.0 Go version files:

$ sudo mkdir -p /usr/local/go1.23.0
Enter fullscreen mode Exit fullscreen mode

4. Create another /usr/local/go1.21.0 directory to store the 1.21.0 Go version files:

$ sudo mkdir -p /usr/local/go1.21.0
Enter fullscreen mode Exit fullscreen mode

5. Extract the 1.23.0 Go archive files to the /usr/local/go1.23.0 directory:

$ sudo tar -C /usr/local/go1.23.0 -xzf go1.23.0.linux-amd64.tar.gz
Enter fullscreen mode Exit fullscreen mode

6. Extract the 1.21.0 Go archive files to the /usr/local/go1.21.0 directory:

$ sudo tar -C /usr/local/go1.21.0 -xzf go1.21.0.linux-amd64.tar.gz
Enter fullscreen mode Exit fullscreen mode

7. Create a new PATH environment variable and set the 1.23.0 Go version to activate it as the default on the server:

$ export PATH=/usr/local/go1.23.0/go/bin:$PATH
Enter fullscreen mode Exit fullscreen mode

8. Verify the active Go version:

$ go version
Enter fullscreen mode Exit fullscreen mode

Output:

go version go1.23.0 linux/amd64
Enter fullscreen mode Exit fullscreen mode

9. Create a new go123 alias for the 1.23.0 Go version to access it using a single command:

$ echo "alias go123='/usr/local/go1.23.0/go/bin/go'" >> ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

10. Create another go121 alias for the 1.21.0 Go version to access it using a single command:

$ echo "alias go121='/usr/local/go1.21.0/go/bin/go'" >> ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

11. Reload your shell configuration to apply the changes:

$ source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

12. Run the alias command associated with the installed Go version. For example, run main.go using the 1.23.0 Go version:

$ go123 run main.go
Enter fullscreen mode Exit fullscreen mode

Output:

Hello from Go
Enter fullscreen mode Exit fullscreen mode

13. Run the main.go application file using the go121 alias command:

$ go121 run main.go
Enter fullscreen mode Exit fullscreen mode

Output:

Hello from Go
Enter fullscreen mode Exit fullscreen mode

Repeat this process for each Go version you want to install and run on the server.

Install Multiple Go Versions Using GVM

The Go Version Manager (GVM) lets you install and use specific Go versions without manually setting up a PATH variable or downloading binary packages.

1. Update the server's package information index:

$ sudo apt update
Enter fullscreen mode Exit fullscreen mode

2. Install all required GVM dependency packages:

$ sudo apt install -y curl git make binutils bison gcc
Enter fullscreen mode Exit fullscreen mode

3. Download and install GVM using the latest installation script:

$ bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
Enter fullscreen mode Exit fullscreen mode

4. Reload your shell configuration to apply the changes:

$ source ~/.gvm/scripts/gvm
Enter fullscreen mode Exit fullscreen mode

5. Use GVM to install a specific Go version. For example, Go version 1.20.8:

$ gvm install go1.20.8
Enter fullscreen mode Exit fullscreen mode

Output:

go1.20.8 successfully installed!
Enter fullscreen mode Exit fullscreen mode

6. Set the 1.20.8 Go version as the default:

$ gvm use go1.20.8 --default
Enter fullscreen mode Exit fullscreen mode

Output:

Now using version go1.20.8
Enter fullscreen mode Exit fullscreen mode

7. View the active Go version:

$ go version
Enter fullscreen mode Exit fullscreen mode

Output:

go version go1.20.8 linux/amd64
Enter fullscreen mode Exit fullscreen mode

8. Install another Go version. For example, Go version 1.21.5:

$ gvm install go1.21.5
Enter fullscreen mode Exit fullscreen mode

9. List all installed Go versions:

$ gvm list
Enter fullscreen mode Exit fullscreen mode

Output:

gvm gos (installed)

    go1.20.8
    go1.21.5
    system
Enter fullscreen mode Exit fullscreen mode

10. Switch to a specific Go version, such as go1.21.5, in your shell environment:

$ gvm use go1.21.5
Enter fullscreen mode Exit fullscreen mode

Next Steps

  • Explore Go modules and popular frameworks like Gin or Echo for building web services
  • Set up a CI pipeline that builds and tests your Go application on every commit
  • Cross-compile your Go application for other operating systems and architectures
  • Package your Go binary as a systemd service for production deployment

For the full guide with additional tips, visit the original article on Vultr Docs.

Top comments (0)