Install Go Language on AlmaLinux 8 with 2 Easy Methods

Posted on

Install Go Language on AlmaLinux 8 with 2 Easy Methods

Install Go Language on AlmaLinux 8 with 2 Easy Methods

In this guide, we want to teach you how to Install Go Language on AlmaLinux 8. Go, also known as Golang, is an open-source, compiled, and statically typed programming language designed by Google. It is built to be simple, high-performing, readable, and efficient.

Go is one of the simplest programming languages out there. It is easy to pick up, especially if you already know any other programming language. You can now proceed to the following steps on the Orcacore website to finish the Golang programming language setup on ALmaLinux 8.

To complete the Golang programming language setup on ALmaLinux 8, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with AlmaLinux 8.

You can install Golang from both the Dnf package manager and from the source. Let’s see how to do it.

Method 1. Install Go Language on AlmaLinux 8 with DNF

Go language packages are available in the default AlmaLinux repository. You can easily use the DNF package manager to install it.

First, update your local package index with the command below:

sudo dnf update -y

Then, use the following command to install Golang on AlmaLinux 8:

sudo dnf install golang -y

This method is not in the latest version. You can proceed to the next step to install the latest version of Golang on your AlmaLinux server.

Method 2. Install Go Language on AlmaLinux 8 From Source

At this point, you can follow the steps below to install the latest version of Go language.

Download Go From Source

Here you can visit the Go Downloads page and use the wget command to download the Tar file given for Linux:

sudo wget https://go.dev/dl/go1.20.1.linux-amd64.tar.gz

Then, extract your downloaded file in the /usr/local directory by using the command below:

sudo tar -xzf go*.linux-amd64.tar.gz -C /usr/local

Note: If you don’t have the tar package installed on your server, you can use the command below to install it:

sudo dnf install tar -y

When your extraction is completed, you can delete your downloaded file to free your space:

sudo rm go*.linux-amd64.tar.gz

Configure System Environment Path

At this point, you need to configure the system environment for Go by adding its folder /usr/local/go/bin to your path.

Open the bashrc file by using your favorite text editor, here we use vi editor:

sudo vi ~/.bashrc

Add the following line at the end of the file:

export PATH=$PATH:/usr/local/go/bin

When you are done, save and close the file.

Next, reload your configuration file by using the following command:

source ~/.bashrc

Verify your Go installation on AlmaLinux 8 by checking its version:

go version
**Output**
go version go1.20.1 linux/amd64

To get options and the help of Go, you can run the command below:

go --help
Golang Programming Language

Test Go Language

At this point, you can create a test program with Go language to see if it is working correctly.

Create a sample hello world file by using your favorite text editor, here we use vi editor:

vi hello.go

Add the following content to the file:

package main 
import "fmt" 
func main () {
fmt.Printf( "hello worldn" )
}

When you are done, save and close the file.

Compile the program by using the following command:

go build hello.go

Then, run your executable file:

./hello
**Output**
hello world

That’s it, you are done.

Conclusion

At this point, you have learned to Install Go Language or Golang on AlmaLinux 8. And you have learned to create a test program to see if your Go language is working correctly.

Hope you enjoy it. You may also like these articles:

Install and Configure OpenNMS on AlmaLinux 8

Install FreeRADIUS and daloRADIUS on AlmaLinux 8

Nginx Web Server Setup AlmaLinux 8

Securing Nginx with Let’s Encrypt on AlmaLinux 8

Redis Configuration AlmaLinux 8

Config Bind on AlmaLinux 8

Alternative Methods for Installing Go on AlmaLinux 8

While the article provides two solid methods for installing Go, let’s explore alternative approaches that offer different benefits, such as automation and containerization.

Method 3: Using a Go Version Manager (goenv)

A Go version manager like goenv provides flexibility in managing multiple Go versions on a single system. This is incredibly useful for projects that require specific Go versions or for testing code compatibility across different versions.

Steps to Install and Use goenv:

  1. Install Prerequisites: goenv requires git for installation. Install it if you don’t have it already:

    sudo dnf install git -y
  2. Install goenv: Clone the goenv repository into your home directory.

    git clone https://github.com/syndbg/goenv.git ~/.goenv
  3. Configure Shell: Add the following lines to your ~/.bashrc file to set up goenv.

    export GOENV_ROOT="$HOME/.goenv"
    export PATH="$GOENV_ROOT/bin:$PATH"
    eval "$(goenv init -)"

    Save the file and reload your shell configuration:

    source ~/.bashrc
  4. Install a Go Version: List the available Go versions and install the one you need.

    goenv install -l # Lists available versions
    goenv install 1.20.1 # Or any other version you desire
  5. Set the Global Go Version: Choose the Go version you want to use globally.

    goenv global 1.20.1
  6. Verify Installation:

    go version

    The output should now reflect the version you installed via goenv.

goenv simplifies the process of switching between Go versions, making it a valuable tool for developers working on multiple projects with different Go version requirements. You’ve now successfully installed Go Language on AlmaLinux 8 using goenv.

Method 4: Using Docker

Docker allows you to run Go in a containerized environment, ensuring consistency across different systems. This eliminates dependency conflicts and simplifies deployment.

Steps to Install and Use Docker for Go:

  1. Install Docker: If you don’t have Docker installed, follow these steps:

    sudo dnf install -y dnf-plugins-core
    sudo dnf config-manager --add-repo https://download.docker.com/linux/almalinux/docker-ce.repo
    sudo dnf install docker-ce docker-ce-cli containerd.io -y
    sudo systemctl start docker
    sudo systemctl enable docker
  2. Create a Dockerfile: Create a file named Dockerfile in your project directory with the following content:

    FROM golang:1.20.1-alpine3.17
    
    WORKDIR /app
    
    COPY go.mod go.sum ./
    RUN go mod download
    
    COPY . .
    
    RUN go build -o main .
    
    CMD ["./main"]

    This Dockerfile uses an official Go image, sets the working directory, copies your source code, downloads dependencies, builds the executable, and specifies the command to run.

  3. Create go.mod and go.sum (if using modules): If your Go project uses modules, initialize them:

    go mod init your_module_name

    This will create go.mod and go.sum files.

  4. Build the Docker Image: In the same directory as your Dockerfile, run the following command to build the image:

    docker build -t your_app_name .
  5. Run the Docker Container: Run the built image in a container:

    docker run your_app_name

    This will execute your Go application within the isolated Docker environment.

Example Go Program (main.go):

package main

import "fmt"

func main() {
    fmt.Println("Hello, Dockerized Go!")
}

By using Docker, you can encapsulate your Go application and its dependencies, ensuring consistent behavior across different environments, simplifying deployment, and avoiding conflicts with other software on your system. You can now say you know another way to Install Go Language on AlmaLinux 8.

These alternative methods offer different approaches to managing your Go environment on AlmaLinux 8, catering to various needs and preferences. Using goenv allows for easy switching between Go versions, while Docker provides a consistent and isolated environment for running your Go applications.

Leave a Reply

Your email address will not be published. Required fields are marked *