How to query packages information with the rpm package manager

RPM is the recursive acronym for RPM Package Manager: it is the default low level package manager in some of the most famous and most used Linux distributions, such as Fedora, Red Hat Enterprise Linux, CentOS, OpenSUSE and their derivatives. The software, as you can expect, is free and open source; when invoked with the -q option it can be used to query packages to retrieve specific information, such as dependencies, recommendations, files etc. In this tutorial we learn how to perform such queries.

In this tutorial you will learn:

  • How to run rpm in “query” mode
  • How to obtain general information about a package
  • How to read a package changelog
  • How to list the files provided by a package
  • How to list the scripts used by a package
  • How to list a package dependencies and recommendations
  • How to list packages rendered obsolete by an rpm
main-image

How to query packages information with the rpm package manager

Software requirements and conventions used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Distributions which uses the rpm package manager
Software The rpm package manager
Other None
Conventions # – requires given linux-commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux-commands to be executed as a regular non-privileged user

Invoking rpm in query mode

The rpm package manager can be used to modify the status of the system, since it can be used to install, upgrade and remove packages (when invoked with the -i, -U and e options, respectively), or it can run in “query” mode, without the need of administrative privileges, when it is launched with the -q option, which is the short version of --query.

If a package is already installed on our system, all we need to do is to mention it by its name; if it is not the case, and we want to query a downloaded .rpm file, for example, we have to use the -p (--package) option, and provide the path of the file as argument. Let’s see some practical examples of how to retrieve information about a package.

Obtaining general information about a package

One of the basic things we may want to do when dealing when an rpm package is to display general information about it. To accomplish this task, all we have to do is to use the -i (or --info) option when invoking rpm in query mode. For the sake of this example we will work with the “gimp” package, which is already installed in the system. In this case we would run:

$ rpm -qi gimp


Immediately after we invoke the command, we receive an output similar to the following, which includes information, such as the software description, its version, architecture, installation date, size, etc:

Name        : gimp
Epoch       : 2
Version     : 2.10.24
Release     : 1.fc34
Architecture: x86_64
Install Date: Mon 21 Jun 2021 11:26:33 AM CEST
Group       : Unspecified
Size        : 109267268
License     : GPLv3+ and GPLv3
Signature   : RSA/SHA256, Sat 03 Apr 2021 02:00:02 AM CEST, Key ID 1161ae6945719a39
Source RPM  : gimp-2.10.24-1.fc34.src.rpm
Build Date  : Sat 03 Apr 2021 01:13:55 AM CEST
Build Host  : buildhw-x86-11.iad2.fedoraproject.org
Packager    : Fedora Project
Vendor      : Fedora Project
URL         : http://www.gimp.org/
Bug URL     : https://bugz.fedoraproject.org/gimp
Summary     : GNU Image Manipulation Program
Description :
GIMP (GNU Image Manipulation Program) is a powerful image composition and
editing program, which can be extremely useful for creating logos and other
graphics for web pages. GIMP has many of the tools and filters you would expect
to find in similar commercial offerings, and some interesting extras as well.
GIMP provides a large image manipulation toolbox, including channel operations
and layers, effects, sub-pixel imaging and anti-aliasing, and conversions, all
with multi-level undo.

Reading a package changelog

As we know, a changelog is a log in which all the changes made to a project are reported and ordered chronologically. If what we want to do is to read the changelog of an rpm package, we have to invoke the rpm package manager in query mode and use the --changelog option. Let’s see an example:

$ rpm -q --changelog gimp

Here is the (truncated) output of the command above on the latest version of Fedora (34):

[...]
 Thu Jan 09 2020 Josef Ridky <jridky@redhat.com> - 2:2.10.14-3
