LinuxCommandLibrary

rpmbuild

Build RPM packages from source files

TLDR

Build binary and source packages

$ rpmbuild -ba [path/to/spec_file]
copy

Build a binary package without source package
$ rpmbuild -bb [path/to/spec_file]
copy

Specify additional variables when building a package
$ rpmbuild -bb [path/to/spec_file] --define "[variable1] [value1]" --define "[variable2] [value2]"
copy

SYNOPSIS

rpmbuild [OPTIONS] SPECSPEC | SRPMFILE | TARBALL

Common build stages:
  rpmbuild -ba SPECFILE - Build both binary and source packages.
  rpmbuild -bb SPECFILE - Build only the binary package.
  rpmbuild -bs SPECFILE - Build only the source package.

Other common usage:
  rpmbuild --rebuild SRPMFILE - Rebuild a binary RPM from a source RPM.
  rpmbuild --clean SPECFILE - Clean up the build directory after a build.
  rpmbuild --define 'MACRO VALUE' SPECFILE - Define a macro for the build.
  rpmbuild --with FEATURE SPECFILE - Enable a conditional feature in the spec file.
  rpmbuild --without FEATURE SPECFILE - Disable a conditional feature in the spec file.

PARAMETERS

-ba
    Build both binary and source packages from the specified .spec file.

-bb
    Build only the binary package from the specified .spec file.

-bs
    Build only the source RPM package from the specified .spec file.

-bp
    Prepare the sources (unpack archives, apply patches) in the build directory, then exit.

-bc
    Execute the %build section of the .spec file, after preparation.

-bi
    Execute the %install section of the .spec file, after compilation.

--rebuild SRPMFILE
    Unpack and build a binary RPM from a given source RPM package.

--clean
    Remove the temporary build directory (BUILD/ and BUILDROOT/) after the build process completes.

--define 'MACRO VALUE'
    Define or override a macro with a specified value during the build process. Useful for custom build options.

--with FEATURE
    Set the RPM macro _with_feature to 1, enabling conditional build paths in the .spec file.

--without FEATURE
    Set the RPM macro _without_feature to 1, disabling conditional build paths in the .spec file.

--sign
    Sign the resulting RPM package(s) with GPG using the configured keys.

--target ARCH
    Specify the target architecture for cross-compilation (e.g., armv7hl, x86_64).

--noclean
    Do not remove the temporary build directory after the build process, useful for debugging.

DESCRIPTION

rpmbuild is the primary command-line utility for creating RPM (Red Hat Package Manager) software packages. It automates the complex process of transforming source code into distributable .rpm files, which are used for software installation and management on RPM-based Linux distributions like Fedora, RHEL, CentOS, and openSUSE.

The tool relies on a .spec file, a detailed blueprint that describes how to build the software, including its sources, patches, build dependencies, compilation instructions (e.g., %prep, %build, %install), and metadata. rpmbuild orchestrates the entire build process, from unpacking source archives and applying patches to configuring, compiling, installing, and finally packaging the results into a binary RPM (.rpm) and/or a source RPM (.src.rpm).

It operates within a standardized build environment, typically located under ~/rpmbuild (or /usr/src/redhat on older systems), which contains dedicated subdirectories for sources, spec files, build outputs, and temporary build roots. This structured approach ensures consistency and reproducibility in package creation. rpmbuild is an indispensable tool for developers and package maintainers contributing to the RPM ecosystem.

CAVEATS

  • Requires a well-defined .spec file and a properly set up RPM build environment (e.g., ~/rpmbuild).
  • Building as the root user is strongly discouraged for security reasons; rpmbuild is designed to be run as a regular user.
  • Does not automatically resolve or install build dependencies; these must be declared in the spec file and met by the system.
  • The complexity of spec files can be high, requiring in-depth knowledge of packaging guidelines and the software being built.

SPEC FILE DIRECTIVES AND SECTIONS

The .spec file is the heart of rpmbuild. It is divided into several logical sections, each serving a specific purpose:

  • Name, Version, Release, Summary, License, URL: Package metadata.
  • Source: Specifies the original source archive(s).
  • Patch: Specifies patch files to apply.
  • BuildRequires, Requires: Define build-time and runtime dependencies.
  • %description: Detailed description of the package.
  • %prep: Prepares the source tree (unpacks, applies patches).
  • %build: Compiles the source code.
  • %install: Installs the compiled software into the build root (a temporary installation directory).
  • %check: Runs tests for the built software.
  • %files: Lists all files to be included in the final package.
  • %changelog: Records changes to the package over time.

STANDARD BUILD ENVIRONMENT

rpmbuild requires a specific directory structure to operate correctly. By default, this is found in ~/rpmbuild/ (or configurable via `_topdir` macro) and includes the following subdirectories:

  • BUILD/: Where the actual compilation and packaging takes place.
  • BUILDROOT/: A temporary root directory mimicking the final installation structure before packaging.
  • RPMS/: Output directory for binary RPM packages, further categorized by architecture.
  • SOURCES/: Stores original source tarballs and patch files referenced in the .spec file.
  • SPECS/: Stores the .spec files themselves.
  • SRPMS/: Output directory for source RPM packages.

HISTORY

rpmbuild is an integral part of the RPM (Red Hat Package Manager) system, which was originally developed by Red Hat for its Linux distribution. It evolved from simpler build scripts into a robust and standardized tool to automate the complex process of software packaging. Over the years, its core functionality, centered around the .spec file, has remained consistent, making it a cornerstone for package creation across various RPM-based Linux distributions. Its development has focused on reliability, reproducibility, and enabling sophisticated build workflows.

SEE ALSO

rpm(8): The core RPM package manager for querying, installing, and uninstalling packages., rpmspec(8): Utility for parsing and querying RPM spec files., rpmmacros(5): Describes the RPM macro configuration files., createrepo(8): Creates a YUM/DNF repository from a collection of RPM packages.

Copied to clipboard