This tutorial will help you install Go(Golang) on a Fedora or a RHEL based system i.e. Centos/Rocky Linux/Alma Linux/RHEL. This guide can also work for other Linux systems like Debian and Ubuntu besides the Redhat specifics like the running the yum commands.
Go is a statically typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. It’s has always been said to be an easy language for developers to learn quickly.
Golang is very useful for writing light-weight microservices, infrastructure like networked servers and also tools and systems for developers. It can alsobe used for generating APIs that interact with front-end applications. If you want to build a small functional microservice quickly, then Golang is a great tool to use.
To follow along this guide, ensure that you have:
- An up to date Fedora or a Redhat based system i.e. Centos/Rocky Linux/Alma Linux/RHEL
- Connection to the internet
- Knowledge of Linux Terminal
Table of Content
- Updating the server
- Installing Go
- Setting up Go environment
- Verifying the installation and creating Hello World App with Go
Updating the server
Before proceeding, it is always a good practice to ensure that your server packages are up to date. Use this command to ensure that server packages are updated
sudo yum -y update
Golangis not available in the default Fedora or RHEL repositories. Instead, it is available as a downloadable package from the Golang website downloads page https://golang.org/dl/. We are going to download the Linux package.
The latest version of Golang as of the writting of this tutorial is
1.17.2. Download it uing this curl command:
curl -LO https://dl.google.com/go/go1.17.2.linux-amd64.tar.gz
Extract the archive you downloaded to your desired location in the system. I am extracting it to the
/usr/local directory, creating a Go tree in
sudo tar -C /usr/local -xzf go1.17.2.linux-amd64.tar.gz
In a system where you do not have access to the root system or if you want to install it for your user alone, you can extract it to a path in the home directory.
Setting up Go Environment
Now that Golang is downloaded to a local path, we need to set up the environment. Golang normally uses the two variables, GOROOT and GOPATH to define the root for golang packages and the location of the working directory. We need to add the golang binary directory to the executable directories as well using the PATH variable.
Add this content to the
~/.bashrc to achieve the above mentioned.
Set the GOROOT location where the Go packages are installed in the system
Next, let’s set the GOPATH, the location of our working directory. I normally prefer having Golang work in
~/go, so I will set it using this:
Finally set up the PATH by appending the go binary paths so we access go binary system wide
To apply the above chages you need to logout and login back to the shell or use the
source command to apply the changes:
Verifying the installation and creating Hello World App with Go
Now that you have successfully installed and configured go language on your system, let’s confirm that it is working fine by checking its version:
$ go version
go version go1.17.2 linux/amd64
Now also verify all configured environment variables using following command.
$ go env
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build970078185=/tmp/go-build -gno-record-gcc-switches"
Let us create a simple Hello world program to test our installation. Create a file main.go in the current directory and add these content to it.
fmt.Println("Citizix - Hello World!")
Then run the program with this command:
$ go run main.go
Citizix - Hello World!
Congratulations! In this guide, you have learned to install Golang on Fedora & CentOS/RHEL Linux systems.