How to Build an RPM Package in Rocky Linux 8 (Step-by-Step Guide)

Rocky Linux 8 / RHEL-based environment, building custom RPM packages is an essential DevOps skill.

In this guide, we will:

  • Install required RPM build tools

  • Install and use the tree command

  • Create a sample shell script

  • Write a SPEC file

  • Build and test the RPM package


📦 Step 1: Install Required Packages

First, install RPM build utilities:

sudo dnf install -y rpm-build rpmdevtools

Now install the tree command (useful for viewing directory structure):

sudo dnf install -y tree

Verify installation:

rpmbuild --version
tree --version

🏗️ Step 2: Create RPM Build Environment

Run:

rpmdev-setuptree

This creates the standard RPM build directory structure inside your home directory.

Check it using tree:

tree ~/rpmbuild

Output:

~/rpmbuild
├── BUILD
├── RPMS
├── SOURCES
├── SPECS
└── SRPMS

Directory Explanation

DirectoryPurpose
BUILDTemporary build files
RPMSFinal binary RPM files
SOURCESSource files (scripts/tarballs)
SPECSSPEC file location
SRPMSSource RPM packages

📝 Step 3: Create Sample Application

Let’s create a simple shell script.

Go to SOURCES directory:

cd ~/rpmbuild/SOURCES

Create script:

vi hello.sh

Add the following content:

#!/bin/bash
echo "Hello from Rocky Linux RPM Package!"

Make it executable:

chmod +x hello.sh

📄 Step 4: Create SPEC File

Move to the SPECS directory:

cd ~/rpmbuild/SPECS

Create SPEC file:

vi hello.spec

Add the following content:

Name:           hello
Version:        1.0
Release:        1%{?dist}
Summary:        Simple Hello Script RPM

License:        GPL
Source0:        hello.sh

BuildArch:      noarch

%description
This is a simple RPM package that prints Hello message.

%prep

%build

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

%files
/usr/local/bin/hello

%changelog
* Mon Feb 23 2026 Vallabh Darole <vallabh.darole@darole.org> - 1.0-1
- Initial RPM package

⚙️ Step 5: Build the RPM

Run the following command from the SPECS directory:

rpmbuild -ba hello.spec

Build Options Explained

OptionMeaning
-baBuild binary + source RPM
-bbBuild binary RPM only
-bsBuild source RPM only

📁 Step 6: Locate the Built RPM

Binary RPM will be located at:

~/rpmbuild/RPMS/noarch/

Check using:

tree ~/rpmbuild/RPMS

Example file:

hello-1.0-1.el8.noarch.rpm

Source RPM:

~/rpmbuild/SRPMS/

🚀 Step 7: Install and Test the RPM

Install the package:

sudo rpm -ivh ~/rpmbuild/RPMS/noarch/hello-1.0-1.el8.noarch.rpm

Run the command:

hello

Output:

Hello from Rocky Linux RPM Package!

🔍 Useful RPM Commands for Administrators

CommandPurpose
rpm -qaList installed packages
rpm -ql packagenameList files inside package
rpm -qpR package.rpmShow dependencies
dnf builddep file.specInstall build dependencies


No comments:

Post a Comment