Introduction / Why This Is Needed
Go (Golang) is a modern compiled language created by Google for efficient development of network services and utilities. Setting up a development environment on Linux is an essential first step for working with Go. After completing this guide, you will have a fully functional environment: the compiler installed, paths configured, and you will be able to create, build, and run Go projects.
You will get:
- Installed
gocompiler in the terminal - Configured environment variables
GOROOT,GOPATH,PATH - Ability to use
go get,go build,go run - A ready project structure (if you configure GOPATH)
Requirements / Preparation
Before starting, ensure that:
- You have access to a Linux terminal (Ubuntu, Debian, Fedora, Arch, or another distribution).
- Basic utilities are installed:
curlorwget,tar. - You have sudo privileges (if planning installation in system directories).
- Determine your system architecture:
uname -mx86_64→amd64aarch64→arm64
Step 1: Check for Existing Go and System Architecture
First, let's check if Go is already installed and determine the architecture.
# Check for installed Go (if any)
go version
# Determine architecture
uname -m
If the go version command outputs a version (e.g., go version go1.20 linux/amd64), Go is already installed. You can proceed to Step 5 to check environment variables or remove the old version before installing a new one.
Step 2: Choose an Installation Method
There are two main options:
| Method | Pros | Cons | When to choose |
|---|---|---|---|
Via package manager (apt, dnf, pacman) | Simple, automatically in PATH, updates via system | Version often not the latest | For quick installation if the latest version is not critical |
| From the official website (golang.org/dl) | Latest stable version, control over location | Requires manual PATH configuration | For development when new features or a specific version are needed |
Recommendation: If you are starting a new project or want to use Go Modules (the modern standard), choose installation from the official website.
Step 3: Installation via Package Manager
For Ubuntu/Debian:
# Update package cache
sudo apt update
# Install Go
sudo apt install golang
# Check version (should be, e.g., 1.18+ in modern repositories)
go version
For Fedora/RHEL/CentOS:
sudo dnf install golang
go version
For Arch Linux:
sudo pacman -S go
go version
Note: In some distributions, the package is called golang or go. After installation via package manager, environment variables are usually configured automatically. Proceed to Step 5.
Step 4: Installation from Official Website (Manual)
This method gives you the latest version and full control.
- Go to the downloads page (can be done via terminal):
# For amd64 wget https://go.dev/dl/go1.21.6.linux-amd64.tar.gz # For arm64 wget https://go.dev/dl/go1.21.6.linux-arm64.tar.gz
Check the latest version at golang.org/dl. - Remove the old version (if installed in /usr/local/go):
sudo rm -rf /usr/local/go - Extract the archive to /usr/local (requires sudo):
sudo tar -C /usr/local -xzf go1.21.6.linux-amd64.tar.gz - Ensure Go is added to PATH. Add lines to
~/.bashrc(or~/.zshrcfor zsh):# Open the file in an editor nano ~/.bashrc
Add at the end:export GOROOT=/usr/local/go export PATH=$PATH:$GOROOT/bin export GOPATH=$HOME/go export PATH=$PATH:$GOPATH/bin💡 Tip: If you don't want to install in a system directory, extract the archive to
~/goor~/local/goand specify the correspondingGOROOT. - Apply changes:
source ~/.bashrc
Step 5: Configure Environment Variables (if not done earlier)
Even when installing via package manager, it's worth checking/configuring GOPATH.
- Check current variables:
go env - Ensure GOPATH is set. If output shows
GOPATH="", add to~/.bashrc:export GOPATH=$HOME/go export PATH=$PATH:$GOPATH/bin - Create a directory for projects (if it doesn't exist):
mkdir -p $GOPATH/{src,bin,pkg} - Reload the shell:
source ~/.bashrc
Step 6: Verify the Setup
Run commands for a full check:
# 1. Check Go version
go version
# Expected output: go version go1.21.6 linux/amd64
# 2. Check environment variables
go env GOROOT GOPATH PATH
# GOROOT should point to /usr/local/go (or your directory)
# GOPATH should point to /home/your_user/go
# 3. Simple test project
mkdir -p $GOPATH/src/hello
cat > $GOPATH/src/hello/main.go << 'EOF'
package main
import "fmt"
func main() {
fmt.Println("Hello, FixPedia!")
}
EOF
# 4. Run
go run $GOPATH/src/hello/main.go
# Should output: Hello, FixPedia!
# 5. Build binary
go build -o $GOPATH/bin/hello $GOPATH/src/hello/main.go
hello
# Should output: Hello, FixPedia!
If all steps completed successfully — the environment is ready for work.
Common Issues
❌ go: command not found
Cause: Go not added to PATH.
Solution:
- Check that
~/.bashrc(or~/.zshrc) containsexport PATH=$PATH:/usr/local/go/bin. - Run
source ~/.bashrcor log out and back into the terminal.
❌ Permission error when extracting to /usr/local
Cause: Insufficient privileges.
Solution: Use sudo for extraction or install Go in your home directory (~/go).
❌ go env shows empty GOPATH
Cause: Variable not set.
Solution: Add export GOPATH=$HOME/go to ~/.bashrc and reload the shell.
❌ Outdated version in distribution repository
Cause: Linux repositories often contain stable but not the latest versions. Solution: Install from the official website (see Step 4).
❌ Go version conflict
Cause: Multiple Go versions installed, and go points to an undesired one.
Solution: Check which go and ls -l $(which go). Remove extra versions or adjust the order in PATH.
Advanced Configuration (Optional)
Configuring Modules (Go Modules)
Starting with Go 1.11, modules are the standard for dependency management. Ensure they are enabled:
go env -w GO111MODULE=on
Configuring a proxy for modules (if access is needed in Russia/China)
go env -w GOPROXY=https://goproxy.cn,direct
Autocompletion for bash/zsh
Install the plugin for autocompletion of go commands:
# For bash
go install golang.org/x/tools/cmd/godoc@latest
echo "source <(godoc -completion)" >> ~/.bashrc
Final Verification
After setup, perform a full workflow:
- Create a new module:
mkdir ~/projects/myapp && cd ~/projects/myapp go mod init github.com/username/myapp - Add simple code:
cat > main.go << 'EOF' package main import "fmt" func main() { fmt.Println("Go is ready!") } EOF - Build and run:
go build -o myapp . ./myapp
If you see Go is ready!, congratulations, the environment is fully functional!
What's next?
- Explore the official tutorial
- Set up an IDE (VS Code with Go extension, Goland)
- Master Go Modules for dependency management