How to Install Go Lang on Ubuntu 22.04 / Ubuntu 20.04

0

Go is an open-source programming language designed by Google’s engineers, Robert Griesemer, Rob Pike, and Ken Thompson. It is a statically typed, compiled programming language primarily intended to be modern.

Go’s major features are: memory safety without a garbage collector, deadlock freedom, concurrency without data races, and coordination avoidance.

Here, we will see how to install Go Lang on Ubuntu 22.04.

Install Go Lang on Ubuntu 22.04

You can install the Go programing language using the Ubuntu repository or the archive from the official site.

1. Install Go Using the Official Archive

First, visit the Go download page to download the latest version of the Go programing language via a web browser. Alternatively, you can download the Go language binary package via terminal using the following command.

wget https://go.dev/dl/go1.18.1.linux-amd64.tar.gz

Then, extract the archive to the /usr/local directory.

sudo tar -zxvf go1.18.1.linux-amd64.tar.gz -C /usr/local/

Next, set up the PATH environment variable to include Go’s bin /usr/local/go/bin directory. To do that, execute the below command based on the requirement.

System-wide Go installation:

echo "export PATH=/usr/local/go/bin:${PATH}" | sudo tee /etc/profile.d/go.sh

source /etc/profile.d/go.sh

Specific user profile:

echo "export PATH=/usr/local/go/bin:${PATH}" | sudo tee -a $HOME/.profile

source $HOME/.profile

2. Install Go Using Ubuntu Repository

Go package is available in the Ubuntu repository. So, use the apt command to install it.

sudo apt update

sudo apt install -y golang-go

Verify Go Installation

Run the below command to see the version of the Go language.

go version

Output:

go version go1.18.1 linux/amd64

Create First Go Project

First, create a project directory.

mkdir -p hello

cd hello

Then, enable dependency tracking for the code. In a real-world scenario, you will replace example/hello with the repository location where your source code is kept. For example, the module path might be github.com/myuser/mymodule.

go mod init example/hello

Use the text editor to create a file to write a simple program (hello.go) to test the Go installation.

nano hello.go

Place the following content in the hello.go file.

package main

import "fmt"

func main() {
fmt.Printf("Welcome To ITzGeek\n")
}

Now, run the code with the go command.

go run .

You would get the following greeting text.

Welcome To ITzGeek

Conclusion

That’s all. You have successfully installed Go Lang on Ubuntu 22.04. Additionally, you can learn more about Go by visiting the Go Lang documentation.

You might also like