How to Create and Build Your Own RPM Packages

How to create and build RPM packages

Learn how to create and build RPM packages for Linux-based systems. This guide covers setting up your RPM build environment, writing spec files, and building and testing RPM packages step-by-step.

Table of Contents

🔈Introduction

In the world of Linux-based systems, the ability to create and manage software packages is a fundamental skill for developers and system administrators. One of the most commonly used packaging formats in the Linux ecosystem is RPM (Red Hat Package Manager). RPM is widely used by distributions such as Red Hat, CentOS, Fedora, and others. Creating your own RPM packages allows you to bundle software for easy distribution and installation across these systems.

In this post, we’ll take a deep dive into the process of creating and building your own RPM packages. From setting up your environment to writing the necessary build scripts, we’ll cover all the essential steps. By the end of this article, you’ll have the knowledge you need to create, package, and distribute software efficiently using RPM.


🤔What is an RPM Package?

An RPM package is a software distribution format used primarily in Linux distributions such as Red Hat, CentOS, Fedora, and SUSE. It allows developers to package software in a standardized format that can easily be installed, upgraded, or removed using package management tools like rpm and yum.

The RPM package consists of:

  • The application binaries (executable files, libraries)
  • Configuration files
  • Metadata (version, dependencies, installation scripts)

An RPM package typically has the .rpm file extension and can be installed, upgraded, and managed with the help of the rpm command line tool or other package managers like dnf and yum.


Why Create RPM Packages?

Creating RPM packages offers several advantages:

  • Standardization: RPM packages follow a standard format that is widely accepted and used in enterprise environments.
  • Simplified Distribution: Once created, the package can be easily distributed and installed on any compatible system.
  • Dependency Management: RPM allows you to define dependencies, ensuring that the software installs with the necessary prerequisites.
  • Automation: Package creation can be automated to streamline the release process.

Prerequisites for Creating RPM Packages

Before you can start building your RPM package, make sure your system is set up with the necessary tools. The following are the prerequisites for creating RPM packages:

  • A Linux-based operating system (Red Hat, Fedora, CentOS, etc.)
  • Root access (sudo privileges)
  • Basic knowledge of Linux shell commands and file system structure

Install the required tools:

				
					sudo dnf install rpm-build rpmdevtools
				
			
How to create and build RPM packages

Photo by admingeek from Infotechys


⚙️Setting Up Your RPM Build Environment

Once the necessary tools are installed, the next step is to set up the RPM build environment. This involves creating a directory structure where all your RPM packages and build files will reside.

Run the following command to set up the RPM build environment:

				
					rpmdev-setuptree
				
			

This will create the following directory structure in your home directory (~):

				
					~/rpmbuild/
├── BUILD
├── RPMS
│   └── x86_64/
├── SOURCES
├── SPECS
└── SRPMS
				
			
  • BUILD: Temporary directory where the RPM is built.
  • RPMS: Directory where the built .rpm files are stored.
  • SOURCES: Where source files (e.g., tarballs) are placed.
  • SPECS: Where spec files, which define how the RPM package should be built, are stored.
  • SRPMS: Holds the source RPM packages.

Understanding the RPM Build Process

The RPM build process revolves around Spec Files, which are files that contain instructions on how to build the RPM package. The spec file includes information like:

  • Name of the software

  • Version and release information

  • Description

  • License

  • Source location

  • Dependencies

  • Installation steps


📄Creating the Spec File

The spec file is the heart of the RPM package creation process. You can create a spec file using the rpmdev-newspec command. Here’s how you can create a basic spec file:

				
					cd ~/rpmbuild/SPECS
				
			
				
					rpmdev-newspec myapp
				
			

This will generate a file named myapp.spec. Open this file with a text editor and fill in the necessary details. Here’s an example of a simple spec file:

				
					Name:           myapp
Version:        1.0
Release:        1%{?dist}
Summary:        My Application

License:        GPL
URL:            https://www.myapp.com
Source0:        myapp-1.0.tar.gz

BuildRequires:  gcc, make
Requires:       libc

%description
MyApp is a simple application that demonstrates how to create an RPM package.

%prep
%setup -q

%build
make

%install
mkdir -p %{buildroot}/usr/local/bin
cp myapp %{buildroot}/usr/local/bin/

%files
/usr/local/bin/myapp

%changelog
* Thu Jun 23 2025 Developer <developer@example.com> 1.0-1
- Initial release
				
			

Let’s break this down:

  • Name, Version, Release: Identifies the software.
  • Summary: A brief description of the package.
  • Source0: Points to the source tarball, which must be placed in the SOURCES directory.
  • %prep, %build, %install: These are macros used to define the steps for preparing, building, and installing the software.
  • %files: Lists all the files that will be included in the package.
  • %changelog: Records changes made in each release.

Building the RPM Package

After setting up your spec file and placing the necessary source files in the SOURCES directory, you can start building the RPM package.

To build the RPM, run the following command:

				
					rpmbuild -ba ~/rpmbuild/SPECS/myapp.spec
				
			

This command will execute the %prep, %build, and %install sections defined in your spec file and generate the RPM package. The built RPM file will be stored in the RPMS/x86_64 directory.

To verify that the RPM package was created successfully, use the ls command:

				
					ls ~/rpmbuild/RPMS/x86_64/
				
			

Testing the RPM Package

Once you’ve created the RPM package, it’s important to test it by installing it on a target system. To install the RPM package, use the rpm command:

				
					sudo rpm -i ~/rpmbuild/RPMS/x86_64/myapp-1.0-1.x86_64.rpm
				
			

To uninstall the RPM package:

				
					sudo rpm -e myapp
				
			

You can also use dnf or yum to manage RPM packages more easily, especially for managing dependencies.


Installing and Uninstalling RPM Packages

RPM makes it easy to install and remove software. Here are a few common commands for managing RPM packages:

Install a Package:

				
					sudo rpm -i myapp-1.0-1.x86_64.rpm
				
			

Upgrade a package:

				
					sudo rpm -U myapp-1.1-1.x86_64.rpm
				
			

Remove a package:

				
					sudo rpm -e myapp
				
			

Query Installed Packages:

				
					rpm -q myapp
				
			

📌 Conclusion

Creating your own RPM packages can streamline the distribution and installation of software across Linux-based systems. By following the steps outlined in this article, you can create and build custom RPM packages with ease. Remember, the key components of the process are setting up the RPM build environment, writing the spec file, and using the rpmbuild command to compile the package.

With this knowledge, you can manage your software releases more efficiently, automate your build processes, and ensure that your software is packaged in a way that is easily installable and maintainable.

Did you find this article helpful? Your feedback is invaluable to us! Feel free to share this post with those who may benefit, and let us know your thoughts in the comments section below.


Related Posts
RHEL9 on KVM
Commands
Install RHEL9 on KVM

In this tutorial, we’ll guide you through the step-by-step installation of RHEL9 on KVM. The process is uncomplicated, and we’ll present it in a format

Read More »

Leave a Reply

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