how to get list of rpm packages installed in the system

16,381

Solution 1

I guess you can always use the rpm command:

$ rpm --query --all  --qf "%-30{NAME} - %{VERSION}\n"

You can then use this in a wide variety of ways:

use autodie;
open my $RPM_FH, "-|", qq(rpm --query --all --qf "%-30{NAME} - %{VERSION}\n");
my @rpmLines = <$RPM_FH>;
close $RPM_FH;

Or:

 my @rpmLines = qx(rpm --query --all --qf "%-30{NAME} - %{VERSION}\n");

I've also found RPM::Database which would be a more Perlish way of doing things. This package ties the RPM database to a hash:

use RPM::Database;

tie %RPM, "RPM::Database" or die "$RPM::err";

for (sort keys %RPM)
{
    ...
}

I have never used it, so I'm not sure exactly how it would work. For example, I assume that the value of each hash entry is some sort of database object. For example, I would assume that it would be important to know the version number and the files in your RPM package, and there must be someway this information can be pulled, but I didn't see anything in RPM::Database or in RPM::HEader. Play around with it. You can use Data::Dumper to help explore the objects returned.

WARNING: Use Data::Dumper to help explore the information in the objects, and the classes. Don't use it to figure out how to pull the information directly from the objects. Use the correct methods and classes.

Solution 2

The easiest way is probably to shell out to the rpm program.

chomp(my @rpms = `rpm -qa`);

Solution 3

Depending on how to interpret your question, the correct answer can be:

rpm -qR perl
Share:
16,381
doubledecker
Author by

doubledecker

Updated on June 05, 2022

Comments

  • doubledecker
    doubledecker almost 2 years

    how to get list of all rpm packages installed on Linux using Perl. any help is appreciated.