Install and Use Golang on Ubuntu 22.04 | Easy Setup
In this tutorial, we want to teach you How To Install and Use Golang on Ubuntu 22.04. Go, often called GoLang, was designed to respond to heated critiques against other languages used at Google. Go’s designers have a noted animosity towards C++ with a long list of simplifications developed through Go.
Many Go projects demonstrate that Golang is commonly used for the following applications:
You can now proceed to the following steps on the Orcacore website to Install and Use Golang on Ubuntu 22.04.
To Install and Use Golang on Ubuntu 22.04, you must log in to your server as a non-root user with sudo privileges. To do this, follow our guide the Initial Server Setup with Ubuntu 22.04.

Step 1 – Install Go or Golang on Ubuntu 22.04
First, you need to update your local package index with the command below:
sudo apt update
Then, you need to visit the Go Downloads page to check the latest version of Golang.
Now use the following command to download Golang on your Ubuntu 22.04:
wget https://go.dev/dl/go1.19.1.linux-amd64.tar.gz
Extract your Golang downloaded file to the /usr/local
directory with the command below:
sudo tar -zxvf go1.19.1.linux-amd64.tar.gz -C /usr/local/
Set the Go path environment variable
At this point, you need to set the path environment variable to include Go’s bin directory. To do this, run the following command:
echo "export PATH=/usr/local/go/bin:${PATH}" | sudo tee /etc/profile.d/go.sh
Next, you need to source the file with the command below:
source /etc/profile.d/go.sh
Verify your Golang installation on Ubuntu 22.04 by checking its version:
go version
In your output you will see:
**Output**
go version go1.19.1 linux/amd64
Check Go environment variables
Also, you can check the Golang environment variables on Ubuntu 22.04:
go env
In your output you will see:

Step 2 – Use Go or Golang on Ubuntu 22.04
In this step of Install and Use Golang on Ubuntu 22.04, we will show you how to use Golang by creating your first project.
First, create a hello directory for your project with the command below:
sudo mkdir -p hello
Switch to your project directory:
cd hello
Then, create a simple program to test your Golang installation on Ubuntu 22.04. Create the file with your favorite text editor, here we use vi:
sudo vi hello.go
Add the following content to your file:
package main
import "fmt"
func main() {
fmt.Printf("Welcome To orcacoren")
}
When you are done, save and close the file.
Build Go Mod File
Now you need to build go.mod
files so that you can execute the Golang file that you have created on Ubuntu 22.04:
sudo vi go.mod
Add the following line to the file:
module example.com/mod
When you are done, save and close the file.
Build and Execute the Go Program
Build the program with the command below:
go build
Then, execute the program with the following command:
./mod
In your output you will see:
**Output**
Welcome To orcacore
Conclusion
At this point, you have learned to Install and Use Golang on Ubuntu 22.04. As you saw, on Ubuntu 22.04, Golang can be easily installed. It is often used in combination with Docker, Kubernetes, and other modern software stacks for cloud, networking, and web applications.
Hope you enjoy it. You may also like these articles:
Configure Linux Users’ Passwords with chpasswd Command
Install and Use Rust Programming Language on Ubuntu 22.04
Install Python 3.10 on Ubuntu 22.04
FAQs
How do I set up the Go environment (GOPATH and GOROOT) on Ubuntu 22.04?
You can set the path environment variable to include Go’s bin directory with the following command:
“`
echo “export PATH=/usr/local/go/bin:${PATH}” | sudo tee /etc/profile.d/go.sh
“`
How can I verify that Go is installed correctly on Ubuntu 22.04?
You can verify it by checking its version:
“`
go version
“`
How do I uninstall Golang from Ubuntu 22.04?
You must Delete the go directory. It is usually located under `/usr/local/go` directory.
Then, remove the Go bin directory from your PATH environment variable. We mentioned about Path variables in the guide steps above on **Install and Use Golang on Ubuntu 22.04**.
Alternative Installation Methods for Golang on Ubuntu 22.04
While the above method works perfectly well for installing Go on Ubuntu 22.04, let’s explore two alternative approaches that offer different advantages: using apt
with the official Go repository, and utilizing snap
.
Method 1: Installing Go using apt
and the official Go repository.
This method leverages Ubuntu’s package manager, apt
, to install Go. This often simplifies updates and management, as Go will be integrated into your system’s package update process. However, it requires adding the official Go repository to your system.
Steps:
-
Add the Go repository: This step adds the official Go repository maintained by the Go team. This ensures you’re getting the latest stable version directly from the source.
sudo add-apt-repository ppa:longsleep/golang-backports
-
Update the package index: This updates your system’s list of available packages, including the newly added Go repository.
sudo apt update
-
Install Go: This command installs Go from the repository.
sudo apt install golang-go
-
Verify Installation: Check the Go version to confirm the installation.
go version
Explanation:
This method uses apt
, Ubuntu’s package manager, which simplifies the installation and updates of Go. The ppa:longsleep/golang-backports
PPA provides more recent versions of Go than the default Ubuntu repositories. It handles dependencies automatically, making the process smoother than manually downloading and extracting the tarball. This is a user-friendly option for those comfortable with apt
.
Method 2: Installing Go using Snap
Snap is a package management system developed by Canonical (the creators of Ubuntu). It packages an application with all its dependencies into a single package, making it easy to install and run applications on various Linux distributions.
Steps:
-
Install Snapd (if not already installed): Ubuntu usually comes with Snapd pre-installed. If not, use the following command:
sudo apt update sudo apt install snapd
-
Install Go using Snap: This command installs Go from the Snap store.
sudo snap install go --classic
-
Verify Installation: Check the Go version to confirm the installation. Note that you might need to explicitly specify the snap path.
/snap/bin/go version
Explanation:
Snaps are containerized software packages, which means they include all their dependencies. This isolates Go from the rest of your system and ensures consistency across different environments. The --classic
flag grants the snap broader access to system resources, which Go often requires. This method is particularly useful for quickly installing Go and avoiding potential conflicts with other system packages. Keep in mind that Snap packages might be slightly larger due to the included dependencies.
Code Example (Applicable to all Installation Methods)
Regardless of the installation method you choose, the following Go code example demonstrates a basic program:
package main
import "fmt"
func main() {
fmt.Println("Hello, Go from Ubuntu 22.04!")
}
Save this code as hello.go
. To run it, first build it using go build hello.go
and then execute the resulting binary: ./hello
.
These alternative methods offer flexibility in choosing how you Install and Use Golang on Ubuntu 22.04, depending on your preferences and system configuration. Each method has its advantages and disadvantages, allowing you to select the one that best suits your needs.