rpmbuild Command in Linux: Syntax, Stages & RPM Package Examples

rpmbuild drives the RPM packaging pipeline from a .spec file — %prep, %build, %install, and binary or source RPM output. On Ubuntu, install the rpm package for rpmbuild; native Debian packages use dpkg-buildpackage instead.

Published

Updated

Read time 9 min read

Reviewed byDeepak Prasad

rpmbuild Command in Linux: Syntax, Stages & RPM Package Examples
About rpmbuild drives the RPM packaging pipeline from a .spec file — %prep, %build, %install, and binary or source RPM output. On Ubuntu, install the rpm package for rpmbuild; native Debian packages use dpkg-buildpackage instead.
Tested on Ubuntu 25.04 (Plucky Puffin); RPM 4.20.1; kernel 7.0.0-27-generic
Package rpm (apt/deb) · rpm-build (dnf/rpm)
Man page rpmbuild(8)
Privilege user (installing built RPMs needs root)
Distros

RPM-based distros (RHEL, Fedora, AlmaLinux, openSUSE) — primary home of rpmbuild.

Debian/Ubuntu native packaging: dpkg-buildpackage (install rpm on Ubuntu only when you need RPM format).

Install/query RPMs: rpm.

rpmbuild — quick reference

Build stages (from a .spec file)

When to use Command
Run through %prep only (unpack sources, patches) rpmbuild -bp SPECS/pkg.spec
Through %prep and %conf rpmbuild -bf SPECS/pkg.spec
Through %build (compile) rpmbuild -bc SPECS/pkg.spec
Check build dependencies rpmbuild -bd SPECS/pkg.spec
Through %install (install into buildroot) rpmbuild -bi SPECS/pkg.spec
Verify %files section lists installed paths rpmbuild -bl SPECS/pkg.spec
Build binary RPM only rpmbuild -bb SPECS/pkg.spec
Build source RPM only rpmbuild -bs SPECS/pkg.spec
Build source RPM with dynamic requires rpmbuild -br SPECS/pkg.spec
Build binary and source RPMs rpmbuild -ba SPECS/pkg.spec

Build from tarballs or source RPMs

When to use Command
%prep from a tarball rpmbuild -tp SOURCES/pkg.tar.gz
Through %build from tarball rpmbuild -tc SOURCES/pkg.tar.gz
Through %install from tarball rpmbuild -ti SOURCES/pkg.tar.gz
Binary and source from tarball rpmbuild -ta SOURCES/pkg.tar.gz
Binary RPM only from tarball rpmbuild -tb SOURCES/pkg.tar.gz
Source RPM only from tarball rpmbuild -ts SOURCES/pkg.tar.gz
Rebuild binary from .src.rpm rpmbuild --rebuild SRPMS/pkg.src.rpm
Recompile through %install from .src.rpm rpmbuild --recompile SRPMS/pkg.src.rpm
%prep from source package rpmbuild -rp SRPMS/pkg.src.rpm
Full build from source package rpmbuild -ra SRPMS/pkg.src.rpm

Tree layout, macros, and cleanup

When to use Command
Use a custom topdir (not ~/rpmbuild) rpmbuild --define "_topdir /tmp/rpmbuild" -ba SPECS/pkg.spec
Define a macro for %configure rpmbuild --define "with_foo 1" -ba SPECS/pkg.spec
Enable configure option rpmbuild --with=feature -ba SPECS/pkg.spec
Disable configure option rpmbuild --without=feature -ba SPECS/pkg.spec
Remove build tree after success rpmbuild --clean -ba SPECS/pkg.spec
Skip %clean stage rpmbuild --noclean -ba SPECS/pkg.spec
Skip %prep stage rpmbuild --noprep -bc SPECS/pkg.spec
Skip %check tests rpmbuild --nocheck -ba SPECS/pkg.spec
Remove sources after build rpmbuild --rmsource -ba SPECS/pkg.spec
Remove spec file after build rpmbuild --rmspec -ba SPECS/pkg.spec
Skip dependency checks (debug only) rpmbuild --nodeps -ba SPECS/pkg.spec
Skip straight to %install or %build rpmbuild --short-circuit -bi SPECS/pkg.spec
Override target platform rpmbuild --target=x86_64-redhat-linux -ba SPECS/pkg.spec
Build in current directory (VCS checkout) rpmbuild --build-in-place -bb SPECS/pkg.spec
Omit debuginfo subpackages rpmbuild --nodebuginfo -ba SPECS/pkg.spec

