Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Build your own RPM package with a sample Go program

May 21, 2021
Alejandro Sáez Morollón
Related topics:
Developer toolsGoLinuxOpen source
Related products:
Red Hat Ansible Automation PlatformRed Hat Enterprise Linux

    A deployment usually involves multiple steps that can be tricky. These days, we have a wide variety of tools to help us create reproducible deployments. In this article, I will show you how easy it is to build a basic RPM package.

    We have had package managers for a while. RPM and YUM simplify installing, updating, or removing a piece of software. However, many companies use package managers only to install software from the operating system vendor and don’t use them for deployments. Creating a package can be daunting at first, but usually, it’s a rewarding exercise that can simplify your pipeline. As a test case, I will show you how to package a simple program written in Go.

    Creating the package

    Many sites rely on configuration managers for deployment. For instance, a typical Ansible playbook might be:

     tasks:
        - name: 'Copy the artifact'
          copy:
            src: 'my_app'
            dest: '/usr/bin/my_app'
          
        - name: 'Copy configuration files'
          template:
            src: config.json
            dest: /etc/my_app/config.json

    Of course, a real-life playbook will include more steps, like checking the previous installation or handling services. But why not use something like this?

      tasks:
        - name: 'Install my_app'
          yum:
            name: 'my_app'

    Now, let’s see our Go application that serves up a webpage. Here's the  main.go file:

    package main
    
    import (
        "encoding/json"
        "flag"
        "fmt"
        "io/ioutil"
        "log"
        "net/http"
    )
    
    type config struct {
        Text string `json:"string"`
    }
    
    func main() {
    
        var filename = flag.String("config", "config.json", "")
        flag.Parse()
    
        data, err := ioutil.ReadFile(*filename)
        if err != nil {
            log.Fatalln(err)
        }
    
        var config config
        err = json.Unmarshal(data, &config)
    
        if err != nil {
            log.Fatalln(err)
        }
    
        http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
            fmt.Fprintf(w, config.Text)
        })
    
        log.Fatal(http.ListenAndServe(":8081", nil))
    
    }

    And our config.json:

    {
        "string": "Hello world :)"
    }

    If we run this program, we should see a web page with the config.json text in port 8081. It's far from production-ready, but it will serve as an example.

    Adding services

    What about a service? Adding services is an excellent way to unify the management of an application, so let’s create our my_app.service:

    [Unit]
    Description=My App
    
    [Service]
    Type=simple
    ExecStart=/usr/bin/my_app -config /etc/my_app/config.json
    
    [Install]
    WantedBy=multi-user.target

    Every time we want to deploy our application, we need to:

    1. Compile the project.
    2. Copy it to /usr/bin/my_app.
    3. Copy the config.json file to /etc/my_app/config.json.
    4. Copy my_app.service to /etc/systemd/system/.
    5. Start the service.

    Creating the spec file

    Like Ansible, an RPM package needs a definition file where we specify the installation steps, the dependencies, and other things that we might need to install our application on a server:

    $ sudo dnf install git
    $ sudo dnf module install go-toolset
    $ sudo dnf groupinstall "RPM Development Tools"

    With all of this installed, we are ready to create the package definition file, also known as the spec file:

    $ rpmdev-newspec my_app.spec

    A spec file can be tricky, but we will keep this simple to appreciate the power of the tool:

    Name:           my_app
    Version:        1.0
    Release:        1%{?dist}
    Summary:        A simple web app
    
    License:        GPLv3
    Source0:        %{name}-%{version}.tar.gz
    
    BuildRequires:  golang
    BuildRequires:  systemd-rpm-macros
    
    Provides:       %{name} = %{version}
    
    %description
    A simple web app
    
    %global debug_package %{nil}
    
    %prep
    %autosetup
    
    
    %build
    go build -v -o %{name}
    
    
    %install
    install -Dpm 0755 %{name} %{buildroot}%{_bindir}/%{name}
    install -Dpm 0755 config.json %{buildroot}%{_sysconfdir}/%{name}/config.json
    install -Dpm 644 %{name}.service %{buildroot}%{_unitdir}/%{name}.service
    
    %check
    # go test should be here... :)
    
    %post
    %systemd_post %{name}.service
    
    %preun
    %systemd_preun %{name}.service
    
    %files
    %dir %{_sysconfdir}/%{name}
    %{_bindir}/%{name}
    %{_unitdir}/%{name}.service
    %config(noreplace) %{_sysconfdir}/%{name}/config.json
    
    
    %changelog
    * Wed May 19 2021 John Doe - 1.0-1
    - First release%changelog
    

    A few notes:

    • The Source0 entry can be the source code repository, something like this: https://github.com/user/my_app/archive/v%version.tar.gz.
    • If you use a URL in Source0, you can issue spectool -g my_app.spec to download your source code.
    • Git allows you to quickly set up a tarball without creating a remote repository:
      $ git archive --format=tar.gz --prefix=my_app-1.0/ -o my_app-1.0.tar.gz HEAD
    • The tarball content should look like this:
       $tar tf my_app-1.0.tar.gz 
      my_app-1.0/
      my_app-1.0/config.json
      my_app-1.0/main.go
      my_app-1.0/my_app.service

    Building the RPM

    First, we need to create the rpmbuild structure and place our tarball inside the source's directory:

    $ rpmdev-setuptree
    $ mv my_app-1.0.tar.gz ~/rpmbuild/SOURCES

    Now, let’s build the RPM for Red Hat Enterprise Linux 8:

    $ rpmbuild -ba my_app.spec

    And that's it!

    You should be able to install the RPM now and start the service:

    $ sudo dnf install ~/rpmbuild/RPMS/x86_64/my_app-1.0-1.el8.x86_64.rpm
    $ sudo systemctl start my_app
    $ curl -L http://localhost:8081

    You should see the content of our config.json (which, by the way, is under /etc/my_app).

    But what if we have a new version of our application? We only need to increase the spec file version and build it again. DNF will see that there is a new update available.

    And if you are using a package repository, you only need to run dnf update my_app.

    Conclusion

    If you want to delve more into the idea of incorporating RPM files in your deployments, I suggest looking at the RPM Packaging Guide and the Fedora Packaging Guidelines.

    Also, a variety of exciting tools are available to help with the build process or even create repositories for you to use, such as mock, fedpkg, COPR, and Koji. These tools can help you in complex scenarios with multiple dependencies, complex steps, or multiple architectures.

    Note that the workflow demonstrated in this article is also applicable to Fedora and to CentOS Stream.

    Last updated: August 26, 2022

    Recent Posts

    • Debugging image mode with Red Hat OpenShift 4.20: A practical guide

    • EvalHub: Because "looks good to me" isn't a benchmark

    • SQL Server HA on RHEL: Meet Pacemaker HA Agent v2 (tech preview)

    • Deploy with confidence: Continuous integration and continuous delivery for agentic AI

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    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
    © 2026 Red Hat

    Red Hat legal and privacy links

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

    Chat Support

    Please log in with your Red Hat account to access chat support.