rebuild rpm database

How to Rebuild RPM Database in CentOS/RHEL

RPM is a popular package manager for RHEL, CentOS, Fedora Linux systems. It allows you to install, update, upgrade and delete packages in Linux systems. It uses an RPM database to keep track of all available packages on your system. It consists of files located in /var/lib/rpm. If this database is corrupted then your RPM package manager will not work properly. You will be unable to install packages, updates and upgrades will not happen properly. You may be unable to run any rpm or yum command successfully. There are several reasons why RPM database may get corrupted. It may be because of incomplete transactions, incompatible third-party packages or removal of certain critical packages. In such cases, you need to rebuild RPM database in CentOS/RHEL. In this article, we will learn how to do this. Please note, this requires root or sudo privileges.


How to Rebuild RPM Database in CentOS/RHEL

Here are the steps to rebuild RPM database in CentOS/RHEL.


1. Backup Existing Database

First, we need to backup existing RPM database. For this purpose, open terminal and run the following commands.

# mkdir /backups/
# tar -zcvf /backups/rpmdb-$(date +"%d%m%Y").tar.gz  /var/lib/rpm


2. Verify Integrity of Database

Next, we need to verify the integrity of master package metadata file /var/lib/rpm/Packages using rpm_verify command. It contains the files that requires rebuilding. But before doing that, we need to remove /var/lib/rpm/__db* files to prevent stale locks. These locks might have been created due to an incomplete transaction done in the past. You can do so using the following commands.

# rm -f /var/lib/rpm/__db*		
# /usr/lib/rpm/rpmdb_verify /var/lib/rpm/Packages

If you face errors, then you should dump the existing database and load a new database. Verify the integrity of the newly loaded packages with the following commands.

# cd /var/lib/rpm/
# mv Packages Packages.back
# /usr/lib/rpm/rpmdb_dump Packages.back | /usr/lib/rpm/rpmdb_load Packages
# /usr/lib/rpm/rpmdb_verify Packages


3. Check Database Headers

Check database headers by querying all installed packages using -q and -a flags. See if you get any error messages in the following command. We discard its output so that only error messages are directly displayed.

# rpm -qa >/dev/null


4. Rebuild RPM database

Finally, rebuild rpm database using the following command.

# rpm -vv --rebuilddb


5. (Alternative) Use DCRPM Tool

You can also use another tool DCRPM (Detect and Correct RPM) tool to identify and correct corrupted RPM Database. It is easy to install and run. You can call the dcrpm command directly without any arguments and it will detect and correct RPM database on your system. Here are the steps to install it.

# git clone https://github.com/facebookincubator/dcrpm.git
# cd dcrpm
# python setup.py install

Once it is installed, you can call it via the following command.

# dcrpm

In fact, you can also run dcrpm command as a cron job automatically to regularly fix any issues with your RPM database.

Once you have rebuilt your RPM database, you can try running the last RPM or YUM command that had failed earlier, to see if everything works as expected. It is important to maintain the integrity the RPM database to ensure smooth functioning of package managers in RHEL/CentOS/Fedora systems. Unfortunately, there is no automatic clean up of failed or incomplete transactions in RHEL/CentOS/Fedora, so we need to manually rebuild RPM database.

Also read:

How to Modify URL Without Reloading Page in JS
How to List All Virtual Environments in Python
How to Transfer MySQL Database from One Computer to Another
How to List Installed PHP Modules in Linux
Fix “Too Many Authentication Failures” SSH Error

Leave a Reply

Your email address will not be published. Required fields are marked *