How to Install Only Package Dependencies Using Yum (Without the Main Package)


3 views

When developing RPM packages or testing installation scenarios, we often need just the dependencies installed - not the main package itself. Yum lacks a direct --only-dependencies flag like some other package managers, but we can achieve this through several methods.

For developers working with RPM spec files:

# First install yum-utils if needed
sudo yum install -y yum-utils

# Install build dependencies for a spec file
yum-builddep your-package.spec

For more control over the dependency list:

# Get all dependencies
repoquery --requires --resolve <package> | sort -u > deps.list

# Install them manually
sudo yum install $(cat deps.list)

For testing various installation scenarios:

# Download all package files (main package + dependencies)
yum install --downloadonly --downloaddir=./package_deps <package>

# Then install just the dependencies from local files
sudo yum --nogpgcheck localinstall $(repoquery --requires --resolve <package> | sort -u | grep -v <package> | xargs -I{} find ./package_deps -name "{}*.rpm")

For maintaining consistent test environments:

# Create a dependency list with versions
repoquery --requires --resolve <package> | xargs rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}\n' > frozen_deps.txt

# Later restore exact versions
sudo yum install $(cat frozen_deps.txt)

This approach enables:

  • Clean environment testing without main package pollution
  • Faster iteration cycles when rebuilding packages
  • Consistent dependency resolution across CI/CD pipelines
  • Better control over deployment scenarios

Some packages may have conditional dependencies. For these cases:

# View all possible dependencies (including conditional ones)
repoquery --requires --resolve --alldeps <package> | sort -u

# Filter out specific dependencies if needed
repoquery --requires --resolve <package> | grep -v "unwanted-dep" > filtered_deps.list

Example Dockerfile snippet for dependency-only installation:

FROM centos:7
RUN yum install -y yum-utils \
    && yum-builddep -y your-package \
    && yum clean all
# Continue with your build steps

When testing RPM packages across different system states, developers often need a clean environment with all dependencies pre-installed. The standard yum install command installs both the target package and its dependencies, which isn't ideal for scenarios where you want to test multiple package versions against the same dependency base.

YUM provides two powerful flags that can be combined for this purpose:

sudo yum install --downloadonly --installroot=/path/to/temp/root package-name

This command will:
1. Download all dependencies without installing them
2. Use a temporary root directory to avoid affecting your main system
3. Leave your actual system pristine for testing

For a more complete solution that actually installs (not just downloads) dependencies:

sudo yum deplist package-name | awk '/provider:/ {print $2}' | sort -u | xargs yum -y install

This pipeline:
1. Lists all dependencies using deplist
2. Extracts package names
3. Removes duplicates
4. Installs them without the main package

Here's how I test multiple versions of nginx against the same dependencies:

# Create dependency baseline
sudo yum deplist nginx | awk '/provider:/ {print $2}' | sort -u > nginx-deps.txt

# Install just dependencies
sudo yum -y install $(cat nginx-deps.txt)

# Take system snapshot
sudo lvcreate -L 10G -s -n nginx-test-base /dev/mapper/vg00-root

# Test different nginx versions
for version in 1.18.0 1.20.1 1.22.0; do
  sudo rpm -ivh --test nginx-${version}.rpm
  # Revert to clean state after each test
  sudo lvconvert --merge /dev/mapper/vg00-nginx-test-base
done

YUM's transaction history can help identify dependencies after the fact:

# Find the transaction ID
sudo yum history list package-name

# View dependencies from that transaction
sudo yum history info [ID] | grep -A 20 "Packages Altered"

For modern workflows, consider using Podman/Docker:

# Create dependency container
podman run -ti --rm fedora:latest sh -c \
  "dnf -y install --downloadonly nginx && dnf -y install $(dnf repoquery --requires nginx)"

This approach gives you a completely isolated environment for dependency testing.