To install Go (Golang) on Linux, follow these steps:
go version
If Go is installed, you'll see the version number. If not, proceed with the installation.
Get the latest Go binary package from the official website:
wget https://go.dev/dl/go1.22.0.linux-amd64.tar.gz
Tip: Check the latest version at Go Downloads before running the command.
sudo rm -rf /usr/local/go
sudo tar -C /usr/local -xzf go1.22.0.linux-amd64.tar.gz
Add Go to your PATH by editing ~/.bashrc
or ~/.profile
:
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
echo 'export GOPATH=$HOME/go' >> ~/.bashrc
echo 'export PATH=$PATH:$GOPATH/bin' >> ~/.bashrc
source ~/.bashrc
For Zsh users:
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.zshrc
echo 'export GOPATH=$HOME/go' >> ~/.zshrc
echo 'export PATH=$PATH:$GOPATH/bin' >> ~/.zshrc
source ~/.zshrc
go version
You should see output like:
go version go1.22.0 linux/amd64
Create a test file:
mkdir -p ~/go/src/hello
cd ~/go/src/hello
nano hello.go
Paste this code:
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
Run the program:
go run hello.go
You should see:
Hello, Go!