Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

How to package Go applications in RHEL 10

December 10, 2024
Alejandro Sáez Morollón
Related topics:
ContainersDeveloper ProductivityDeveloper ToolsDevOpsGoLinux
Related products:
Red Hat Enterprise Linux

Share:

    Until now, packaging Go applications for Red Hat Enterprise Linux (RHEL) was very different from packaging them for Fedora. Fedora has plenty of tools that make the process as easy and quick as possible. And now, with the release of RHEL 10 beta, the packaging differences between Fedora, CentOS Stream and RHEL are smaller than ever before. We can also work from a Fedora machine without complex setups.

    Let's explore how similar the process is now and how you can package a sample Go application faster than ever in RHEL 10.

    Packaging Go applications in Red Hat Enterprise Linux 10

    For the sake of this article, I made a sample application that basically prints a little of information about the system. You can find the code in this repository. But the code is as simple as it gets:

    package main
    import (
        "fmt"
        "os"
        "runtime"
    )
    func getRelease() error {
        content, err := os.ReadFile("/etc/redhat-release")
        if err != nil {
            if os.IsNotExist(err) {
                return fmt.Errorf("file not found")
            }
            return fmt.Errorf("failed to read file: %w", err)
        }
        fmt.Printf("OS: %s", string(content))
        return nil
    }
    func main() {
        getRelease()
        fmt.Printf("Arch: %s\n", runtime.GOARCH)
        fmt.Printf("Go version: %s\n", runtime.Version())
    }

    The idea behind the workflow for packaging a Go project has not changed, and you can see how I discussed this a few years ago. However, the process became cumbersome as the applications became more complex. The vast majority of the steps can now be leveraged to more modern, up-to-date tools. The majority of RPM packagers operate on Fedora desktops, and so do we. If you don't have a Fedora setup at hand or don't want to mess with your setup directly, we'll use a container.

    $ podman pull registry.fedoraproject.org/fedora-toolbox:41
    Trying to pull registry.fedoraproject.org/fedora-toolbox:41...
    Getting image source signatures
    Copying blob cabdb1918cf1 skipped: already exists  
    Copying config 8a11b6229e done   | 
    Writing manifest to image destination
    8a11b6229efe85b59df4b7ae2fd7c27dc1ac9b553ddcdecf932e0256c7c2e710
    $ podman run -it registry.fedoraproject.org/fedora-toolbox:41 /bin/bash
    [root@18af08a1e3ce /]# 

    If you have Toolbx installed, the process couldn't be easier:

    $ toolbox create 
    Image required to create Toolbx container.
    Download registry.fedoraproject.org/fedora-toolbox:41 (375.4MB)? [y/N]: y
    Created container: fedora-toolbox-41
    Enter with: toolbox enter
    $ toolbox enter
    [fedora-toolbox-41]$

    Regardless of your choice, you should now have a Fedora 41 environment, where we can install all the necessary components.

    [fedora-toolbox-41]$ sudo dnf install golang go-rpm-macros go2rpm fedpkg centpkg "@rpm-development-tools" -y

    Before, we had to create the specfile file manually and adjust several steps to meet the needs of the application we were packaging. However, we can now use go2rpm to generate the specfile file automatically. Usually, there aren't many changes required.

    If you look at the Go sample repository, you'll see I created a release. This is intentional to represent a real upstream project and to make life easier for go2rpm, but you can always specify a commit or a tag in case you want a specific release:

    [fedora-toolbox-41]$ go2rpm github.com/alexsaezm/rhd_blog2024
    # OR
    [fedora-toolbox-41]$ go2rpm github.com/alexsaezm/rhd_blog2024 --commit 3da750

    Both commands will create a specfile in the current path. After this, we can use spectool and centpkg to build our package. This tool requires us to work with a Git repository. If you aren't already working on a Git repository, create one before you tackle any of the following steps:

    [fedora-toolbox-41]$ go2rpm github.com/alexsaezm/rhd_blog2024
    [fedora-toolbox-41]$ spectool -g golang-github-alexsaezm-rhd-blog2024.spec
    [fedora-toolbox-41]$ centpkg --release c10s mockbuild

    This will create a folder that starts with results_, inside which you will find the resulting RPM packages. These packages can now be installed on your CentOS Stream 10 or RHEL 10 machine.

    Furthermore, we can build it for Fedora without any modifications:

    [fedora-toolbox-41]$ fedpkg --release rawhide mockbuild

    This also simplifies the process of creating an RPM during a CI pipeline. For example, we could add the previous steps to a Containerfile to complete the process with a small tweak. Instead of using mockbuild which uses a container to perform the build, we can now use local:

    FROM registry.fedoraproject.org/fedora-minimal:latest
    RUN dnf install -y \
       golang \
       go-rpm-macros \
       centpkg \
       "@rpm-development-tools" && \
       dnf clean all
    WORKDIR /build
    COPY . .
    RUN centpkg --release c10s local

    Final thoughts

    This is, of course, a demo. The next step in a normal packaging workflow would be to put the RPM on a repository so that all machines with it configured would have the update.

    Personally, I am very excited about the packaging for RHEL 10 and Go. Some people used to complain that it was challenging to make a Go package work in Fedora and RHEL at the same time. Now that gap is even smaller than ever before.

    Related Posts

    • Build your own RPM package with a sample Go program

    • Using Delve to debug Go programs on Red Hat Enterprise Linux

    • Register allocation in the Go compiler

    • Implementing OpenSSL-backed Go cryptographic algorithms

    • How to add debug support for Go stripped binaries

    • Python performance optimizations with RHEL 9.5

    Recent Posts

    • How to encrypt RHEL images for Azure confidential VMs

    • How to manage RHEL virtual machines with Podman Desktop

    • Speech-to-text with Whisper and Red Hat AI Inference Server

    • How to use Splunk as an event source for Event-Driven Ansible

    • Integrate vLLM inference on macOS/iOS with Llama Stack APIs

    What’s up next?

    This cheat sheet covers the basics of installing .NET on Red Hat Enterprise Linux (RHEL), how to get a simple program running, and how to run a program in a Linux container.

    Get the cheat sheet
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue