Using RPM to find package dependencies

I recently discovered that is it actually rather difficult to determine which packages depend on a particular package using the RedHat Package Manager, and I ended up writing a short script to list all the dependencies for a particular package.

Initially I tried to use  rpm  to discover what packages depended  lz4 .

The first thing I did was check the package names.

# rpm -q -a lz4

Then I checked to see what packages depended on these packages (or at least I thought I was checking for packages that depended on them).

rpm -q --whatrequires lz4-1.7.5-2.el7.i686 lz4-1.7.5-2.el7.x86_64
no package requires lz4-1.7.5-2.el7.i686
no package requires lz4-1.7.5-2.el7.x86_64

Great – Nothing depends on them! But, when I try to delete these packages I get an error…

# rpm --erase --allmatches lz4
error: Failed dependencies: is needed by (installed) systemd-libs-219-57.el7.x86_64 is needed by (installed) systemd-219-57.el7.x86_64 is needed by (installed) systemd-libs-219-57.el7.i686

What is going on?

It turns out that in RedHat each package provides one or more capabilities and can also depend on one or more capabilities provided by several different packages (NOT the packages themselves).

So to discover what packages depend on a particular package, you need to find out what capabilities that package provides and then check each one of those capabilities to see which packages depend on them.

rpm -q --provides lz4
lz4 = 1.7.5-2.el7
lz4(x86-64) = 1.7.5-2.el7
lz4 = 1.7.5-2.el7
lz4(x86-32) = 1.7.5-2.el7

rpm -q --whatrequires "" \
"lz4 = 1.7.5-2.el7" "lz4(x86-64) = 1.7.5-2.el7" "" \
"lz4(x86-32) = 1.7.5-2.el7"
no package requires lz4 = 1.7.5-2.el7
no package requires lz4(x86-64) = 1.7.5-2.el7
no package requires lz4(x86-32) = 1.7.5-2.el7

Yes – you really do need to include each capability in quotes!

Since I really don’t want to do that any more often than I have to I wrote a script to do it for me.

# Lists all the installed packages that are dependent on one of the packages
# specified.
# This  program  is free software: you can redistribute it and/or modify  it
# under the terms of the GNU General Public License as published by the Free
# Software  Foundation, either version 3 of the License, or (at your option)
# any later version.
# This  program  is  distributed  in the hope that it will  be  useful,  but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public  License
# for more details.
# You  should  have received a copy of the GNU General Public License  along
# with this program. If not, see <>
#  19 Sep 18   0.1   - Initial version - MEJT
#              0.2   - Now only evaluates the dependencies once and displays
#                      the number of dependent packages against each package
#                      name - MEJT
#                    - Added options to display the version number and force
#                      package names and the number of dependent packages to
#                      be shown ('--version' and '--verbose') - MEJT
#                    - Dependencies  not shown indented if the package names
#                      are shown - MEJT
# Scan command line arguments
while test $# -gt 0do
  case "$1" in
    echo "$0: version $_version"
    exit 0 ;;
    echo "syntax: $0 PACKAGE [PACKAGE...]"
    echo "  -v, --verbose           show package names and number of"
    echo "                          dependent packages"
    echo "      --help              display this help and exit"
    echo "      --version           output version information and exit"
    exit 0 ;;
  -*) # Unrecognized qualifier!
    echo "$0: unrecognized option '$1'"
    echo "Try '$0 --help' for more information."
    exit 0 ;;
  *) # Append each argument to args[] (preserving quoted strings).
    _count=$((_count+1)) ;;
# Get depandant packages for each package based on capability.
if [ "$_count" -gt 0 ]; then
  for _package in "${_args[@]}"do
    # Start by counting the number of dependancies.
    _dependancies=$(rpm -q "$_package" --provides | while read _x; \
      do rpm -q --whatrequires "$_x"done \
      | grep -v "no package" | grep -v "not installed" |sort -u)
    # If there are no dependencies check the next package.
    if [ "$(echo -n "$_dependancies" | wc -l)" -gt 0 ]; then
      if [ "$_count" -gt 1 ] || [ $_flag = true ]; then 
        echo "$_package : $(echo "$_dependancies" | wc -l)"
        echo "$_dependancies" | sed "s/^/  /"
      else # Don't indent dependencies unless displaying package name/count.
        echo "$_dependancies"
  echo "$0: no package specified."
  echo "Try '$0 --help' for more information."
  exit -1
# Exit!
exit $?

Now I can easily check the dependencies for multiple packages.

./ lz4 zlib
lz4 : 3
zlib : 75

This entry was posted in CentOS, RedHat and tagged , . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.