Introduction
In the world of programming, efficiency and performance are key factors that can greatly impact the success of any project. One programming language that has gained immense popularity for its speed and efficiency is Go, also known as Golang. Developed by Google, Go is designed to provide developers with a reliable and robust platform for building efficient and scalable software. In this article, we will walk you through a step-by-step guide on how to install and configure Go on Ubuntu 22.04 to ensure optimal performance.
Table of Contents
Go is commonly used for various purposes, such as web development, networking applications, system programming, cloud services, and containerization (e.g., Docker). Its popularity in the software industry continues to grow due to its performance, simplicity, and strong concurrency support, making it an attractive choice for modern application development.
Prerequisites
- Ubuntu 22.04. Before you begin, you should have a non-root user account with sudo privileges set up on your system. You can learn how to do this by following the Initial Server Setup Guide with Ubuntu 22.04 tutorial.
- It would help if you used a fresh OS install to prevent potential issues.
Installing Go on Ubuntu 22.04
Go is a programming language developed by Google. It’s an open-source, statically typed, compiled language which provides garbage collection, memory safety, and concurrency.
There are several methods to install Go on Ubuntu 22.04 and the details of each method are given below:
This tutorial explains how to install Go on Ubuntu 22.04.
Method 1: Using Apt Command
The apt package management system in Ubuntu 22.04 includes the repository of Go, thus allowing the user to install the latest version of software directly from the apt command. You can use the following installation command to install Go on Ubuntu:
Step 1: System Update
Before we begin, it’s essential to ensure that your system packages are up to date. Open a terminal and run the following commands:
samm@go:~$ sudo apt-get update -y
Running the update command will inform you of the number of packages that need to be upgraded. However, you can choose to skip this step if there are no packages to upgrade.
samm@go:~$ sudo apt-get upgrade -y
Step 2: Install Go on Ubuntu 22.04
If you are curious enough, you can search for the availability of the golang-go package from Debian / Ubuntu repositories as shown.
samm@go:~$ apt search golang-go
Output :
Sorting... Done
Full Text Search... Done
gccgo-go/jammy 2:1.18~0ubuntu2 amd64
Go programming language -- gccgo
gogoprotobuf/jammy 1.3.2-1 amd64
alternative protocol buffer support for Golang - utilities
golang-1.17/jammy-updates 1.17.13-3ubuntu1 all
Go programming language compiler - metapackage
golang-1.17-go/jammy-updates 1.17.13-3ubuntu1 amd64
Go programming language compiler, linker, compiled stdlib
golang-1.18/jammy-updates,jammy-security 1.18.1-1ubuntu1.1 all
Go programming language compiler - metapackage
golang-1.18-go/jammy-updates,jammy-security 1.18.1-1ubuntu1.1 amd64
Go programming language compiler, linker, compiled stdlib
golang-1.20/jammy-updates 1.20.3-1ubuntu0.1~22.04 all
Go programming language compiler - metapackage
golang-1.20-go/jammy-updates 1.20.3-1ubuntu0.1~22.04 amd64
Go programming language compiler, linker, compiled stdlib
......
To install Golang Go using apt command, run
samm@go:~$ sudo apt install golang-go
Output :
samm@go:~$ sudo apt install golang-go
Reading package lists… Done
Building dependency tree… Done
Reading state information… Done
The following additional packages will be installed:
golang-1.18-go golang-1.18-src golang-src pkg-config
Suggested packages:
bzr | brz mercurial subversion
The following NEW packages will be installed:
golang-1.18-go golang-1.18-src golang-go golang-src pkg-config
0 upgraded, 5 newly installed, 0 to remove and 11 not upgraded.
Need to get 82.4 MB of archives.
After this operation, 437 MB of additional disk space will be used.
Do you want to continue? [Y/n]
Press “Y“ to continue installation.
Once the installation is complete, verify that Go is installed as shown.
samm@go:~$ go version
go version go1.18.1 linux/amd64
From the output, you can see that Go version 1.18.1 has been installed. Notably, this is not the latest version at the time of writing this guide.
Method 2: Install Go from Binary File
You can use this method to install the latest version of Go on Ubuntu 22.04. For that purpose, you will need to follow the steps given below:
Step 1: Download Go
Start by visiting the official download page Go to download the latest stable version of Go and download the 64-bit tarball installation file (amd64.tar.gz)
On the command line, you can download the latest tarball file using the wget command. At the time of writing this guide, the latest version of Go is Go 1.20.7. This is likely to change by the time you are reading this guide, so ensure to replace the version number accordingly.
samm@go:~$ curl -o go.tar.gz "https://dl.google.com/go/$(curl https://go.dev/VERSION?m=text).linux-amd64.tar.gz"
Once the download is complete, extract the tar.gz file to the /usr/local
directory:
samm@go:~$ sudo tar xf go.tar.gz -C /usr/local
Step 2: Add the Golang binary to the $PATH
The next step is to specify where Go binaries are located. Add the /usr/local/go/bin
directory to the system’s PATH environment variable. It can be set in /etc/profile
file. In this case, Go will be available for all users as a system-wide command. To achieve this, run the following command:
samm@go:~$ vi ~/.bashrc
Paste the following line.
export PATH=$PATH:/usr/local/go/bin
Save the changes and exit the file.
Next, to make changes to take effect, reload the .bashrc or .bash_profile file using the source command as shown.
samm@go:~$ source ~/.bashrc
To ensure the installation was successful, we will check the version of “Go.”
To do this, execute the “go” command together with the “version” option on the terminal as shown below:
samm@go:~$ go version
go version go1.20.7 linux/amd6
The following output shows that go has successfully been installed.
How to use Go in Ubuntu 22.04
After completing the Go installation, proceed to execute a simple code written in the Go language. This test will determine whether the environment is functioning correctly on Ubuntu.
We will create a separate directory for our project as follows.
samm@go:~$ mkdir go-project
Now go to the directory and run the example module using the following commands:
samm@go:~$ cd go-project/
We will create a simple program called hai.go that prints out a simple message on the terminal.
samm@go:~/go-project$ vi hai.go
Copy and paste the following lines of code into the file.
package main
import "fmt"
func main() {
fmt.Printf("Congratulations samm!! Go has successfully been installed on your system\n")
}
Save the changes and exit. Then run the program as follows
samm@go:~/go-project$ go run hai.go
You will get the following output. This confirms that the installation of Golang Go is working as expected.
Congratulations samm!! Go has successfully been installed on your system
That’s it, now you can execute other codes using the same method on Ubuntu 22.04. For further guidance, you can read the Go Documentation.
Conclusion
Congratulations! You have successfully installed and configured Go on your Ubuntu 22.04 system. Go’s performance-oriented design and simplicity make it a powerful tool for developing efficient applications. By following the steps outlined in this guide, you’ve set up a solid foundation for building high-performance software using the Go programming language. Start exploring Go’s extensive libraries and tools to unlock its full potential in your projects. Happy coding!
Also Read Our Other Guides :
- How To Install and Configure Elasticsearch on Ubuntu Server 22.04
- How To Install and Configure NFS Server Client on Ubuntu 22.
- How To Install Jenkins on Ubuntu 22.04
- How To Install a MinIO Object Storage Server on Rocky Linux
Finally, now you have learned how to install and configure Go (Golang) on Ubuntu 22.04.