Install and Use Golang on Ubuntu 22.04

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. In fact, 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

Steps To Install and Use Golang on Ubuntu 22.04

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide the Initial Server Setup with Ubuntu 22.04.

Install Go 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/

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

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"

Use Golang on Ubuntu 22.04

At this point, 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.

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

Hope you enjoy it.

You may be like these articles:

Install and Use Rust Programming Language on Ubuntu 22.04

How To Install Python 3.10 on Ubuntu 22.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!