How to Fix 'go: command not found'
In this tutorial, you'll learn about How to Fix 'go: command not found. We cover key concepts, practical examples, and best practices.
The Problem
You installed Go but when you type go version you get:
go: command not found
The shell cannot find the go binary in any directory listed in your PATH.
Quick Fix
1. Verify Go is actually installed
Check whether the binary exists on disk:
# Common install locations
ls /usr/local/go/bin/go
ls /usr/lib/go/bin/go
ls ~/go/bin/go
ls ~/sdk/go*/bin/go
If none of these files exist, Go is not installed. Download it from the official site:
wget https://go.dev/dl/go1.22.3.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.22.3.linux-amd64.tar.gz
2. Add Go to PATH
If Go is installed but the shell cannot find it, add the binary directory to PATH:
# For standard installation
export PATH=$PATH:/usr/local/go/bin
# For a user installation
export PATH=$PATH:$HOME/go/bin
Make the change permanent by adding it to your shell profile:
# For bash
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
source ~/.bashrc
# For zsh
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.zshrc
source ~/.zshrc
3. Understand GOROOT vs GOPATH
GOROOT is the directory where Go is installed. GOPATH is your workspace directory for projects and downloaded modules.
# Set GOROOT if Go is in a non-standard location
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
# Set GOPATH (defaults to $HOME/go)
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
In most cases you do not need to set GOROOT — the go binary finds itself. Setting GOPATH is needed if you want binaries from go install to be accessible.
4. Check for multiple Go installations
If you installed Go via a package manager and also manually, they may conflict:
# Find all Go binaries
which -a go 2>/dev/null
sudo find / -name "go" -type f 2>/dev/null | grep -E "/go$"
# Remove the unwanted version
sudo apt remove golang-go # if installed via apt
Keep only one installation and ensure the correct one is first in your PATH.
5. Restart the shell or terminal session
Environment variables set in a shell profile apply only to new sessions. If you edited ~/.bashrc or ~/.zshrc, apply the changes:
source ~/.bashrc
# or
source ~/.zshrc
Or simply open a new terminal window.
6. Verify with go version
After fixing the PATH, confirm everything works:
go version
# Expected output: go version go1.22.3 linux/amd64
go env GOROOT
go env GOPATH
If go version prints correctly, run a quick test:
mkdir -p ~/hello && cd ~/hello
cat > main.go << 'EOF'
package main
import "fmt"
func main() {
fmt.Println("Go is working")
}
EOF
go run main.go
# Output: Go is working
7. Check for shell alias conflicts
Rarely, a shell alias shadows the go command:
alias go
# If an alias exists, remove it
unalias go
Check for aliases in ~/.bashrc, ~/.zshrc, or ~/.aliases.
Prevention
- Always add
export PATH=$PATH:/usr/local/go/binto your shell profile immediately after installing Go. - Use a version manager like
gvmorgoenvif you switch between Go versions frequently. - Run
hash -rafter updating PATH to clear the shell's command cache. - Document the exact install steps in a setup script or README for team consistency.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro