++ed by:
Author image Lubomir Rintel
and 1 contributors


RPM2 - Perl bindings for the RPM Package Manager API


  use RPM2;

  my $db = RPM2->open_rpm_db();

  my $i = $db->find_all_iter();
  print "The following packages are installed (aka, 'rpm -qa'):\n";
  while (my $pkg = $i->next) {
    print $pkg->as_nvre, "\n";

  $i = $db->find_by_name_iter("kernel");
  print "The following kernels are installed (aka, 'rpm -q kernel'):\n";
  while (my $pkg = $i->next) {
    print $pkg->as_nvre, " ", int($pkg->size()/1024), "k\n";

  $i = $db->find_by_provides_iter("kernel");
  print "The following packages provide 'kernel' (aka, 'rpm -q --whatprovides kernel'):\n";
  while (my $pkg = $i->next) {
    print $pkg->as_nvre, " ", int($pkg->size()/1024), "k\n";

  print "The following packages are installed (aka, 'rpm -qa' once more):\n";
  foreach my $pkg ($db->find_by_file("/bin/sh")) {
    print $pkg->as_nvre, "\n";

  my $pkg = RPM2->open_package("/tmp/XFree86-4.1.0-15.src.rpm");
  print "Package opened: ", $pkg->as_nvre(), ", is source: ", $pkg->is_source_package, "\n";


The RPM2 module provides an object-oriented interface to querying both the installed RPM database as well as files on the filesystem.


Pretty much all use of the class starts here. There are three main entrypoints into the package -- either through the database of installed rpms (aka the rpmdb), through a file on the filesystem (such as kernel-2.4.9-31.src.rpm or kernel-2.4.9-31.i386.rpm, or via an rpm transaction.

You can have multiple RPM databases open at once, as well as running multiple queries on each. That being said if you expect to run a transaction to install or erase some rpms, you will need to cause any RPM2::DB and RPM2::PackageIterator objects to go out of scope. For instance:

        $db = RPM2->open_rpm_db();
        $i  = $db->find_by_name("vim");
        $t  = create_transaction();
        while($pkg = $i->next()) {

Would end up in a dead lock waiting for $db, and $i (the RPM2::DB and RPM2::PackageIterator) objects to releaase their read lock on the database. The correct way of handling this then would be to do the following before running the transaction:

        $db = undef;
        $i  = undef;

That is to explicitly cause the RPM2::DB and RPM2::PackageIterator objects to go out of scope.

open_rpm_db(-path => "/path/to/db")

As it sounds, it opens the RPM database, and returns it as an object. The path to the database (i.e. -path) is optional.


Opens a specific package (RPM or SRPM). Returns a Header object.


Creates an RPM2::Transaction. This can be used to install and remove packages. It, also, exposes the dependency ordering functionality. It takes as an optional argument verify signature flags. The following flags are available:


You don't ever have to specify this, but you could if you wanted to do so. This will check headers, not require a files payload, and support all the various hash and signature formats that rpm supports.


Don't check the header.


Require that a files payload be part of the RPM (Chip is this right?).


RPM DB object methods


Returns an iterator object that iterates over the entire database.


Returns an list of all of the results of the find_all_iter() method.


Returns an iterator that returns all packages that contain a given file.


Ditto, except it just returns the list


You get the idea. This one is for iterating by package name.


Ditto, except it returns a list.


This one iterates over provides.


Ditto, except it returns a list.


This one iterates over requires.


Ditto, except it returns a list.

RPM Database Iterator Methods

Once you have a a database iterator, then you simply need to step through all the different package headers in the result set via the iterator.


Return the next package header in the result set.


Return the list of all the package headers in the result set of the iterator.

RPM Header object methods

In addition to the following methods, all tags have simple accessors; $hdr->epoch() is equivalent to $hdr->tag('epoch').

The <=> and cmp operators can be used to compare versions of two packages.


Returns the value of the tag $tagname.




Returns a true value if the package is a source package, false otherwise.


Returns the filename of the package.


Returns the rpm database offset for the package.


Returns a string formatted like:


If epoch is undefined for this package, it and the leading colon are omitted.




Returns a list of hash refs containing the change log data of the package. The hash keys represent individual change log entries, and their keys are: time (the time of the changelog entry), name (the "name", ie. often the email address of the author of the entry), and text (the text of the entry).

Transaction object methods

Transactions are what allow you to install, upgrade, and remove rpms. Transactions are created, have elements added to them (i.e. package headers) and are ran. When run the updates to the system and the rpm database are treated as on "transaction" which is assigned a transaction id. This can be queried in install packages as the INSTALLTID, and for repackaged packages they have the REMOVETID set.

add_install($pkg, $upgrade)

Adds a package to a transaction for installation. If you want this to be done as a package upgrade, then be sure to set the second optional parameter to 1. It will return 0 on failure and 1 on success. Note, this should be obvious, but the package header must come from an rpm file, not from the RPM database.


Adds a package to a transaction for erasure. The package header should come from the database (i.e. via an iterator) and not an rpm file.


Returns the number of elements in a transaction (this is the sum of the install and erase elements.


Closes the rpm database. This is needed for some ordering of transactions for non-install purposes.


Verify that the dependencies for this transaction are met. Returns 0 on failure and 1 on success.


Order the elements in dependency order.


Return a list of elements as they are presently ordered. Note, this returns the NEVR's not the package headers.


Run the transaction. This will automatically check for dependency satisfaction, and order the transaction.


Make package installation and removal better (-;.

Signature validation.


0.01 Initial release.
0.68 RPM 4.6 Support.
0.69 RPM 4.9 Support.


RPM2 was originally written by Chip Turner <cturner@pattern.net> and contributions were made by numerous authors. It is currently maintained by Lubomir Rintel <lkundrak@v3.sk>.

The source code is kept in a Git repository at https://github.com/lkundrak/perl-RPM2.git.


Report the bugs (and feature requests) at http://rt.cpan.org/Public/Bug/Report.html?Queue=RPM2. Alternatively, you can report a bug by sending mail to <bug-RPM2@rt.cpan.org>.


This module is free software and is licensed under the same terms as Perl itself.


perl. The original RPM module.