Using the rpm command – part 1

·6 min read·paskov

RPM logo

This article shows you how to use the rpm command to perform package management tasks, such as installing and removing packages and querying the RPM database. The examples are from openSUSE 11.4.

Installing, upgrading, and removing system packages requires root privileges. You can run the query commands as a regular user.

1. Installing, upgrading, and removing packages

1.1. Installing packages

To install a package, use rpm -i (or rpm --install) followed by the package file:

rpm -i <package-file.rpm>

For example, to install htop, an interactive text-mode process viewer, download its RPM package and install it as follows:

rpm -i htop-0.9-11.1.x86_64.rpm

You can also supply an FTP or HTTP URL instead of a local package file. In that case, RPM downloads the package before installing it.

The table below describes some useful installation options. You can pass them after -i:

rpm -i <option> <package-file.rpm>

For a full list of options, read the RPM manual page by running man rpm.

Option Description Usage example
-h, --hash Prints hash marks (#) to show installation progress. rpm -ih htop-0.9-11.1.x86_64.rpm
-v, --verbose Provides additional output. Combine it with -h for a more informative progress display. rpm -iv htop-0.9-11.1.x86_64.rpm
--test Checks for potential conflicts without installing the package. rpm -i --test htop-0.9-11.1.x86_64.rpm
--excludedocs Skips files marked as documentation, including manual pages and Texinfo documents. rpm -i --excludedocs htop-0.9-11.1.x86_64.rpm
--replacepkgs Reinstalls an already installed package. This can be useful for restoring damaged package files. rpm -i --replacepkgs htop-0.9-11.1.x86_64.rpm
--force Combines --replacepkgs, --replacefiles, and --oldpackage, allowing reinstallation, replacement of files owned by other packages, and installation of older versions. Dependency checks still apply. rpm -i --force htop-0.9-11.1.x86_64.rpm
--ignoresize Skips the check for sufficient disk space on mounted file systems. rpm -i --ignoresize htop-0.9-11.1.x86_64.rpm
--ignorearch Allows installation even if the package architecture does not match the host architecture. rpm -i --ignorearch htop-0.9-11.1.x86_64.rpm
--nodeps Skips dependency checks. The installed program may fail to run if required dependencies are missing. rpm -i --nodeps htop-0.9-11.1.x86_64.rpm
--ignoreos Allows installation even if the package’s target operating system does not match the host’s. rpm -i --ignoreos htop-0.9-11.1.x86_64.rpm

1.2. Upgrading packages

To upgrade a package, use rpm -U (or rpm --upgrade) followed by the new package file:

rpm -U <package-file.rpm>

For example, if a newer htop package were named htop-1.0-1.x86_64.rpm, you would download it and run:

rpm -U htop-1.0-1.x86_64.rpm

During an upgrade, RPM:

  1. Installs the new version of the package.
  2. Removes the previously installed versions, if any exist.

If the package is not already installed, rpm -U installs it. Like rpm -i, it also accepts an FTP or HTTP URL and downloads the package before proceeding.

The installation options described in section 1.1 also apply to rpm -U. Refer to man rpm for the full list of command-line options.

1.3. Removing packages (erasing)

RPM calls the process of removing packages erasing. Use rpm -e (or rpm --erase) followed by one or more installed package names:

rpm -e <package-name-1> <package-name-2> ...

For example, to remove htop, run:

rpm -e htop

RPM performs several steps during package removal:

  • It checks the RPM database to make sure that no other packages depend on the package being erased.
  • It executes a pre-uninstall script defined by the package (if one exists).
  • It checks for modified configuration files and may save copies of them.
  • It removes the package’s files, preserving files that are still owned by other packages.
  • It executes a post-uninstall script defined by the package (if one exists).
  • It removes the package’s entry from the RPM database.

The table below describes some useful removal options. You can pass them after -e:

rpm -e <option> <package-name>

For a full list of options, run man rpm.

Option Description Usage example
--test Checks whether the package can be removed without actually removing it. rpm -e --test htop
--noscripts Skips pre- and post-uninstall scripts, which may leave cleanup tasks unfinished. rpm -e --noscripts htop
--nodeps Skips dependency checks. Removing a package that other packages require may leave them unable to function. rpm -e --nodeps htop

2. Querying packages

One of the most useful features of rpm is its ability to query the RPM database. You can retrieve package details, lists of installed files, dependencies, and more.

Use rpm -q with additional options to choose what information to display. Let’s look at some of these options.

2.1. Obtaining general information about a package

Use rpm -qi to display general information about an installed package. For example, to retrieve information about htop, run:

rpm -qi htop

This produces output like the following:

Name        : htop                         Relocations: (not relocatable)
Version     : 0.9                               Vendor: openSUSE
Release     : 11.1                          Build Date: Sat 19 Feb 2011 06:45:05 AM EET
Install Date: Wed 20 Jul 2011 02:50:36 PM EEST      Build Host: build22
Group       : System/Monitoring             Source RPM: htop-0.9-11.1.src.rpm
Size        : 151367                           License: GPLv2+
Signature   : RSA/8, Sat 19 Feb 2011 06:45:09 AM EET, Key ID b88b2fd43dbdc284
Packager    : http://bugs.opensuse.org
URL         : http://htop.sourceforge.net
Summary     : An Interactive text-mode Process Viewer for Linux
Description :
htop is an interactive text-mode process viewer for Linux. It aims to
be a better 'top' and requires ncurses. It is tested with Linux 2.6,
but is also reported to work (and was originally developed) with the

2.4 series.

Authors:
--------
Hisham H. Muhammad
Distribution: openSUSE 11.4

2.2. Listing files that belong to a package

Use rpm -ql to list the files that belong to an installed package. For example, to list htop’s files, run:

rpm -ql htop

This produces output like the following:

/usr/bin/htop
/usr/share/applications/htop.desktop

/usr/share/doc/packages/htop
/usr/share/doc/packages/htop/COPYING

/usr/share/man/man1/htop.1.gz
/usr/share/pixmaps/htop.png

Use rpm -qc to list only a package’s configuration files. For example, to list the configuration files for tomcat6, run:

rpm -qc tomcat6

This produces output like the following:

/etc/logrotate.d/tomcat6

/etc/tomcat6/catalina.policy
/etc/tomcat6/catalina.properties
/etc/tomcat6/context.xml

/etc/tomcat6/logging.properties
/etc/tomcat6/server.xml
/etc/tomcat6/tomcat-users.xml

/etc/tomcat6/tomcat6.conf
/etc/tomcat6/web.xml

Use rpm -qd to list only a package’s documentation files.

Use rpm -qs to display each file’s installation state as recorded in the RPM database. Common states include:

  1. normal – The file was installed and has not been replaced by another package.
  2. replaced – The file was replaced by a file from another package.
  3. not installed – The file was skipped during installation, for example because documentation was excluded with --excludedocs.

These states describe how RPM installed the files. To check for later changes to their contents or attributes, use rpm -V instead.

Here is an example of the output from rpm -qs tomcat6:

normal        /etc/init.d/tomcat6
normal        /etc/logrotate.d/tomcat6
normal        /etc/tomcat6
normal        /etc/tomcat6/Catalina
normal        /etc/tomcat6/catalina.policy
normal        /etc/tomcat6/catalina.properties
normal        /etc/tomcat6/context.xml
normal        /etc/tomcat6/logging.properties
normal        /etc/tomcat6/server.xml
normal        /etc/tomcat6/tomcat-users.xml
normal        /etc/tomcat6/tomcat6.conf
normal        /etc/tomcat6/web.xml
.......

The word normal at the start of each line is the file’s state.

2.3. Finding which package provides a file

Use rpm -qf to find which installed package owns a file. For example, to find the package that provides the htop binary, run:

rpm -qf /usr/bin/htop

The result should look like this:

htop-0.9-11.1.x86_64

rpm -qf requires the file’s path, which is /usr/bin/htop in this example. If you don’t know where an executable is located, you can use which to look it up and pass the result to rpm -qf:

rpm -qf "$(which htop)"

2.4. Querying all installed packages

Use rpm -qa to list all installed packages. For example, running it on my openSUSE system produces output like this:

oziris:/home/paskov # rpm -qa
licenses-20070810-92.1.noarch
netcat-1.10-1008.1.x86_64
libpth20-2.0.7-124.1.x86_64
libattr-2.4.44-11.1.x86_64
pptp-1.7.2-31.1.x86_64
libasm1-0.149-2.7.x86_64
perl-Net-Daemon-0.43-90.1.x86_64
xz-5.0.0-13.1.x86_64
usbutils-001-3.1.x86_64
....

The output of rpm -qa can be quite long, so you may want to append it to a file or view it with the more pager:

rpm -qa >> install_list # Appends the output to a file
rpm -qa | more # Displays the output with the more pager

Conclusion

This concludes part one of the “Using the rpm command” series. In the following articles, we’ll look at more query options, ways to format the output of rpm -q, and how to develop applications that use librpm.