How to Conditionally Add RPM Dependencies Based on Linux Distribution Version (EL5/EL6)


2 views

When building RPM packages that need to support multiple RHEL/CentOS versions (particularly EL5 and EL6), you might encounter situations where certain dependencies only exist or are required on specific distributions. The challenge is to make these dependencies conditional in your spec file.

The initial attempts shown in the question fail for several reasons:

%if 0%{?redhat} == 5 || 0%{?centos} == 5
Requires:   kmod-coretemp
%endif

This doesn't work because:

  • The redhat and centos macros aren't guaranteed to be set
  • Numeric comparisons in RPM spec files can be tricky due to macro expansion timing

Here's the correct way to handle distribution-specific dependencies:

%if 0%{?rhel} == 5 || 0%{?centos_version} == 5
Requires: kmod-coretemp
%endif

Even better, use the dist tag with proper string comparison:

%if "%{?dist}" == ".el5"
Requires: kmod-coretemp
%endif

To debug these conditions, you can add print statements in your spec file:

%{!?dist:%define dist .el5}  # For testing purposes
%{echo:Dist is %{dist}}
%{echo:RHEL version is %{rhel}}

For more complex scenarios, consider using virtual provides:

%if "%{?dist}" == ".el5"
Provides: el5-specific-deps = %{version}
Requires: kmod-coretemp
%endif
  • Always test your conditions on both target distributions
  • Consider using %{rhel} instead of %{dist} when possible
  • Document your conditional dependencies clearly in the spec file
  • Use comments to explain why certain dependencies are version-specific

Here's a full spec file snippet demonstrating proper conditional requires:

Name: my-python-app
Version: 1.0
Release: 1%{?dist}

# EL5-specific dependencies
%if 0%{?rhel} == 5
Requires: kmod-coretemp
%endif

# Common dependencies
Requires: python >= 2.4

When maintaining packages across multiple RHEL/CentOS versions, you'll often encounter distribution-specific dependencies. The particular case mentioned involves adding kmod-coretemp only for EL5 systems while keeping a unified spec file.

The fundamental issue with the initial approach lies in how RPM processes conditional macros. The %{?dist} macro expands to strings like el5 or el6, but direct string comparison in RPM spec files requires special handling.

Here are three verified approaches to implement version-specific dependencies:

# Method 1: Using rpm --eval to check distribution tag
%if 0%{?rhel} == 5 || 0%{?centos} == 5
Requires: kmod-coretemp
%endif
# Method 2: Alternative distro version detection
%if 0%{?el5}
Requires: kmod-coretemp
%endif
# Method 3: Using the dist macro with proper syntax
%if "%{?dist}" == ".el5"
Requires: kmod-coretemp
%endif

1. Don't forget the zero prefix (0%{?var}) which handles cases where the macro might be undefined
2. String comparisons require quotes around both the macro and comparison value
3. The %{dist} macro typically includes a leading dot (e.g., ".el5")

For more complex requirements across different distributions:

%if 0%{?rhel} >= 7
Requires: systemd-units
%else
Requires: sysvinit-tools
%endif

Always verify your conditionals by examining the built RPM:

rpm -qp --requires yourpackage.rpm