Setting up Go on Linux begins with downloading the official tarball and configuring your PATH environment variable. If you’ve been searching for a clear guide on how to install go linux, you’re in the right place. This article walks you through every step, from downloading the right package to verifying your installation works. Whether you’re a beginner or a seasoned developer, these instructions are designed to be simple and direct.
Go, also known as Golang, is a statically typed, compiled programming language created by Google. It’s known for its simplicity, efficiency, and strong support for concurrent programming. Installing it on Linux is straightforward, but you need to follow the correct steps to avoid common pitfalls.
In this guide, we’ll cover multiple methods: using the official tarball, using a package manager, and even compiling from source. Each method has its pros and cons, but the tarball method is the most reliable and recommended by the Go team.
Prerequisites For Installing Go On Linux
Before you start, make sure your Linux system is up to date. You’ll need a user account with sudo privileges or root access. Also, ensure you have a stable internet connection to download the Go tarball.
- A Linux distribution (Ubuntu, Debian, Fedora, CentOS, Arch, etc.)
- Terminal access
- Basic familiarity with command-line operations
- At least 500 MB of free disk space
Most modern Linux systems come with essential tools like wget or curl pre-installed. If not, you can install them using your package manager. For example, on Ubuntu, run sudo apt install wget curl.
How To Install Go Linux Using The Official Tarball
This is the most common and recommended method. The Go team provides pre-compiled binaries for Linux, which you can download and extract directly. Here’s the step-by-step process.
Step 1: Remove Any Previous Go Installation
If you have an older version of Go installed, remove it first. This prevents conflicts. Use the following command to delete the existing Go directory:
sudo rm -rf /usr/local/go
Also, remove any Go-related lines from your shell profile (like ~/.bashrc or ~/.zshrc). This ensures a clean slate for the new installation.
Step 2: Download The Latest Go Tarball
Visit the official Go download page at https://go.dev/dl/ to find the latest version. As of this writing, the latest stable release is Go 1.22.2. Copy the download link for the Linux tarball (usually ends with linux-amd64.tar.gz).
Use wget or curl to download it. For example:
wget https://go.dev/dl/go1.22.2.linux-amd64.tar.gz
Or with curl:
curl -O https://go.dev/dl/go1.22.2.linux-amd64.tar.gz
This downloads the tarball to your current directory. Make sure you download the correct architecture (amd64 for 64-bit systems, arm64 for ARM-based systems).
Step 3: Verify The Tarball Checksum (Optional But Recommended)
To ensure the file hasn’t been tampered with, verify its SHA256 checksum. Download the checksum file from the same page:
wget https://go.dev/dl/go1.22.2.linux-amd64.tar.gz.sha256
Then run:
sha256sum -c go1.22.2.linux-amd64.tar.gz.sha256
If the output says OK, the file is valid. If not, delete the tarball and download it again.
Step 4: Extract The Tarball To /Usr/local
Now extract the downloaded archive to the /usr/local directory. This is the standard location for Go installations.
sudo tar -C /usr/local -xzf go1.22.2.linux-amd64.tar.gz
This command extracts the Go files into /usr/local/go. The -C flag specifies the target directory, and -xzf extracts the gzipped tarball.
Step 5: Configure The PATH Environment Variable
To run Go commands from anywhere, add the Go binary directory to your PATH. Open your shell profile file. For Bash, it’s ~/.bashrc; for Zsh, it’s ~/.zshrc.
nano ~/.bashrc
Add the following line at the end of the file:
export PATH=$PATH:/usr/local/go/bin
Save and exit. Then reload the profile:
source ~/.bashrc
For Zsh, use source ~/.zshrc instead.
Step 6: Verify The Installation
Check that Go is installed correctly by running:
go version
You should see output like go version go1.22.2 linux/amd64. If you see this, the installation was successful.
Also, test that the environment is set up properly:
go env
This prints all Go environment variables. Look for GOROOT (should be /usr/local/go) and GOPATH (defaults to $HOME/go).
How To Install Go Linux Using A Package Manager
Many Linux distributions offer Go through their official repositories. This method is simpler but may not provide the latest version. Here’s how to do it for popular distros.
Ubuntu And Debian
Use apt to install Go:
sudo apt update
sudo apt install golang-go
This installs the version available in the repository. To check the version, run go version. If you need a newer version, use the tarball method instead.
Fedora And CentOS
On Fedora, use dnf:
sudo dnf install golang
For CentOS or RHEL, enable EPEL first, then install:
sudo yum install epel-release
sudo yum install golang
Arch Linux
Arch users can install Go from the community repository:
sudo pacman -S go
This usually provides a recent version. After installation, verify with go version.
Note: Package manager installations often set up the PATH automatically. But if you encounter issues, manually add /usr/lib/go/bin (or similar) to your PATH.
How To Install Go Linux From Source
Compiling Go from source is for advanced users who need custom builds or want to contribute to the Go project. It’s more time-consuming but gives you full control.
Prerequisites For Building From Source
You need a C compiler (like GCC), make, and Git. Install them if missing:
sudo apt install build-essential git
Step 1: Clone The Go Repository
Clone the official Go source code from GitHub:
git clone https://go.googlesource.com/go
cd go
Step 2: Checkout The Desired Version
List available tags to choose a version:
git tag
Checkout the version you want, for example:
git checkout go1.22.2
Step 3: Build Go
Run the bootstrap script. This compiles Go using an existing Go compiler (bootstrap). If you don’t have Go installed, you need to install a previous version first (e.g., Go 1.4).
cd src
./all.bash
This process takes several minutes. If successful, you’ll see a message like “ALL TESTS PASSED”. The compiled binaries are in /go/bin.
Step 4: Set Up The Environment
Add the bin directory to your PATH:
export PATH=$PATH:$(pwd)/../bin
For permanent access, add this line to your shell profile.
Setting Up A Go Workspace
After installation, you need to set up a workspace. Go uses a specific directory structure for projects. By default, the workspace is at $HOME/go. You can change this by setting the GOPATH environment variable.
Creating The Workspace Directory
Create the default workspace:
mkdir -p $HOME/go/{bin,src,pkg}
The src directory holds your source code, pkg stores compiled packages, and bin contains compiled binaries.
Configuring GOPATH
Add the following to your shell profile:
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
Reload the profile with source ~/.bashrc.
Testing Your First Go Program
Create a simple “Hello, World” program to test everything works:
mkdir -p $GOPATH/src/hello
cd $GOPATH/src/hello
nano main.go
Add the following code:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Save and exit. Then run:
go run main.go
You should see “Hello, World!” printed. If so, your Go installation is fully functional.
Common Issues And Troubleshooting
Even with a straightforward process, you might encounter issues. Here are common problems and their solutions.
Go Command Not Found
If you get “command not found” after installation, your PATH isn’t set correctly. Check that you added the Go bin directory to PATH and reloaded your profile. Also, ensure you didn’t make a typo in the export line.
Permission Denied Errors
If you see permission errors when extracting the tarball, use sudo. If you get permission issues when running Go commands, check that the Go binary has execute permissions:
sudo chmod +x /usr/local/go/bin/go
Version Mismatch
If you have multiple Go installations, the wrong version might be in your PATH. Use which go to find which binary is being used. Remove or rename the old installation.
GOPATH Not Set
If Go complains about a missing GOPATH, set it explicitly. Add the export line to your profile as shown earlier.
Uninstalling Go From Linux
If you need to remove Go, the process depends on how you installed it.
Remove Tarball Installation
Simply delete the Go directory:
sudo rm -rf /usr/local/go
Also, remove the PATH lines from your shell profile.
Remove Package Manager Installation
Use your package manager to uninstall:
sudo apt remove golang-go # Ubuntu/Debian
sudo dnf remove golang # Fedora
sudo pacman -R go # Arch
Remove Source Installation
Delete the cloned repository directory and any binaries you placed in system paths.
Frequently Asked Questions
What Is The Easiest Way To Install Go On Linux?
The easiest method is using your distribution’s package manager. For example, sudo apt install golang-go on Ubuntu. However, the tarball method gives you the latest version and is still very simple.
How Do I Update Go On Linux?
To update, download the latest tarball and repeat the extraction steps. For package manager installations, use sudo apt upgrade golang-go (or equivalent). Always remove the old version first.
Can I Install Multiple Versions Of Go On Linux?
Yes, you can install multiple versions in different directories. Use symlinks or modify your PATH to switch between them. Tools like gvm (Go Version Manager) make this easier.
Why Is My Go Installation Not Working After Following The Steps?
Check that you’ve added the correct PATH and reloaded your profile. Also, verify the tarball checksum and ensure you downloaded the correct architecture. If using a package manager, ensure the package is fully installed.
Do I Need To Set GOPATH For Go To Work?
In Go 1.16 and later, the default GOPATH is $HOME/go. You don’t need to set it explicitly unless you want a custom location. However, setting it can help avoid confusion.
Final Thoughts On Installing Go On Linux
Installing Go on Linux is a simple process once you understand the steps. The tarball method is the most reliable and gives you the latest version. Package managers are convenient but may lag behind. Compiling from source is only for specific needs.
Remember to always verify your installation with go version and test with a simple program. If you run into issues, the troubleshooting section above should help. With Go installed, you’re ready to start building efficient, concurrent applications.
Now you know how to install go linux using multiple methods. Choose the one that fits your needs and start coding. The Go community is welcoming, and the language’s simplicity makes it a joy to learn. Happy coding!