Package managers on Linux allow you to control the installation and removal of packages. In addition, package managers also help you find broken packages on your system and reinstall them to fix various issues associated with Linux packages.

If you are unaware of which commands to use to find and fix broken packages in Linux, then this guide is for you. We will briefly discuss what broken packages are, how you can check if your system contains broken packages, and how to reinstall them properly to fix the error.

What Are Broken Packages in Linux?

When you install a new package in Linux, your system's package manager is in charge of the whole installation process. These package managers have built-in methods to handle exceptions and errors. But sometimes, in case of unexpected issues, the installation halts and the complete package isn't installed. Such packages are called broken packages.

Package managers like APT do not allow further installation if it finds a broken package on the system. In such a situation, repairing the broken package is the only option.

How to Find and Fix Broken Packages in Linux

Every package manager handles different types of packages. For example, DNF and YUM work with the Red Hat Package Manager (RPM) to download and install RPM packages. Similarly, APT acts as a frontend wrapper for the base dpkg software on Debian-based distributions.

Reinstalling Broken Packages on Ubuntu and Debian

APT is the default package manager that comes preinstalled on every Debian-based distribution. Apart from APT, Debian and Ubuntu users can download and install packages manually using dpkg as well.

The steps mentioned below will also work if you want to fix broken packages on Kali Linux, since after all, Kali is a Debian-based distro.

To fix broken packages on Debian-based distributions using APT:

  1. Open the terminal by pressing Ctrl + Alt + T on your keyboard and enter:
            sudo apt --fix-missing update
        
  2. Update your system's package list from the available sources:
            sudo apt update
        
  3. Now, force the installation of the broken packages using the -f flag. APT will automatically search for broken packages on your system and reinstall them from the official repository.
            sudo apt install -f
        

If the aforementioned steps do not work for you, you can try to solve the issue using dpkg.

  1. Force dpkg to reconfigure all the pending packages that are already unpacked but need to undergo configuration. The -a flag in the following command stands for All:
            sudo dpkg --configure -a
        
  2. Pipe grep with dpkg to get a list of all the packages marked as Required:
            sudo dpkg -l | grep ^..r
        
  3. Use the --remove flag to delete all the broken packages:
            sudo dpkg --remove --force-remove-reinstreq
        
  4. Clean up the package cache and install scripts using apt clean:
            sudo apt clean
        
  5. Now, update your system's package lists using the following command:
            sudo apt update
        

Fix Broken Packages on Fedora/CentOS and RHEL

Although YUM and DNF are great when it comes to managing broken packages automatically, sometimes problems do arise as there are thousands of packages installed on a Linux system. In such situations, you can use RPM, the base package manager for Fedora and CentOS, to fix such issues quickly.

  1. Verify all the packages on your system using the -V flag:
            sudo rpm -Va
        
  2. You will see a long list containing all the installed packages on your system. Reinstall the package that you think might be causing the broken package issue with:
            sudo dnf --refresh reinstall packagename
        

The above steps are highly inconvenient—identifying which package is causing the problem from a list of hundreds is tiresome. Although RPM is a powerful package manager and you will rarely run into such issues, knowing how to fix these problems is still important in case you bump into a similar situation in the near future.

Managing Packages on Linux Distributions

Package managers on Linux are capable of handling most of the issues including failed installations. But sometimes, various problems occur that you can only solve intuitively. The solution to fixing broken packages comprises several steps—identifying the broken package, reinstalling it, and updating the system's package list.

There are countless Linux distributions available that are worth trying, but deep down, each one of them has a similar foundation.

Desktop environments set each distribution apart by providing a unique user experience. Choosing an ideal desktop environment that suits your taste should be your priority if you have finally decided to go ahead with Linux.