Skip to content

Install Go on Linux ​

Before You Start

  • Applies to: Go 1.21+ (examples verified with Go 1.22) · Prerequisites: Get Started · Read time: about 7 min

This guide will help you install Go language on Linux system.

Download Go ​

First, download the Go installation package suitable for your Linux system from the official website.

Choose the corresponding version based on your system architecture:

  • linux-amd64 - 64-bit x86 architecture
  • linux-arm64 - 64-bit ARM architecture
  • linux-386 - 32-bit x86 architecture
  • linux-armv6l - ARMv6 architecture

Installation Steps ​

1. Download the installation package ​

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

2. Delete old version (if any) ​

bash
sudo rm -rf /usr/local/go

3. Extract to /usr/local ​

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

4. Configure environment variables ​

Add the following to your ~/.profile or ~/.bashrc file:

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

5. Make environment variables take effect ​

bash
source ~/.profile

Or:

bash
source ~/.bashrc

6. Verify installation ​

bash
go version

If the installation is successful, you should see output similar to:

go version go1.21.3 linux/amd64

Custom Installation Path ​

If you want to install Go to a custom path, you can:

1. Extract to custom directory ​

bash
mkdir -p ~/go
tar -C ~/go -xzf go1.21.3.linux-amd64.tar.gz

2. Configure environment variables ​

Add the following to your ~/.profile or ~/.bashrc file:

bash
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin

3. Make environment variables take effect ​

bash
source ~/.profile

Configure GOPATH ​

By default, Go uses $HOME/go as the workspace directory. If you want to use a different path, you can set the GOPATH environment variable:

bash
export GOPATH=$HOME/gopath
export PATH=$PATH:$GOPATH/bin

Verify Installation ​

Create a simple Go program to verify that the installation is successful:

bash
mkdir -p $HOME/hello
cd $HOME/hello
cat > hello.go << 'EOF'
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}
EOF
go run hello.go

If the output is Hello, World!, it means the installation is successful.

Uninstall ​

If you need to uninstall Go, just delete the installation directory:

bash
sudo rm -rf /usr/local/go

Then remove the Go-related environment variables from your ~/.profile or ~/.bashrc file.

Troubleshooting ​

Permission denied ​

If you encounter a permission denied error, please use sudo to execute the command:

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

Command not found ​

If the go command is not found, please check:

  1. Whether the PATH environment variable is configured correctly
  2. Whether the environment variables have taken effect
  3. Try to restart the terminal or re-login

Version mismatch ​

If the displayed version does not match the installed version, please check:

  1. Whether there are multiple Go installations
  2. Whether the PATH environment variable points to the correct Go installation directory

Next Steps ​

After installation is complete, you can:

Complete Example ​

After installing, use the program below to verify the toolchain from the command line. Copy it into a file and run go run main.go.

go
package main

import (
	"fmt"
	"os/exec"
	"runtime"
	"strings"
)

func main() {
	out, err := exec.Command("go", "version").Output()
	fmt.Println("go command found in PATH:", err == nil)
	fmt.Println("version output starts with go:", err == nil && strings.HasPrefix(string(out), "go"))
	fmt.Println("GOROOT is set:", runtime.GOROOT() != "")
}

Expected output:

text
go command found in PATH: true
version output starts with go: true
GOROOT is set: true

Common Pitfalls ​

  • Add the bin directory to PATH. Unpack to /usr/local/go and export /usr/local/go/bin in ~/.profile or /etc/profile.d.
  • Distribution packages lag behind. For the latest Go, use the official tarball instead of the distro repository.
  • Keep old versions switchable. Replace /usr/local/go with a symlink (for example to go1.22) when multiple versions must coexist.
  • Avoid sudo go build. Root-owned caches cause permission errors for later builds.
  • Remove the old directory before unpacking a new one. Mixing two trees under the same path leads to confusing version output.

Change Log ​

  • 2026-10-14: Added "Before You Start" notes, a complete runnable example with expected output, common pitfalls, and this change log.

Golang by www.golangdev.cn · Adapted in part from Golang 中文学习文档 (MIT, © 寒江蓑笠翁)