How to Download Already Installed RPM Packages in CentOS/Yum for Dependency Resolution


3 views

When working with CentOS/RHEL systems, developers often need to obtain the RPM packages of already installed software - particularly when:

  • Creating local repositories for air-gapped systems
  • Debugging dependency issues
  • Replicating environments across multiple servers

The most straightforward solution is combining yumdownloader with package querying:

# Install yum-utils if not present
sudo yum install yum-utils -y

# Download installed package + dependencies
yumdownloader --resolve openssl-devel

Yum maintains a cache of downloaded packages. To locate them:

# Find cache location
grep cachedir /etc/yum.conf

# Typical location (CentOS 7)
ls /var/cache/yum/x86_64/7/base/packages/

For complex dependency chains, use this script to get all installed dependencies:

#!/bin/bash
PKG=$1
REPO=$(yum repolist enabled | awk 'NR>1 {print $1}' | paste -sd ",")

yum deplist $PKG | \
awk '/provider:/ {print $2}' | \
sort -u | \
xargs yumdownloader --disablerepo="*" --enablerepo="$REPO" --resolve

For the specific openssl-devel scenario mentioned:

# Get exact package version
INSTALLED_VERSION=$(rpm -q openssl-devel --qf "%{VERSION}-%{RELEASE}")

# Download that specific version
yumdownloader openssl-devel-$INSTALLED_VERSION

Yum stores transaction history that can be replayed:

# Find the transaction ID
yum history list openssl-devel

# Re-download packages from transaction
yum history undo --download-only [TRANSACTION_ID]

For enterprise environments, consider setting up a local repo:

# Create repo directory
mkdir /var/local-repo

# Download all dependencies
repotrack openssl-devel -p /var/local-repo

# Create metadata
createrepo /var/local-repo

When working with CentOS systems, there are scenarios where you need to obtain the RPM packages of already installed software and their dependencies. This commonly occurs when:

  • Setting up local repositories
  • Creating offline installation packages
  • Documenting system configurations
  • Preparing identical environments

The most effective method is to use yumdownloader, which comes with the yum-utils package:


# First ensure yum-utils is installed
sudo yum install yum-utils -y

# Download the main package and its dependencies
yumdownloader --resolve openssl-devel

While the OP's approach didn't work directly, we can modify it:


sudo yum reinstall openssl-devel --downloadonly --downloaddir=./rpm_packages

This forces yum to download the packages again while keeping them in the specified directory.

For a comprehensive solution that captures all recursive dependencies:


repoquery --requires --resolve openssl-devel | xargs yumdownloader

If you need particular versions of packages:


yumdownloader openssl-devel-1.0.1e-4.fc18.x86_64

Here's a complete example for creating an offline repository of all openssl dependencies:


# Create directory for packages
mkdir -p /var/www/html/offline_repo/openssl_packages

# Download all dependencies
yumdownloader --resolve --destdir=/var/www/html/offline_repo/openssl_packages openssl-devel

# Create repository metadata
createrepo /var/www/html/offline_repo/openssl_packages

If you encounter "No package available" errors, try:

  1. Clearing yum cache: yum clean all
  2. Updating metadata: yum makecache
  3. Checking enabled repositories: yum repolist

While CentOS/RHEL systems don't provide a direct command to export already-installed RPMs, these methods effectively solve the problem. For production environments, consider automating this process with Ansible or shell scripts.