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.

Advertisement

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

  1. To start using Go, create a new directory for your example Go project:
    mkdir -p $HOME/go/src/hello 
    
  2. Switch to newly created directory
    cd $HOME/go/src/hello 
    
  3. 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!")
    }
    
    
  4. 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.

Share.

12 Comments

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

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

  3. Thanks for the tutorial!

    Hi – I think you meant:

    export GOROOT=/usr/local/go

    should be

    export GOPATH=/usr/local/go

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

Exit mobile version