How To Install Go(Golang) on Ubuntu 22.04
Go, commonly known as Golang, is a modern and highly efficient programming language developed by Google engineers.It has gained popularity for its clean syntax, readability, and strong support for concurrent programming. Featuring lightweight threads called Goroutines, Go simplifies concurrent application development, while its static typing, garbage collection, and cross-platform support contribute to code reliability and ease of use. With a robust standard library and fast compilation times, Go is well-suited for web development, system programming, and cloud services.
We will walk you through the steps necessary to install the Go on Ubuntu 22.04.
Steps to Install Go (Golang) on Ubuntu 22.04
First, check for any pending system upgrade
Let's update software packages first. To perform updates, run
the following command:
sudo apt update
Install Go:
Ubuntu 22.04
includes a version of Go
in its repositories. You can install
it using the following command:
sudo apt install golang
This will install the default version available in the Ubuntu 22.04 repositories.
Verify Installation:
After the installation, you can check the Go version
to ensure it was installed successfully:
go version
This should display the installed Go version
.
Output:
root@vps:~# go version
go version go1.18.1 linux/amd64
Setting Up Your Go Workspace
The directory structure and GOPATH setup are optional, and many Go developers prefer to use Go modules, introduced in recent Go versions, which do not require setting up a specific workspace.
Go
projects typically follow a specific directory structure. You can set up your Go workspace
by defining the GOPATH environment variable and adding the bin
directory to your PATH. Edit your shell
profile file (e.g., ~/.bashrc or ~/.zshrc),
sudo nano ~/.bashrc
and add the following lines:
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
Then, reload your profile:
source ~/.bashrc
Test a Simple Program:
Create a simple Go
program to test your installation.
For example, create a file named hello.go
nano hello.go
And add the following content to this file:
package main
import "fmt"
func main() {
fmt.Println("Hello, Golang!")
}
Then, run the program:
go run hello.go
This should print "Hello, Golang!" to the console.
Output:
root@vps:~# root@vps:~# go run hello.go
Hello, Golang!
root@vps:~#
That's it! You've successfully installed Go on Ubuntu 22.04.
CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME
for 10% off!
1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month
Available Locations: LAX | MIA | ATL | FRA | AMS