Golang, also known as Go, has emerged as a popular programming language for modern software development due to its simplicity and efficiency. This tutorial provides a comprehensive guide on installing Golang on CentOS/RHEL versions 9 and 8, two widely used Linux distributions renowned for their stability and robustness in enterprise environments.
Prerequisites
Before beginning the installation, ensure you have:
- A CentOS/RHEL 9 or 8 system.
- Root or sudo access to the system.
- Basic knowledge of Linux terminal commands.
Step 1: Update Your System
Start by updating your system packages to the latest versions. This ensures compatibility and security. Open a terminal and run:
sudo dnf update
Step 2: Downloading Golang
Go to the official Golang download page (https://go.dev/dl/) and find the latest version compatible with CentOS/RHEL. You can download it using the wget command. For instance:
wget https://go.dev/dl/go1.21.6.linux-amd64.tar.gz
Replace the link with the latest version available.
Step 3: Extracting the Golang Package
Once the download is complete, extract the Golang tarball to /usr/local
:
sudo tar -C /usr/local -xzf go1.21.6.linux-amd64.tar.gz
Again, ensure the version number in the command matches the downloaded fil
Step 4: Setting Go Environment Variables
For Go to function properly, you need to set the environment variables.. Commonly you need to set 3 environment variables as GOROOT, GOPATH and PATH. Add these lines to your ~/.bash_profile
or ~/.bashrc
file:
export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
After adding these lines, source your profile to apply the changes:
source ~/.bash_profile
or
source ~/.bashrc
Step 5: Verifying the Installation
To verify if Go has been installed correctly, check its version:
go version
This should display the installed version of Go. For example: go version go1.21.6 linux/amd64
Step 6: Creating Your First Go Project
- To start using Go, create a new directory for your example Go project:
mkdir -p $HOME/go/src/hello
- Switch to newly created directory
cd $HOME/go/src/hello
- Create a simple “Hello World” program to ensure everything is working. Create a file named hello.go with the following content:
package main import "fmt" func main() { fmt.Println("Hello, world!") }
- Run the program using:
go run hello.go
This should print “Hello, world!” on screen.
Conclusion
With these steps, you should have a working Go environment on your CentOS/RHEL system. Go is a powerful language for everything from web development to systems programming. Happy coding in Go!
If you encounter any issues during installation, consult the official Go FAQ or CentOS/RHEL support forums for assistance. Common issues usually involve environment variable settings or permissions.
12 Comments
Worked for me. Cool !! Thanks
Is there a good reason to not use the package manager for your system and install the packages provided by the vendor? e.g. RedHat/CentOS, after enabling the appropriate repos, “yum install golang”.
I’m not trying to stir up some debate that may have already been hashed out elsewhere, I’m honestly curious about any potential drawbacks.
Hi Tom,
There is no issue with the installation of packages from yum repos. But most of the time you did not get the latest or required version. In this way you can install any version you required.
Many thanks for the clear instructions
Thanks. Worked for me.
There is a typo in the link
# wget ttps://storage.googleapis.com/golang/go1.9.2.linux-amd64.tar.gz
Thanks Cheprus, I have updated tutorial.
yes, you have a typo when you export GOPATH (which you don’t actually do) instead you export GOROOT again which pooches the other steps. Also, you shouldn’t normally need $GOPATH/bin in your PATH. Only $GOROOT/bin.
Thanks, George. Updated tutorial.
Thanks for the tutorial!
Hi – I think you meant:
export GOROOT=/usr/local/go
should be
export GOPATH=/usr/local/go
doesn’t work for me, im sure i follow every step right
go command not found
Try changing “# export GOROOT=$HOME/Projects/Proj1” to “# export GOPATH=$HOME/Projects/Proj1”. I had the same problem running “go version” until I did that.