RPM Package Manager, commonly known as RPM, is a versatile open-source package management tool that provides a simple way to build, install, verify, update, and uninstall software packages. This article guides you through the steps to build an RPM package on various Linux distributions: Ubuntu, CentOS, Fedora, and Red Hat Enterprise Linux (RHEL).
Before proceeding, it’s crucial to understand that building an RPM requires a special user environment, generally referred to as a build environment. This safeguards your system from potential damage if anything goes wrong during the package build process.
Table of Contents
Setting Up the Build Environment
Regardless of the Linux distribution you are using, you’ll need to create a build environment. We recommend creating a dedicated user for this purpose:
- Create a new user:
sudo useradd makerpm
- Switch to the new user:
su - makerpm
- Set up the build environment by creating a
rpmbuild
directory structure in the home directory:mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
- Create an
.rpmmacros
file in the home directory and include the following:
echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros
Now, you’re ready to install the required tools based on your Linux distribution.
Ubuntu
Ubuntu uses the DEB package format by default, so we need to install some additional tools:
- Update the package lists:
sudo apt-get update
- Install the required packages:
sudo apt-get install rpm
CentOS, Fedora, and RHEL
These distros natively support RPM. We just need to install the rpm-build
package:
- Update the system:
sudo yum update
(for CentOS/RHEL) orsudo dnf update
(for Fedora) - Install
rpm-build
:sudo yum install rpm-build
(for CentOS/RHEL) orsudo dnf install rpm-build
(for Fedora)
Creating an RPM Spec File
RPM uses a spec file to describe how to build the package. The spec file contains necessary information like the package’s name, version, release number, and instructions on how to set up, build, install, and clean up the package.
Create a spec file under the ~/rpmbuild/SPECS
directory. A sample spec file may look like this:
Name: helloworld
Version: 1.0
Release: 1%{?dist}
Summary: Hello World Program
License: GPL
URL: https://example.com
Source0: https://example.com/source/helloworld-1.0.tar.gz
BuildRequires: gcc
%description
A simple Hello World program in C.
%prep
%setup -q
%build
gcc -o helloworld helloworld.c
%install
rm -rf $RPM_BUILD_ROOT
install -d $RPM_BUILD_ROOT/usr/local/bin
install -m 755 helloworld $RPM_BUILD_ROOT/usr/local/bin
%clean
rm -rf $RPM_BUILD_ROOT
%files
%defattr(-,root,root,-)
/usr/local/bin/helloworld
%changelog
* Fri May 19 2023 makerpm
- Initial RPM release
In this spec file, we define various sections that guide the package building process, including setting up (%prep
), building (%build
), installing (%install
), cleaning up (%clean
), and finally specifying the files to include in the package (`%files`).
Building the RPM Package in Linux
With the spec file ready, you can now build your RPM package:
- Change to the SPECS directory:
cd ~/rpmbuild/SPECS
- Build the RPM package:
rpmbuild -ba helloworld.spec
Your RPM package will be available in the ~/rpmbuild/RPMS
directory.
Frequently Asked Questions (FAQ)
How to rebuild RPM from source in CentOS?
To rebuild an RPM from source in CentOS, first install the source RPM using yum install package-name.src.rpm
. Then, use the rpmbuild
command followed by the -ba
option and the spec file to rebuild the RPM. The spec file can typically be found in the ~/rpmbuild/SPECS/
directory. For example: rpmbuild -ba ~/rpmbuild/SPECS/package-name.spec
.
How to create RPM with Maven?
Creating an RPM with Maven involves using the RPM Maven Plugin. First, add the RPM Maven Plugin to your project’s POM file. Configure it by specifying the name, version, and architecture of the package, and mapping project resources to RPM paths. Finally, execute the ‘rpm:rpm’ goal from the command line like so: mvn rpm:rpm
. This will create an RPM file in the project’s target directory.