How to Install Go on a VPS Server

Go, often referred to as Golang, is a modern programming language developed by Google. Thanks to its efficiency, simplicity, and robust support for concurrency, Go has become a popular choice for developing scalable web servers, cloud services, and command-line tools. If you rent a VPS (Virtual Private Server), installing Go is straightforward and enables you to leverage your server for application deployment and development. This guide walks you through installing Go on both Ubuntu and CentOS-based VPS servers, covering both package manager and manual (official binary) installation methods.

1. Prerequisites

Before you begin, ensure that you:

  • Have access to your VPS via SSH.
  • Have sudo or root privileges.
  • Know which Linux distribution your server is running (typically, Ubuntu/Debian or CentOS/RHEL).

2. Installing Go on Ubuntu

There are two popular ways to install Go on Ubuntu: using the package manager (easy but may not give the latest version) or manually installing from the official Go distribution (recommended for latest releases).

A. Install with the Ubuntu Package Manager

This method is convenient but may install an older version.

sudo apt update && sudo apt upgrade -y
sudo apt install golang-go -y

After installation, verify Go is installed:

go version

If you see the installed version printed, Go is ready to use.

B. Installing Latest Go from Official Binaries

Download the Official Go Binary: Visit the Go downloads page and copy the latest Linux tar.gz link. Or use:

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

Extract the Archive:

sudo tar -xzvf go.tar.gz -C /usr/local

Set Up Environment Variables:

Add Go’s bin directory to your PATH. Add this line to ~/.profile or ~/.bashrc:

echo 'export PATH=$PATH:/usr/local/go/bin:$HOME/go/bin' >> ~/.profile
source ~/.profile

Verify Installation:

go version

You should see the version you installed.

3. Installing Go on CentOS

Just like on Ubuntu, you can use the default repository or install the latest version manually.

A. Manual Installation (Recommended)

Download the Latest Go Package:

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

Extract the Archive:

sudo tar -C /usr/local -xzf go1.21.4.linux-amd64.tar.gz

Add Go to PATH: Append the following to your ~/.bash_profile or ~/.bashrc:

export PATH=$PATH:/usr/local/go/bin
source ~/.bash_profile

Check the Installation:

go version

The Go version should display, confirming the installation.

4. Testing Your Go Installation

To ensure Go is working and your environment is set up correctly, create a simple Hello World program:

vi hello.go

Paste following content into it and save.

package main
import "fmt"
func main() {
    fmt.Println("Hello, Go VPS!")
}

Now run the Go program using following command.

go run hello.go

You should see: Hello, Go VPS! printed to your terminal.

5. Final Tips

  • Uninstalling Go: Remove the Go directory from /usr/local and clean up your PATH.
  • Managing Different Versions: If working with multiple Go versions, consider using tools like gvm for Go version management.
  • Automating Updates: Set a reminder to update Go regularly for security and feature improvements.

Setting up Go on your VPS is a fast process that opens a world of development possibilities—from small CLI tools to entire web backends. With Go ready on your VPS, you can now start building, deploying, and running high-performance applications confidently.