How To Install Go(Golang) on Ubuntu 23.10

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

Steps to Install Go (Golang) on Ubuntu 23.10

First, check for any pending system upgrade

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

apt update

Install Go:

Ubuntu 23.10 includes a version of Go in its repositories. You can install it using the following command:

apt install golang -y

This will install the default version available in the Ubuntu 23.10 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@server:~# go version
go version go1.21.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),

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