Installing Golang on Ubuntu
To run Golang programs on Ubuntu, you first need to install the Go environment. Common methods include using the APT package manager (for quick installation) or manually downloading the official binary (for version control).
Method 1: Install via APT Package Manager
Update the system’s package list and install Golang using:
sudo apt update
sudo apt install golang-go
Verify installation with go version, which should display the installed version (e.g., go version go1.20.3 linux/amd64).
Method 2: Manual Installation from Official Binary
For the latest stable version, download the .tar.gz package from the official Go website. Extract it to /usr/local and configure environment variables:
wget https://golang.org/dl/go1.23.0.linux-amd64.tar.gz # Replace with desired version
sudo tar -C /usr/local -xzf go1.23.0.linux-amd64.tar.gz
Configuring Environment Variables
After installation, set up environment variables to enable Go commands in any terminal session. Key variables include GOROOT (Go installation path), GOPATH (workspace directory), and PATH (binary executable paths).
~/.bashrc (or ~/.zshrc for Zsh) in a text editor:nano ~/.bashrc
export GOROOT=/usr/local/go # Default installation path
export GOPATH=$HOME/go # Workspace directory (customizable)
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin # Add Go binaries to PATH
source ~/.bashrc to load the updated configuration.Verify environment setup with go env, which should show correct values for GOROOT, GOPATH, and PATH.
Running a Simple Go Program
Create a basic “Hello, World!” program to test your setup:
mkdir -p ~/go_projects && cd ~/go_projects
go mod init example.com/hello
hello.go):package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
go run:go run hello.go
Expected output: Hello, World!\n.Optional: Configure Go Modules
Go Modules is the standard dependency management tool. Enable it by setting the GO111MODULE environment variable to on (default in newer Go versions):
go env -w GO111MODULE=on
This allows you to manage dependencies efficiently for larger projects.
Installing Development Tools (Optional)
For a better development experience, install tools like golint (code style checker) and delve (debugger):
go install golang.org/x/tools/gopls@latest # Language server for VS Code
go install github.com/go-delve/delve/cmd/dlv@latest # Debugger
Verify installations with gopls --version and dlv version.
Deploying a Go Application (Optional)
To deploy a Go application (e.g., a web server built with Fiber), compile it into a binary and run it:
cd ~/go_projects/my-fiber-app
go build -o my-fiber-app
./my-fiber-app
The app will start on the specified port (e.g., 3000).For production, consider using Nginx as a reverse proxy to handle HTTP requests and forward them to your Go application. Install Nginx with sudo apt install nginx, then configure a proxy pass in /etc/nginx/sites-available/default to forward traffic from port 80 to your app’s port (e.g., 3000). Restart Nginx with sudo systemctl restart nginx to apply changes.