Skip to content

Install Go on Linux

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:

Golang by www.golangdev.cn edit