Skip to main content

Command Palette

Search for a command to run...

How to Install Go on Linux

Published
7 min readView as Markdown

Installing Go on Linux is easier than you might think. Whether you’re a beginner or an experienced developer, setting up Go on your Linux machine opens up a world of programming possibilities. In this guide, I’ll walk you through the entire process, making sure you get Go up and running smoothly.

You’ll learn about different installation methods, how to verify your setup, and tips to keep your Go environment clean and efficient. By the end, you’ll be ready to start coding in Go on your Linux system without any hassle.

Why Install Go on Linux?

Go, also known as Golang, is a popular programming language created by Google. It’s known for its simplicity, speed, and strong support for concurrent programming. Installing Go on Linux is a common choice because Linux offers a stable and developer-friendly environment.

Here’s why you might want to install Go on Linux:

  • Performance: Go compiles to native machine code, making it fast.
  • Cross-platform: Write code on Linux and deploy it anywhere.
  • Strong community: Lots of open-source tools and libraries.
  • Easy setup: Installing Go on Linux is straightforward and quick.

With these benefits, it’s no surprise that many developers prefer Linux for Go development.

Preparing Your Linux System for Go Installation

Before installing Go, it’s important to prepare your Linux system. This ensures a smooth installation process and avoids common pitfalls.

Check Your Linux Distribution and Version

Go supports most popular Linux distributions, including Ubuntu, Fedora, Debian, and CentOS. However, the installation steps might vary slightly depending on your distro.

  • Use lsb_release -a or cat /etc/os-release to check your Linux version.
  • Make sure your system is up to date by running:
    sudo apt update && sudo apt upgrade   # For Debian/Ubuntu
    sudo dnf update                       # For Fedora
    sudo yum update                       # For CentOS
    

Remove Previous Go Versions

If you have an older version of Go installed, it’s best to remove it to avoid conflicts.

  • Check if Go is installed:
    go version
    
  • If it’s installed via package manager, remove it:
    sudo apt remove golang-go   # Debian/Ubuntu
    sudo dnf remove golang      # Fedora
    sudo yum remove golang      # CentOS
    
  • Also, delete any Go directories in /usr/local/go if present:
    sudo rm -rf /usr/local/go
    

Installing Go on Linux: Step-by-Step Guide

There are two main ways to install Go on Linux: using the official binary tarball or via your distribution’s package manager. I’ll cover both methods so you can choose what fits you best.

Method 1: Installing Go Using the Official Binary

This method installs the latest stable Go version directly from the official source.

Step 1: Download the Latest Go Binary

  • Visit the official Go downloads page: https://go.dev/dl/
  • Find the latest Linux tarball (e.g., go1.21.0.linux-amd64.tar.gz).
  • Download it using wget:
    wget https://go.dev/dl/go1.21.0.linux-amd64.tar.gz
    

Step 2: Extract the Archive

  • Extract the tarball to /usr/local:
    sudo tar -C /usr/local -xzf go1.21.0.linux-amd64.tar.gz
    

Step 3: Set Up Environment Variables

  • Add Go’s binary directory to your PATH by editing your shell profile.
  • For Bash, open ~/.bashrc or ~/.profile and add:
    export PATH=$PATH:/usr/local/go/bin
    
  • For Zsh, edit ~/.zshrc similarly.
  • Apply the changes:
    source ~/.bashrc
    

Step 4: Verify the Installation

  • Check the Go version:
    go version
    
  • You should see the version you installed, confirming success.

Method 2: Installing Go Using Package Manager

Some Linux distros offer Go packages in their repositories. This method is simpler but might not provide the latest version.

For Ubuntu/Debian

  • Update package lists:
    sudo apt update
    
  • Install Go:
    sudo apt install golang-go
    
  • Verify:
    go version
    

For Fedora

  • Install Go:
    sudo dnf install golang
    
  • Verify:
    go version
    

For CentOS

  • Enable EPEL repository if not enabled:
    sudo yum install epel-release
    
  • Install Go:
    sudo yum install golang
    
  • Verify:
    go version
    

Comparing Both Methods

FeatureOfficial Binary MethodPackage Manager Method
Latest VersionYesOften outdated
Easy to UpdateManualAutomatic with system updates
Installation SpeedFastFast
CustomizationFull controlLimited

