In this tutorial, we are going to explore how to install go on Ubuntu 22.04
Golang is an open-source programming language that is easy to learn and use. It is built-in concurrency and a robust standard library. It is reliable and builds fast, and efficient software that scales fast.
Its concurrency mechanisms make it easy to write programs that get the most out of multicore and networked machines, while its novel-type systems enable flexible and modular program constructions.
Go compiles quickly to machine code and has the convenience of garbage collection and the power of run-time reflection.
In this guide, we are going to learn how to install Golang 1.19.4 on Ubuntu 22.04.
Related Articles
Installing Go 1.19.4 on Ubuntu 22.04
1. Run system updates
To begin with, we need to run system updates in order to make our current repositories up to date. To do so we need to run the following command on our terminal.
sudo apt update && apt upgrade -y
When both upgrades and updates are complete, go ahead to download go from its download page.
2. Installing Go
To install go we need to download it by going to the download page. we are going to download the release. We are going to use the wget command to download.
$ wget https://go.dev/dl/go1.19.4.linux-amd64.tar.gz
when the download is complete, extract the archive downloaded to your desired location. I will be using /usr/local directory. First delete the directory if you have installed Golang before.
$ sudo rm -rf /usr/local/go && tar -C /usr/local -xzf go1.19.4.linux-amd64.tar.gz
After extraction is complete move ahead and set up the go environment.
3. Setting Go Environment
To set up go environment we need to define the root of Golang packages. We normally use GOROOT and GOPATH to define that environment. We need to set up the GOROOT location where the packages are installed.
export GOROOT=/usr/local/go
Next, we will need to set up the GOPATH. Let’s set up GOPATH in the $HOME/go directory.
export GOPATH=$HOME/go
Now we need to append the go binary PATH so that we can be able to access the program system-wide. Use the following command.
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
To apply the changes we have made above, run the following command:
source ~/.bashrc
Lastly, we can do the verification with the following;
$ go version
go version go1.19.4 linux/amd64
Let’s create a program to check all our settings. We will create a simple one.
Create a file main.go on the main directory.
touch main.go
On your favorite text editor do the following
sudo nano main.go
Insert the below code
$ package main
import "fmt"
func main(){
fmt.Println("This is go1.19.4 on Ubuntu 22.04")
}
To run the program use the following command;
$ go run main.go
This is go1.19.4 on Ubuntu 22.04
Conclusion
Now you know how to install Golang 1.19.4 in Ubuntu 22.04. You can consult the go documentation in case you have any problems.