How To Install Go(Golang) on Rocky Linux 9

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 Rocky Linux 9.

Steps to Install Go (Golang) on Rocky Linux 9

First, check for any pending system upgrade

Let's update software packages first. To perform updates, run the following command:

dnf update

Install Go:

Rocky Linux 9 includes a version of Go in its repositories. You can install it using the following command:

dnf install golang -y

This will install the default version available in the Rocky Linux 9 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.


[root@vps ~]# go version
go version go1.20.10 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),

vi ~/.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

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


[root@vps ~]# go run hello.go
Hello, Golang!
[root@vps ~]#

That's it! You've successfully installed Go on Rocky Linux 9.