How to install Go on CentOS 8

Go, often called golang, is a modern open source programming language created by Google that allows you to build robust and efficient applications.

Many popular applications such as Kubernetes, Docker, Prometheus, and Terraform are written in Go.

This article explains how to download and install Go on a CentOS 8 system.

At the time of this writing, the latest stable version of Go is 1.13.4. Before downloading the tar archive, visit the official Go downloads page and check if a new version is available.

Follow these steps to download and install Go on CentOS 8:

  1. Download the Go binary using either wget or curl:
    wget https://dl.google.com/go/go1.13.4.linux-amd64.tar.gz
    

  2. Once the archive is downloaded, check the checksum of the tar archive by typing:
    sha256sum go1.13.4.linux-amd64.tar.gz
    

    Make sure the hash printed from the sha256sum command matches the hash on the downloads page.

    692d17071736f74be04a72a06dab9cac1cd759377bd85316e52b2227604c004c  go1.13.4.linux-amd64.tar.gz

  3. Unpack the archive to the / usr / local directory using the tar command:
    sudo tar -C /usr/local -xf go1.13.4.linux-amd64.tar.gz
    

    The above command must be run as root user or user with sudo privileges.

  4. Tell the system where to find the Go binaries by changing the $ PATH environment variable. This can be done by adding the following line to / etc / profile (for a system-wide installation) or $ HOME / .bash_profile (for installing the current user): ~ / .bash_profile
    export PATH=$PATH:/usr/local/go/bin

    Save the file and load the new PATH environment variable into the current shell session using the source command:

    source ~/.bash_profile
    

That’s all. At this point, Go has been installed on your CentOS system.

To test that Go is installed correctly, we’ll set up a workspace and create a simple “Hello world” program.

  1. The location of the workspace directory is specified using the GOPATH environment variable. The default is $ HOME / go. Run the following command to create a directory:
    mkdir ~/go
    

  2. Inside the workspace, create a new src / hello directory:
    mkdir -p ~/go/src/hello
    

    In this directory, create a file called hello.go:

    nano ~/go/src/hello/hello.go
    

    Paste the following code into the file: ~ / go / src / hello / hello.go

    package main
    
    import "fmt"
    
    func main() {
        fmt.Printf("Hello, Worldn")
    }

  3. Change to ~ / go / src / hello directory and run go build to generate the code:
    cd ~/go/src/hello
    go build
    

    The command above will create an executable file named hello.

  4. Run the executable by typing:
    ./hello
    

    If you see the following output, then you have successfully installed Go.

    Hello, World

Now that you’ve downloaded and installed Go, you can start writing your Go code.

If you run into an issue or have feedback, please leave a comment below.

Sidebar