- Demodularizing of gimp (#1772469)

* Mon Nov 18 2019 Kalev Lember <klember@redhat.com> - 2:2.10.14-2
- Rebuild for libmypaint 1.4.0

* Mon Nov 04 2019 Kalev Lember <klember@redhat.com> - 2:2.10.14-1
- Update to 2.10.14

* Tue Aug 20 2019 Josef Ridky <jridky@redhat.com> - 2:2.10.12-3
- Fix default configuration for font folders (#1706653)

* Wed Jul 31 2019 Josef Ridky <jridky@redhat.com> - 2:2.10.12-2
- Fix issue with reading SVG files (#1715882)

* Thu Jul 25 2019 Fedora Release Engineering <releng@fedoraproject.org> - 2:2.10.12-1.1
- Rebuilt for https://fedoraproject.org/wiki/Fedora_31_Mass_Rebuild

* Thu Jun 13 2019 Kalev Lember <klember@redhat.com> - 2:2.10.12-1
- Update to 2.10.12

* Thu Apr 11 2019 Richard Shaw <hobbes1069@gmail.com> - 2:2.10.10-2
- Rebuild for OpenEXR 2.3.0.

* Mon Apr 08 2019 Josef Ridky <jridky@redhat.com> - 2:2.10.10-1
- New upstream release 2.10.10 (#1697119)

Listing all the files provided by a package

When we install a package on our system a series of files are copied in the appropriate places, as intended by its maintainer. If we want to list the files provided by a package that is already installed on our system we can invoke the
rpm package manager in query mode with the -l option (short for --list):

$ rpm -ql gimp


As we previously said, if we want to query a package before we install it, instead, we have to add the -p option, and provide the path of the package as argument:

$ rpm -qlp /path/to/package.rpm

The command above will produce the following result:

/etc/gimp
/etc/gimp/2.0
/etc/gimp/2.0/controllerrc
/etc/gimp/2.0/gimprc
/etc/gimp/2.0/gtkrc
/etc/gimp/2.0/menurc
/etc/gimp/2.0/sessionrc
/etc/gimp/2.0/templaterc
/etc/gimp/2.0/toolrc
/etc/gimp/2.0/unitrc
/usr/bin/gimp
/usr/bin/gimp-2.10
/usr/bin/gimp-console
/usr/bin/gimp-console-2.10
/usr/bin/gimp-test-clipboard-2.0
[...]

Listing the scripts used by a package

Some .rpm packages uses a series of scripts or “scriptlets” that are executed as part of the installation or removal process. If we want to take a look at them, we can do it invoking rpm in query mode with the --scripts
option. For the sake of this example we will checkout the scripts included in the systemd package:

$ rpm -q --scripts systemd

Below we can see the (truncated) output of the command above, when launched on Fedora 34. As you can see, before the scripts themselves we can notice the context in which they are used is also reported (e.g. “preinstall” or “postinstall”):

preinstall scriptlet (using /bin/sh):
getent group cdrom &>/dev/null || groupadd -r -g 11 cdrom &>/dev/null || :
getent group utmp &>/dev/null || groupadd -r -g 22 utmp &>/dev/null || :
getent group tape &>/dev/null || groupadd -r -g 33 tape &>/dev/null || :
getent group dialout &>/dev/null || groupadd -r -g 18 dialout &>/dev/null || :
getent group input &>/dev/null || groupadd -r input &>/dev/null || :
getent group kvm &>/dev/null || groupadd -r -g 36 kvm &>/dev/null || :
getent group render &>/dev/null || groupadd -r render &>/dev/null || :
getent group systemd-journal &>/dev/null || groupadd -r -g 190 systemd-journal 2>&1 || :
[...]

Another similar option is --triggers which is used to display the trigger scripts contained in a package. What are trigger scripts? They are basically scripts which are executed (triggered) when the status of another package
changes:

$ rpm -q --triggers systemd

With the above command we can visualize the trigger scripts contained in the systemd package:

triggerun scriptlet (using /bin/sh) -- systemd < 246.1-1
# This is for upgrades from previous versions before systemd-resolved became the default.
systemctl --no-reload preset systemd-resolved.service &>/dev/null || :

if systemctl -q is-enabled systemd-resolved.service &>/dev/null; then
  systemctl -q is-enabled NetworkManager.service 2>/dev/null && \
  ! test -L /etc/resolv.conf 2>/dev/null && \
  ! mountpoint /etc/resolv.conf &>/dev/null && \
  grep -q 'Generated by NetworkManager' /etc/resolv.conf 2>/dev/null && \
  echo -e '/etc/resolv.conf was generated by NetworkManager.\nRemoving it to let systemd-resolved manage this file.' && \
  mv -v /etc/resolv.conf /etc/resolv.conf.orig-with-nm && \
  ln -sv ../run/systemd/resolve/stub-resolv.conf /etc/resolv.conf 2>/dev/null || :

  systemctl start systemd-resolved.service &>/dev/null || :
fi
triggerpostun scriptlet (using /bin/sh) -- systemd < 247.3-2
# This is for upgrades from previous versions before oomd-defaults is available.
# We use %triggerpostun here because rpm doesn't allow a second %triggerun with
# a different package version.
systemctl --no-reload preset systemd-oomd.service &>/dev/null || :


Listing a package dependencies and recommendations

Another common task we may want to perform is to find the dependencies of an rpm package. The option which let us accomplish the task is -R (short for --requires). To check the dependencies of the “gimp” package, for example, we would run the following command:

$ rpm -qR gimp

Once we run the command, the list of software dependencies will be reported onscreen:

/usr/bin/python2
babl(x86-64) >= 0.1.78
config(gimp) = 2:2.10.24-1.fc34
fontconfig >= 2.12.4
freetype >= 2.1.7
gegl04(x86-64) >= 0.4.30
gimp-libs(x86-64) = 2:2.10.24-1.fc34
glib2 >= 2.56.2
gtk2 >= 2.24.32
hicolor-icon-theme
[...]

If instead of the -R option we use --recommends we can obtain a list of the software which is not strictly needed by the package we are querying, but are recommended by it:

$ rpm -q --recommends gimp

Listing the packages rendered obsolete by an rpm

In the same fashion of the examples above, we can see what packages are rendered obsolete by the package that we are querying. All we have to do is to use the --obsoletes option:

$ rpm -q --obsoletes gimp

Here is the output of the command in the example:

gimp < 2:2.10.24-1.fc34
gimp-help-browser < 2:2.10.24-1.fc34
gimp-unstable < 2:2.10

Conclusions

Rpm is a very powerful package manager: in this tutorial we saw how to run it in “query” mode. We saw how to obtain general information about a package, such as its version and architecture, how to read a package changelog, how to list all the files which will be installed by a package, how to take a look at the “scriptlets” that are used as part of the package management and how to obtain a list of a package dependencies and recommendations.