Inspection and help

When to use Command
Expand an RPM macro expression rpmbuild -E '%{_topdir}'
Define macro on command line rpmbuild -D 'my_macro 1' -ba SPECS/pkg.spec
Show final rpmrc / macro config rpmbuild --showrc
Verbose build logging rpmbuild -v -ba SPECS/pkg.spec
Quiet output rpmbuild --quiet -ba SPECS/pkg.spec
Show help rpmbuild --help
Show RPM version rpmbuild --version

rpmbuild — command syntax

Synopsis from rpmbuild --help on Ubuntu 25.04 (RPM 4.20.1):

text
rpmbuild [OPTION...] [ <specfile> | <tarball> | <source package> ]

rpmbuild reads a .spec file that defines %prep, %build, %install, and %files sections. By default it expects a directory tree under ~/rpmbuild (BUILD, BUILDROOT, RPMS, SOURCES, SPECS, SRPMS). Override with --define "_topdir /path".


rpmbuild — command examples

Essential Minimal spec and build tree under /tmp

On Ubuntu, install rpm to get rpmbuild. For learning or CI, a custom _topdir under /tmp keeps artifacts out of your home directory.

Create the tree and a noarch demo spec:

Package installs and updates in this section use dnf command.

bash
rm -rf /tmp/rpmbuild-test
mkdir -p /tmp/rpmbuild-test/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
echo 'Hello from demo package' > /tmp/rpmbuild-test/SOURCES/hello.txt
cat > /tmp/rpmbuild-test/SPECS/demo.spec << 'EOF'
Name:           demo
Version:        1.0
Release:        1%{?dist}
Summary:        Minimal demo package for rpmbuild cheat sheet
License:        MIT
BuildArch:      noarch
%description
A tiny noarch package used to demonstrate rpmbuild stages.
%prep
mkdir -p demo-1.0
cp %{_sourcedir}/hello.txt demo-1.0/
%build
# no compile step
%install
mkdir -p %{buildroot}/usr/share/demo
install -m 644 demo-1.0/hello.txt %{buildroot}/usr/share/demo/hello.txt
%files
/usr/share/demo/hello.txt
EOF

Verify layout:

bash
ls /tmp/rpmbuild-test/SPECS/ /tmp/rpmbuild-test/SOURCES/

Sample output:

text
/tmp/rpmbuild-test/SPECS/:
demo.spec

/tmp/rpmbuild-test/SOURCES/:
hello.txt

On RHEL hosts, rpmdev-setuptree creates the same layout under ~/rpmbuild.

Essential Stop after %prep with -bp

-bp runs only the %prep section — unpack sources and apply patches — so you can inspect the build directory before compile steps.

Run the command:

bash
rpmbuild --define "_topdir /tmp/rpmbuild-test" -bp /tmp/rpmbuild-test/SPECS/demo.spec

Sample output:

text
Building target platforms: noarch
Building for target noarch
Executing(%mkbuilddir): /bin/sh -e /var/tmp/rpm-tmp.W8yM58
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.R4JkNE
+ umask 022
+ cd /tmp/rpmbuild-test/BUILD/demo-1.0-build
+ mkdir -p demo-1.0
+ cp /tmp/rpmbuild-test/SOURCES/hello.txt demo-1.0/
+ RPM_EC=0
+ jobs -p
+ exit 0

Confirm sources landed under BUILD:

bash
cat /tmp/rpmbuild-test/BUILD/demo-1.0-build/demo-1.0/hello.txt

Sample output:

text
Hello from demo package
Essential Binary and source RPM with -ba

