Go is a programming language developed by Google. It's an open-source, statically typed, compiled language which provides garbage collection, memory safety, and concurrency.
This tutorial explains how to install Go on Ubuntu 24.04.
Install Go
Download the latest version of Go from official website:
curl -o go.tar.gz "https://dl.google.com/go/$(curl -sS https://go.dev/VERSION?m=text | head -n1).linux-amd64.tar.gz"
Once the download is complete, extract the tar.gz
file to the /usr/local
directory:
sudo tar xf go.tar.gz -C /usr/local
The next step is to specify where Go binaries are located. Add the /usr/local/go/bin
directory to the system's PATH environment variable. It can be set in /etc/profile
file. In this case, Go will be available for all users as a system-wide command. To achieve this, run the following command:
echo 'export PATH=$PATH:/usr/local/go/bin' | sudo tee -a /etc/profile
To make changes to take effect, logout and login to the system. Or apply the changes immediately to the current session by using the following command:
source /etc/profile
Verify the installation by checking the Go version:
go version
The GOPATH
environment variable specifies the location of the workspace. By default, GOPATH
is set to $HOME/go
directory. Create that directory:
mkdir ~/go
The tar.gz
file is no longer needed, remove it:
rm -rf go.tar.gz
Testing Go
In the workspace, create a new project and navigate to it:
mkdir -p ~/go/src/hello && cd ~/go/src/hello
Create the main.go
file:
nano main.go
Add the following code:
package main
import "fmt"
func main() {
fmt.Printf("Hello world\n")
}
To test the program, execute the go run
command:
go run main.go
To build the program, use go build
command:
go build main.go
The command builds an executable file main
. Run it as follows:
./main
Uninstall Go
To completely remove Go, delete the installation directory:
sudo rm -rf /usr/local/go
Remove entry from /etc/profile
file:
sudo sed -i '/export PATH=\$PATH:\/usr\/local\/go\/bin/d' /etc/profile
We can also remove the workspace and cache directories:
rm -rf ~/go
rm -rf ~/.cache/go-build
Leave a Comment
Cancel reply