GoGolangBuild7 min read

Install Go (Golang) and build applications

Install Go, build applications, and run them as services on your Linux VPS.


Go is a compiled language ideal for web services, APIs, and high-performance CLI tools.

Step 1 — Download and install Go

bash
cd /tmp
curl -LO https://go.dev/dl/go1.22.4.linux-amd64.tar.gz
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf go1.22.4.linux-amd64.tar.gz

Step 2 — Configure PATH

Add to the end of ~/.bashrc:

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

Apply changes:

bash
source ~/.bashrc
go version

Step 3 — Create a sample project

bash
mkdir -p ~/myapi && cd ~/myapi
go mod init myapi

Create main.go:

bash
cat > main.go << 'EOF'
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "API running on Baires Host")
    })
    fmt.Println("Server on :8080")
    http.ListenAndServe(":8080", nil)
}
EOF

Step 4 — Build

bash
go build -o myapi .

This generates a static binary with no dependencies.

Step 5 — Run

bash
./myapi

Step 6 — Create systemd service

Create /etc/systemd/system/myapi.service:

ini
[Unit]
Description=My Go API
After=network.target

[Service]
Type=simple
User=deploy
WorkingDirectory=/home/deploy/myapi
ExecStart=/home/deploy/myapi/myapi
Restart=on-failure
RestartSec=5
Environment=PORT=8080

[Install]
WantedBy=multi-user.target

Enable the service:

bash
sudo systemctl daemon-reload
sudo systemctl enable myapi
sudo systemctl start myapi
sudo systemctl status myapi

Step 7 — Production build

bash
# Optimized build
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags='-s -w' -o myapi .

Flags:

  • -s -w: strips debug info (smaller binary)
  • CGO_ENABLED=0: static binary without C dependencies

Step 8 — Update Go

bash
cd /tmp
curl -LO https://go.dev/dl/go1.XX.X.linux-amd64.tar.gz
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf go1.XX.X.linux-amd64.tar.gz
go version

Go is perfect for high-performance services on your Baires Host VPS: lightweight binaries, instant startup, and low memory usage.


Was this guide helpful?