-ba completes all stages and writes .rpm files under RPMS/ and SRPMS/.

Run the command:

bash
rpmbuild --define "_topdir /tmp/rpmbuild-test" -ba /tmp/rpmbuild-test/SPECS/demo.spec

Sample output (tail):

text
Processing files: demo-1.0-1.noarch
Provides: demo = 1.0-1
Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 ...
Wrote: /tmp/rpmbuild-test/SRPMS/demo-1.0-1.src.rpm
Wrote: /tmp/rpmbuild-test/RPMS/noarch/demo-1.0-1.noarch.rpm

List artifacts:

bash
ls -la /tmp/rpmbuild-test/RPMS/noarch/ /tmp/rpmbuild-test/SRPMS/

Install on an RPM-based host with sudo rpm -ivh or sudo dnf install; on Ubuntu, use rpm -qlp to inspect without installing.

Common Compile with -bc

-bc runs %prep and %build. For C/C++ packages this is where make runs; our demo spec has an empty %build.

Run the command:

bash
rpmbuild --define "_topdir /tmp/rpmbuild-test" -bc /tmp/rpmbuild-test/SPECS/demo.spec

Sample output ends with:

text
Executing(%build): /bin/sh -e /var/tmp/rpm-tmp.GHivSu
+ umask 022
+ cd /tmp/rpmbuild-test/BUILD/demo-1.0-build
+ RPM_EC=0
+ jobs -p
+ exit 0

If %build fails, fix the spec or BuildRequires before continuing to -bi.

Common Install into buildroot with -bi

-bi runs through %install, placing files under BUILDROOT — a fake filesystem root used to assemble the RPM payload.

Run the command:

bash
rpmbuild --define "_topdir /tmp/rpmbuild-test" -bi /tmp/rpmbuild-test/SPECS/demo.spec

Check the staged file:

bash
find /tmp/rpmbuild-test/BUILD -path '*/BUILDROOT/*' -type f

Sample output:

text
/tmp/rpmbuild-test/BUILD/demo-1.0-build/BUILDROOT/usr/share/demo/hello.txt

The %files section must list every path under BUILDROOT or -ba fails the file manifest check.

Common Verify %files with -bl

-bl prints the path list RPM expects from %files — catch missing or unpackaged files before a full build.

Run after a successful -bi:

bash
rpmbuild --define "_topdir /tmp/rpmbuild-test" -bl /tmp/rpmbuild-test/SPECS/demo.spec

Sample output includes:

text
/usr/share/demo/hello.txt

If a file exists in BUILDROOT but not in %files, the full build warns about unpackaged files.

Common Override _topdir for CI or multi-tree builds

Multiple projects on one host should not share a single ~/rpmbuild — point each job at its own tree.

Run the command:

bash
rpmbuild --define "_topdir /tmp/rpmbuild-test" -E '%{_topdir}'

Sample output:

text
/tmp/rpmbuild-test

Use the same --define "_topdir ..." prefix on every rpmbuild invocation for that project.

Advanced Remove test trees after lab runs

Lab builds under /tmp should be deleted when you finish capturing output — do not leave stale BUILDROOT trees on shared hosts.

Remove the demo tree:

bash
rm -rf /tmp/rpmbuild-test
ls /tmp/rpmbuild-test 2>&1

Sample output:

text
ls: cannot access '/tmp/rpmbuild-test': No such file or directory

Use rpmbuild --clean inside a spec's %clean section for production workflows; for throwaway /tmp trees, rm -rf is enough.


rpmbuild — when to use / when not

Use rpmbuild when Use something else when
  • You package software for RHEL, Fedora, AlmaLinux, or openSUSE in RPM format
  • You maintain a .spec file with %prep, %build, %install, and %files
  • You rebuild a .src.rpm for a different architecture or distro policy
  • You need staged builds (-bp, -bc, -bi) to debug a failing spec
  • You ship native Debian/Ubuntu packages → dpkg-buildpackage / debuild
  • You only install or query existing RPMs → rpm or dnf
  • You distribute portable bundles → containers, tarballs, or language-specific packagers
  • You are on Ubuntu and have no RPM consumers — installing rpm is optional tooling, not the distro packaging path

