Installing Go on Linux requires extracting the binary release to a directory and adjusting your shell profile. If you are looking for a clear, step-by-step guide on how to install Go on Linux, you have come to the right place. Go, also known as Golang, is a fast and efficient programming language developed by Google. This guide will walk you through the entire process, from downloading the latest version to verifying your installation.
We will cover multiple methods, including the official binary release and package managers. By the end, you will have Go running on your Linux system. Let’s get started.
Prerequisites For Installing Go On Linux
Before you begin, make sure you have a few things ready. You need a Linux distribution like Ubuntu, Debian, Fedora, or CentOS. You also need sudo or root access to install software system-wide. If you are on a personal machine, you likely have this already.
You should also have a terminal open. Most Linux systems have a terminal emulator installed by default. You can open it from your applications menu or by pressing Ctrl+Alt+T.
Finally, check your system architecture. Most modern Linux systems are 64-bit (x86_64 or amd64). You can verify this by running:
uname -m
If the output shows x86_64, you are on a 64-bit system. If it shows aarch64, you are on an ARM64 system. This matters for downloading the correct Go binary.
How To Install Go On Linux Using The Official Binary
This is the recommended method for most users. It gives you the latest version and avoids potential issues with outdated package repositories. Follow these steps carefully.
Step 1: Download The Go Binary
First, visit the official Go download page. You can do this from your browser or use wget in the terminal. Using the terminal is faster and more reliable.
Open your terminal and navigate to your home directory or a temporary folder:
cd ~
Now, download the latest Go binary for Linux. As of this writing, the latest version is 1.22.2. You can check the official site for the current version. Use this command, replacing the version number if needed:
wget https://go.dev/dl/go1.22.2.linux-amd64.tar.gz
If you are on ARM64, replace linux-amd64 with linux-arm64. This downloads the tarball to your current directory.
Step 2: Verify The Download (Optional But Recommended)
Go provides checksums to verify the integrity of your download. You can download the checksum file from the same page. Run:
wget https://go.dev/dl/go1.22.2.linux-amd64.tar.gz.sha256
Then, check the checksum:
sha256sum -c go1.22.2.linux-amd64.tar.gz.sha256
If the output says OK, your download is intact. If not, delete the file and download again.
Step 3: Remove Any Previous Go Installation
If you have an older version of Go installed, remove it first. The default installation location is /usr/local/go. Run:
sudo rm -rf /usr/local/go
This ensures no conflicts with the new version. If you installed Go via a package manager, you might need to uninstall it differently. We will cover that later.
Step 4: Extract The Archive
Now, extract the downloaded tarball to /usr/local. This creates a /usr/local/go directory. Run:
sudo tar -C /usr/local -xzf go1.22.2.linux-amd64.tar.gz
This command uses sudo because you are writing to a system directory. The -C flag tells tar to change to that directory before extracting.
Step 5: Add Go To Your PATH
To use Go commands from anywhere, you need to add /usr/local/go/bin to your PATH. This is done by editing your shell profile. The file depends on your shell.
For Bash (most common), edit ~/.bashrc or ~/.bash_profile. For Zsh, edit ~/.zshrc. Open the file with a text editor like nano:
nano ~/.bashrc
Add the following line at the end of the file:
export PATH=$PATH:/usr/local/go/bin
Save and exit (in nano, press Ctrl+X, then Y, then Enter). Then, reload your profile:
source ~/.bashrc
Step 6: Verify The Installation
Check that Go is installed correctly. Run:
go version
You should see output like go version go1.22.2 linux/amd64. If you see this, congratulations! You have successfully installed Go on Linux.
You can also test a simple program. Create a file called hello.go with this content:
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
Run it with:
go run hello.go
You should see Hello, Go! printed. This confirms everything works.
How To Install Go On Linux Using A Package Manager
If you prefer using your system’s package manager, you can install Go that way. However, this often provides an older version. It is still a valid method for many users.
On Ubuntu Or Debian
Update your package list and install Go:
sudo apt update
sudo apt install golang-go
This installs the golang-go package. Check the version with go version. It might be older than the official release.
On Fedora Or CentOS
For Fedora, use dnf:
sudo dnf install golang
For CentOS or RHEL, you might need to enable EPEL first:
sudo yum install epel-release
sudo yum install golang
On Arch Linux
Arch users can install Go from the official repositories:
sudo pacman -S go
This usually provides a recent version.
Pros And Cons Of Package Manager Installation
- Pros: Easy, automatic updates, integrates with system
- Cons: Older version, may not have latest features or security patches
If you need the latest Go version, stick with the binary installation method.
Setting Up Your Go Workspace
After installation, you should set up your Go workspace. This is where your projects live. In older versions of Go, you needed a specific GOPATH. Modern Go uses modules, so you can work anywhere.
Understanding GOPATH And Modules
In Go 1.16 and later, modules are the default. You no longer need a fixed GOPATH. However, it is still good to know where Go stores downloaded packages. By default, GOPATH is $HOME/go.
You can check your GOPATH with:
go env GOPATH
If you want to change it, add this to your shell profile:
export GOPATH=$HOME/go
Then, add the GOPATH/bin directory to your PATH:
export PATH=$PATH:$GOPATH/bin
Creating Your First Project
Create a new directory for your project:
mkdir myproject
cd myproject
Initialize a Go module:
go mod init myproject
This creates a go.mod file. Now you can write Go code in this directory. The module system handles dependencies automatically.
Troubleshooting Common Installation Issues
Sometimes things go wrong. Here are common problems and solutions.
Go Command Not Found
If you get command not found, your PATH is not set correctly. Check your shell profile file. Make sure the export line is correct. Then, reload the profile with source ~/.bashrc.
You can also check if Go is installed by running:
/usr/local/go/bin/go version
If that works, the issue is definitely your PATH.
Permission Denied Errors
If you get permission errors during extraction, you forgot to use sudo. Run the tar command with sudo again.
Wrong Architecture
If you downloaded the wrong binary, you might get errors like exec format error. Check your system architecture with uname -m and download the correct file.
Version Mismatch
If you have multiple Go versions, you might see unexpected behavior. Remove the old version first. You can check all Go binaries with:
which -a go
This shows all locations of the go command. Remove any you do not need.
Updating Go On Linux
To update Go to a newer version, you follow a similar process. First, remove the old version:
sudo rm -rf /usr/local/go
Then, download the new binary and extract it. Update your PATH if needed. That is it. Your existing projects will work with the new version.
If you installed via a package manager, use your system’s update command:
sudo apt upgrade golang-go
But again, this might not give you the latest version.
Uninstalling Go On Linux
If you need to remove Go completely, follow these steps. Remove the installation directory:
sudo rm -rf /usr/local/go
Remove the GOPATH directory if you want:
rm -rf ~/go
Remove the PATH additions from your shell profile. Then, reload your profile.
If you installed via a package manager, use the remove command:
sudo apt remove golang-go
This cleans up system files.
Frequently Asked Questions
Can I Install Go On Linux Without Sudo?
Yes, you can. Instead of extracting to /usr/local, extract to your home directory. For example:
tar -C ~ -xzf go1.22.2.linux-amd64.tar.gz
Then, add ~/go/bin to your PATH. This is useful if you do not have root access.
What Is The Difference Between Golang-go And Golang?
On Debian-based systems, golang-go is the Go compiler and tools. golang is a metapackage that installs the entire Go ecosystem. Usually, golang-go is enough.
How Do I Install A Specific Version Of Go On Linux?
Download the specific version from the Go download page. Replace the version number in the download URL. For example, for Go 1.21.0, use go1.21.0.linux-amd64.tar.gz.
Is It Safe To Install Go From The Official Binary?
Yes, it is safe. The official binary is signed and checksums are provided. Always verify the checksum to ensure the file has not been tampered with.
Do I Need To Set GOPATH Anymore?
Not necessarily. With Go modules, you can work anywhere. However, setting GOPATH is still useful for managing downloaded packages. The default $HOME/go works fine.
Final Thoughts On Installing Go On Linux
You now know how to install Go on Linux using two methods. The binary installation is the most reliable and gives you the latest version. The package manager method is easier but may lag behind.
Remember to set your PATH correctly and verify the installation. With Go installed, you can start building fast, efficient applications. Whether you are a beginner or an experienced developer, Go is a powerful tool.
If you run into any issues, refer back to the troubleshooting section. The Go community is also very helpful. Check the official documentation or forums for more help.
Happy coding with Go on your Linux system!