Build an RPM in CentOS, RHEL and Ubuntu Linux

build rpm linux, build rpm ubuntu

Discover how to build RPM packages on Ubuntu, CentOS, Fedora, and Red Hat. Follow our step-by-step guide to mastering RPM creation easily.

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.

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:

  1. Create a new user: sudo useradd makerpm
  2. Switch to the new user: su - makerpm
  3. Set up the build environment by creating a rpmbuild directory structure in the home directory: mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
  4. 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 uses the DEB package format by default, so we need to install some additional tools:

  1. Update the package lists: sudo apt-get update
  2. 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:

  1. Update the system: sudo yum update (for CentOS/RHEL) or sudo dnf update (for Fedora)
  2. Install rpm-build: sudo yum install rpm-build (for CentOS/RHEL) or sudo dnf install rpm-build (for Fedora)
See also  How to compare 2 files using 'diff' in Linux

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


BuildRequires:  gcc

A simple Hello World program in C.

%setup -q

gcc -o helloworld helloworld.c

install -d $RPM_BUILD_ROOT/usr/local/bin
install -m 755 helloworld $RPM_BUILD_ROOT/usr/local/bin



* 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:

  1. Change to the SPECS directory: cd ~/rpmbuild/SPECS
  2. 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.

Leave a Comment