If you want the latest Go features, the official binary method is best. For quick setup, package managers work well.

Configuring Your Go Workspace

Once Go is installed, setting up your workspace correctly helps you organize projects and dependencies.

Set GOPATH

  • GOPATH is where your Go projects and dependencies live.
  • By default, it’s set to $HOME/go.
  • You can confirm or set it in your shell profile:
    export GOPATH=$HOME/go
    export PATH=$PATH:$GOPATH/bin
    
  • Reload your shell:
    source ~/.bashrc
    

Create Your Workspace Directories

  • Inside $GOPATH, create directories:
    mkdir -p $GOPATH/src $GOPATH/bin $GOPATH/pkg
    
  • This structure keeps your source code, compiled binaries, and packages organized.

Verify Workspace Setup

  • Run:
    go env GOPATH
    
  • It should output your workspace path.

Testing Your Go Installation

To make sure everything works, write and run a simple Go program.

Create a Hello World Program

  • Create a file named hello.go:

    package main
    
    import "fmt"
    
    func main() {
        fmt.Println("Hello, Go on Linux!")
    }
    
  • Save it in your workspace, for example, $GOPATH/src/hello/hello.go.

Build and Run the Program

  • Navigate to the directory:
    cd $GOPATH/src/hello
    
  • Run the program:
    go run hello.go
    
  • You should see:
    Hello, Go on Linux!
    

Compile the Program

  • To compile into a binary:
    go build hello.go
    
  • Run the binary:
    ./hello
    
  • It will print the same message.

Updating Go on Linux

Keeping Go updated ensures you have the latest features and security fixes.

If Installed via Official Binary

  • Download the new version tarball.
  • Remove the old Go directory:
    sudo rm -rf /usr/local/go
    
  • Extract the new tarball:
    sudo tar -C /usr/local -xzf go1.xx.linux-amd64.tar.gz
    
  • Verify with go version.

If Installed via Package Manager

  • Update your system packages:
    sudo apt update && sudo apt upgrade golang-go
    
  • Or for Fedora/CentOS:
    sudo dnf update golang
    sudo yum update golang
    

Note that package managers might lag behind the official releases.

Troubleshooting Common Go Installation Issues

Sometimes, you might face issues during installation. Here are common problems and fixes:

  • Go command not found: Ensure /usr/local/go/bin is in your PATH.
  • Permission denied errors: Use sudo when extracting or installing.
  • Old Go version showing: Remove old versions completely before reinstalling.
  • GOPATH conflicts: Make sure GOPATH is set correctly and does not overlap with Go’s installation directory.
  • Proxy or network issues: Set Go proxy environment variables if you have network restrictions:
    export GOPROXY=https://proxy.golang.org,direct
    

Useful Go Commands After Installation

Once Go is installed, here are some commands to get you started:

  • go version – Check Go version.
  • go env – View Go environment variables.
  • go run <file.go> – Run a Go program.
  • go build <file.go> – Compile a Go program.
  • go get <package> – Download and install Go packages.
  • go fmt <file.go> – Format Go source code.

Using these commands regularly will help you work efficiently with Go.

Conclusion

Installing Go on Linux is a straightforward process that opens up many programming opportunities. Whether you choose the official binary method or your distro’s package manager, you’ll have Go ready in minutes. Setting up your workspace and environment variables properly ensures a smooth development experience.

Now that you have Go installed, you can start building fast, efficient applications on your Linux system. Keep your Go version updated and explore the rich ecosystem of Go libraries and tools. Happy coding!

FAQs

How do I check if Go is installed on my Linux system?

Run go version in the terminal. If Go is installed, it will display the version number. Otherwise, you’ll get a “command not found” error.

Can I install multiple Go versions on Linux?

Yes, but it requires managing different Go installations manually or using tools like gvm (Go Version Manager) to switch between versions.

Is it better to install Go via package manager or official binary?

The official binary method provides the latest version and more control. Package managers are easier but might have older versions.

How do I set the GOPATH environment variable?

Add export GOPATH=$HOME/go to your shell profile (~/.bashrc or ~/.zshrc) and reload the shell with source ~/.bashrc.

What should I do if go command is not found after installation?

Make sure /usr/local/go/bin is added to your PATH environment variable and reload your shell profile.

More from this blog

L

LinuxBloke | Linux Tips, Tricks & Troubleshooting

672 posts