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:

  1. Distributed Network Services
  2. Cloud-Native Development 
  3. Replacements for Existing Infrastructure
  4. Utilities and Stand-Alone Tools
  5. News Outlets
  6. Media Platform

You can now proceed to the following steps on the Orcacore website to Install and Use Golang on Ubuntu 22.04.

Steps 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.

Steps To Install and Use Golang on 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:

Output
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19.1"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2096209945=/tmp/go-build -gno-record-gcc-switches"

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 orcacore\n")
}

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:

Install and Use NMAP on Linux

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.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!