rpmbuild vs dpkg-buildpackage

rpmbuild (RPM) dpkg-buildpackage (Debian)
Primary distros RHEL, Fedora, SUSE Debian, Ubuntu
Recipe file .spec debian/control, debian/rules
Output format .rpm / .src.rpm .deb / source packages
On Ubuntu 25.04 Available via apt install rpm Native packaging tool

On Ubuntu, install rpm when you need to build or inspect RPMs for cross-distro distribution — not for packages destined for apt. Debian-native packaging uses dpkg and dpkg-buildpackage; see the dpkg command for install and list operations behind apt.


Packaging and package management around RPM workflows.

Command One line
rpmbuild Build RPMs from spec files (this page)
How to create an RPM package Full spec-file walkthrough

Browse the full index in our Linux commands reference.


rpmbuild — interview corner

What does rpmbuild do?

rpmbuild executes the stages defined in an RPM .spec file: prepare sources (%prep), compile (%build), install into a fake root (%install), and pack the result into binary and/or source RPM files.

Stage flags like -bp, -bc, -bi, and -ba stop after the named phase — useful when debugging a broken build.

A strong answer is:

"rpmbuild runs the spec file pipeline — prep, build, install, package — and -bp/-bc/-bi/-ba let me stop at each stage. Output lands under RPMS and SRPMS."

What is the difference between -bp, -bc, -bi, and -ba?
Flag Stops after
-bp %prep — sources unpacked
-bc %build — compiled
-bi %install — files in BUILDROOT
-ba Full package — binary and source RPMs written

A strong answer is:

"-bp stops after prep, -bc after compile, -bi after install into buildroot, -ba builds the final RPM and SRPM."

What is _topdir in rpmbuild?

_topdir is the RPM build tree root — typically ~/rpmbuild with BUILD, BUILDROOT, RPMS, SOURCES, SPECS, and SRPMS.

Override per project:

bash
rpmbuild --define "_topdir /tmp/myproject" -ba SPECS/foo.spec

A strong answer is:

"_topdir is the rpmbuild workspace — BUILD, SOURCES, SPECS, RPMS, etc. I override it with --define for CI or multiple projects."

Can you use rpmbuild on Ubuntu?

Yes — install the rpm package (sudo apt install rpm). You get rpmbuild for building or inspecting RPM format.

Ubuntu's native packages use .deb and dpkg-buildpackage, not .spec files, for distribution through apt.

A strong answer is:

"On Ubuntu I apt install rpm for rpmbuild when I need RPM output; for Debian-native packages I use dpkg-buildpackage, not rpmbuild."

What belongs in an RPM %files section?

The %files section lists every path installed under BUILDROOT that should ship in the binary RPM — config files, binaries, documentation, and %dir entries for empty directories.

Run rpmbuild -bl after -bi to verify the manifest before -ba.

A strong answer is:

"%files lists packaged paths from BUILDROOT — I verify with rpmbuild -bl before the full -ba so nothing is missing or unpackaged."


Troubleshooting

Symptom Likely cause Fix
rpmbuild: command not found on Ubuntu rpm package not installed sudo apt install rpm
error: File not found in %files Path not installed in %install Fix %install or adjust %files; verify with find BUILDROOT
Unpackaged file(s) warning File in BUILDROOT not listed in %files Add path to %files or exclude in %exclude
Build uses wrong ~/rpmbuild Missing _topdir Pass --define "_topdir /path" on every invocation
%prep macro failure Missing tarball in SOURCES Place archive in SOURCES and match Source0: in spec
Failed build dependencies BuildRequires not installed Install deps or use dnf builddep on Fedora; on Ubuntu install -dev packages manually

Rohan Timalsina

is a technical writer and Linux enthusiast who writes practical guides on Linux commands and system administration. He focuses on simplifying complex topics through clear explanations.