Installing Go (Golang) on WSL Ubuntu

Installing Go (Golang) on WSL Ubuntu This guide walks you through installing Go programming language on Windows Subsystem for Linux (WSL) with Ubuntu. Prerequisites WSL with Ubuntu installed and properly configured Internet connection Basic terminal knowledge Installation Steps There are two main methods to install Go on WSL Ubuntu: Method 1: Using the Official Go Binary (Recommended) This method ensures you get the latest version of Go directly from the official website: # 1. Update your Ubuntu packages sudo apt update sudo apt upgrade -y # 2. Install required packages sudo apt install -y wget git # 3. Check the latest Go version # Visit https://go.dev/dl/ for the latest version # As of May 2025, Go 1.24.2 is the latest stable version # 4. Download the Go binary package # Replace 1.24.2 with the latest version as needed # For standard Intel/AMD processors (most common): wget https://dl.google.com/go/go1.24.2.linux-amd64.tar.gz # For ARM64 architecture (e.g., if using WSL on ARM-based systems): # wget https://dl.google.com/go/go1.24.2.linux-arm64.tar.gz # 5. Extract the archive to /usr/local # This requires root permissions # For AMD64 architecture: sudo tar -C /usr/local -xzf go1.24.2.linux-amd64.tar.gz # For ARM64 architecture: # sudo tar -C /usr/local -xzf go1.24.2.linux-arm64.tar.gz # 6. Remove the downloaded archive to save space (optional) rm go1.24.2.linux-amd64.tar.gz # 7. Create Go workspace directories mkdir -p ~/go/{bin,src,pkg} Method 2: Using Ubuntu's Package Manager (Simpler but may not be latest) # Install Go using apt sudo apt update sudo apt install -y golang-go # Create Go workspace directories mkdir -p ~/go/{bin,src,pkg} Configuring Go Environment After installing Go, you need to set up environment variables. Add the following to your shell configuration file: For Bash (default in Ubuntu) # Open .bashrc file nano ~/.bashrc # Add these lines at the end of the file export GOROOT=/usr/local/go export GOPATH=$HOME/go export PATH=$PATH:$GOROOT/bin:$GOPATH/bin # Save and exit (Ctrl+O, Enter, Ctrl+X) # Apply changes immediately source ~/.bashrc For Zsh (if you use it) # Open .zshrc file nano ~/.zshrc # Add these lines at the end of the file export GOROOT=/usr/local/go export GOPATH=$HOME/go export PATH=$PATH:$GOROOT/bin:$GOPATH/bin # Save and exit (Ctrl+O, Enter, Ctrl+X) # Apply changes immediately source ~/.zshrc Verifying the Installation # Check Go version go version # Check Go environment go env You should see output similar to: go version go1.24.2 linux/amd64 Creating a Simple Hello World Program Let's create a simple Go program to verify the installation works correctly: # Navigate to your Go workspace cd ~/go/src # Create a new directory for your project mkdir hello cd hello # Create a new Go file nano hello.go Add the following code to hello.go: package main import "fmt" func main() { fmt.Println("Hello, World from Go on WSL!") } Save and exit the editor, then compile and run the program: # Run the program directly go run hello.go # Or build and then run the executable go build ./hello Setting Up VS Code Integration (Optional) If you use Visual Studio Code, you can enhance your Go development experience: Install VS Code on Windows Install the "Remote - WSL" extension Install the "Go" extension when working in WSL Open your WSL terminal and navigate to your project Run code . to open VS Code with WSL integration Troubleshooting Common Issues Command 'go' not found If you encounter this error even after installation

May 7, 2025 - 08:18
 0
Installing Go (Golang) on WSL Ubuntu

Installing Go (Golang) on WSL Ubuntu

This guide walks you through installing Go programming language on Windows Subsystem for Linux (WSL) with Ubuntu.

Prerequisites

  • WSL with Ubuntu installed and properly configured
  • Internet connection
  • Basic terminal knowledge

Installation Steps

There are two main methods to install Go on WSL Ubuntu:

Method 1: Using the Official Go Binary (Recommended)

This method ensures you get the latest version of Go directly from the official website:

# 1. Update your Ubuntu packages
sudo apt update
sudo apt upgrade -y

# 2. Install required packages
sudo apt install -y wget git

# 3. Check the latest Go version
# Visit https://go.dev/dl/ for the latest version
# As of May 2025, Go 1.24.2 is the latest stable version

# 4. Download the Go binary package
# Replace 1.24.2 with the latest version as needed
# For standard Intel/AMD processors (most common):
wget https://dl.google.com/go/go1.24.2.linux-amd64.tar.gz

# For ARM64 architecture (e.g., if using WSL on ARM-based systems):
# wget https://dl.google.com/go/go1.24.2.linux-arm64.tar.gz

# 5. Extract the archive to /usr/local
# This requires root permissions
# For AMD64 architecture:
sudo tar -C /usr/local -xzf go1.24.2.linux-amd64.tar.gz
# For ARM64 architecture:
# sudo tar -C /usr/local -xzf go1.24.2.linux-arm64.tar.gz

# 6. Remove the downloaded archive to save space (optional)
rm go1.24.2.linux-amd64.tar.gz

# 7. Create Go workspace directories
mkdir -p ~/go/{bin,src,pkg}

Method 2: Using Ubuntu's Package Manager (Simpler but may not be latest)

# Install Go using apt
sudo apt update
sudo apt install -y golang-go

# Create Go workspace directories
mkdir -p ~/go/{bin,src,pkg}

Configuring Go Environment

After installing Go, you need to set up environment variables. Add the following to your shell configuration file:

For Bash (default in Ubuntu)

# Open .bashrc file
nano ~/.bashrc

# Add these lines at the end of the file
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

# Save and exit (Ctrl+O, Enter, Ctrl+X)

# Apply changes immediately
source ~/.bashrc

For Zsh (if you use it)

# Open .zshrc file
nano ~/.zshrc

# Add these lines at the end of the file
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

# Save and exit (Ctrl+O, Enter, Ctrl+X)

# Apply changes immediately
source ~/.zshrc

Verifying the Installation

# Check Go version
go version

# Check Go environment
go env

You should see output similar to:

go version go1.24.2 linux/amd64

Creating a Simple Hello World Program

Let's create a simple Go program to verify the installation works correctly:

# Navigate to your Go workspace
cd ~/go/src

# Create a new directory for your project
mkdir hello
cd hello

# Create a new Go file
nano hello.go

Add the following code to hello.go:

package main

import "fmt"

func main() {
    fmt.Println("Hello, World from Go on WSL!")
}

Save and exit the editor, then compile and run the program:

# Run the program directly
go run hello.go

# Or build and then run the executable
go build
./hello

Setting Up VS Code Integration (Optional)

If you use Visual Studio Code, you can enhance your Go development experience:

  1. Install VS Code on Windows
  2. Install the "Remote - WSL" extension
  3. Install the "Go" extension when working in WSL
  4. Open your WSL terminal and navigate to your project
  5. Run code . to open VS Code with WSL integration

Troubleshooting Common Issues

Command 'go' not found

If you encounter this error even after installation