How to Fix Broken Packages on Linux

Wondering How to Fix Broken Packages on Linux?

A package or package format is a file that contains computer programs and metadata that package managers need. The metadata includes information such as software’s name, description, vendor, and package dependencies. In other words, a package consists of a group of files that execute a task.

Sometimes, the installation process of a new package is terminated due to unexpected errors. When that happens, the resulting package is known as a broken package. Further, wrong configurations and missing dependencies can stop a package from installing and functioning; hence the broken package needs to be repaired.

A package manager is a group of software tools that automates the installation, configuration, upgrading, and removal of computer programs. Moreover, the package manager helps find and reinstall broken packages. In this guide, we will learn How to Fix Broken Packages on Linux.

Let’s dive right in!

Using APT or APT-GET Command.

Advanced package tool (APT) is a group of software tools distributed in a package called apt. In other words, APT is a frontend package manager used in Debian-based Linux distros, for instance, Ubuntu, Mint, and Debian.

We can use apt or apt-get commands to fix broken packages. Also, apt is more user-friendly and used in the command-line interface, while apt-get is more back-end-oriented and can be used in scripts. The apt is more recent, while apt-get is an older version of the apt command.

Here is how you can use the apt command to find and install missing dependencies:

  1. Go ahead and open Command Terminal by pressing Ctrl + Alt + T. Then, run the command below.
$ sudo apt --fix-missing update

Running this command ensures you have the newest version of the package. --fix-missing instructs APT to ignore the missing packages.

broken packages on linux
  1. After that, type the command below. It updates the package list and repository of your system.
$ sudo apt update
  1. Lastly, this command forces APT to install missing dependencies. That is to say, running this command repairs the broken packages. It uses the -f flag.
$ sudo apt install -f

If this method does not work in repairing the broken packages on Linux, try using dpkg as shown in the method below.

Using DPKG Command.

dpkg or Debian Package is software that is used to manage, install and remove .deb packages. dpkg is the main package manager for Debian systems. It is used as the base or backend for apt.

If a package fails during the configuration process, you can use the dpkg function to resolve the issue.

Here is how you can use the dpkg command to fix broken packages:

  1. Begin by opening Command Terminal by pressing Ctrl + Alt + T. Now, type this on your Terminal.
$ sudo dpkg --configure -a

Running this command reconfigures the incomplete packages. In other words, dpkg modifies and updates broken and partially installed packages so that the system can function properly.

configure
  1. If that does not resolve the issue, pipe grep with dpkg as shown below.
$ sudo dpkg -l | grep ^..r

This will display a list of packages marked as Required for reinstallation by dpkg.

  1. Next, remove the broken packages by typing the command below. It uses the -- remove flag.
$ sudo dpkg --remove --force-remove-reinstreq [package name]
  1. After that, use apt to clear the package cache and clean up the system.
$ sudo apt clean
  1. When you are done with all that, update the package repositories again using the command below.
$ sudo apt update
fix broken packages on linux

You can now test and see if you can install, remove and manage packages in your Linux system.

Fixing DPKG Lock Issue.

When trying to install a package, you may get an error ‘Could not get lock…’ for instance, ‘Could not get lock /var/lib/dpkg/lock – open (15: Resource temporarily unavailable)’. This message alerts you that the dpkg packet manager is not available. This means that dpkg has a lock and you can’t continue with the installation process.

In the same vein, sometimes when using apt or dpkg, you can get an error showing that the package is already being used. However, the error can occur even when no other applications or processes are using the package.

Meanwhile, when these errors occur, the lock files remain in place hence preventing the processes from continuing. To resolve the dpkg lock issue, you can manually delete the lock file that is preventing the packages from being installed or configured. 

Here is how you can resolve the DPKG Lock Issue:

  1. Go ahead and remove the lock file manually using the command below.
$ sudo rm /var/lib/apt/lists/lock 
broken packages on linux
  1. To be thorough, you can also remove the lock in the cache.
$ sudo rm /var/cache/apt/archives/lock 
lock

When you are done with all that, check if you can use APT and  DPKG packages again.

Using YUM and DNF Commands.

These commands are mostly used in Fedora/CentOS and RHEL Linux distros. YUM (Yellowdog Updater Modified) is a frontend package manager found in RPM (RedHat Package Manager) Linux systems. The yum command enables installing, configuring, removing, and managing packages in RPM repositories.  

DNF or Dandified YUM is the next generation of the already existing YUM package manager. It is meant to address poor performance, high memory usage that may be experienced when using YUM.

Here is how you can use yum and dnf commands to fix broken packages:

  1. Start by opening Command Terminal by pressing Ctrl + Alt + T. Then, use the command below to verify the packages on your system with the ones in the RPM database. It uses the -V flag for verification.
$ sudo rpm -Va 
broken packages on linux
  1. A list of installed packages will be displayed.
  1. Lastly, reinstall the broken packages using this command.
$ sudo dnf --refresh reinstall packagename

So, dnf will run, and if it finds broken dependencies, it will reinstall them.

We have come to the end of our guide. In conclusion, we have learned about multiple ways of How to Fix Broken Packages on Linux. If you have any questions or suggestions, let us know in the comment section below.

If this guide helped you, please share it. 🙂

Author

Leave a Reply

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