Steve October 26, 2020
how-to-fix-broken-packages-in-linux

Linux package managers, like Apt and DNF, are extremely powerful and intuitive, but that doesn’t mean things can’t go wrong. Occasionally, a package install goes wrong, and you’re left to pick up the pieces. Package managers have the ability to fix broken packages and skip broken updates to get your system working again and avoid troubles in the future. This article covers how to fix broken packages in Linux.

These tips and tricks should help you get unstuck in most instances. They’re all fairly universal, but every situation is different, so keep that in mind when trying to debug your own situation.

Fixing Broken Packages in Ubuntu

Apt has a couple of flags you can use to fix missing dependencies or packages that broke for one reason or another during install. A common use here would be installing a third-party .deb and finding that it had dependencies you didn’t know about. Those dependencies probably won’t be pulled in on their own, and dpkg will complain that the package is missing in them. In any case, you can try the following steps.

Ubuntu Apt Fix Missing

First, run an update to make sure there aren’t newer versions of the required packages.

sudo apt update --fix-missing

Next, you can try forcing Apt to look for and correct any missing dependencies or broken packages. This will actually install any missing packages and repair existing installs.

Another place where error can pop up in package installations is the configuration process. Behind the scenes, dpkg is taking care of this part, not Apt, so it would stand to reason that when a package fails during configuration, dpkg is the tool to turn to in order to fix it.

Ubuntu DPKG Reconfigure

Start by trying to force dpkg to reconfigure any broken or partially configured packages.

If that doesn’t solve the problem, you can take a more forceful approach. Start by listing any packages that dpkg marked as requiring a reinstall.

You should see the problematic package(s) there. As long as there isn’t anything that doesn’t seem to belong there, you can force-remove the broken packages.

sudo dpkg --remove --force-remove --reinstreq

When dpkg is done, try to clean up with Apt.

sudo apt clean
sudo apt update

With any luck, that’ll get you back to where you started. It won’t get you the broken packages that you tried to install, but at least Apt will be functional again.

Permanent DPKG Lock

There’s a less common issue with ridiculous dpkg locks preventing you from doing anything. Every time you try to use Apt or dpkg, you get an error saying another application already is … when it isn’t.

It’s actually easy to delete the lock file preventing you from using Apt and getting back to doing what you need to. Sometimes these lock files remain in place after an error occurs in an install, derailing the process and preventing the file from being removed automatically. In this case, you’ll need to do it yourself.

sudo rm /var/lib/apt/lists/lock

For good measure, delete the lock in cache.

sudo rm /var/cache/apt/archives/lock

Fixing Broken Packages in Fedora/CentOS/RHEL

Fixing broken packages in Fedora/CentOS/RHEL is less common, as yum and dnf do really great work in making sure packages are installed correctly. However, sometimes things get mixed up in package use. The command to do that on rpm-based systems is:

Fix Broken Packages Linux Rpm Va

The -V option is for verify, meaning it will go through and compare information on the installed files with the information stored in the rpm database. This is slightly unhelpful, as it will usually give you a long list of files, but it can give you somewhere to start if you’re having issues with a particular application. You can run a dnf reinstall on any packages you see in that list that might be giving you trouble.

sudo dnf --refresh reinstall PACKAGE_NAME

That will set all metadata as expired, so it will crawl through every repository you have enabled and look for a new version of that package. If you find that there are broken dependencies with that package, DNF will probably complain and tell you to use the --skip-broken flag. This can also mean that you don’t have a particular repository enabled to pull in a dependency, so you may have to do some research there.

Conclusion

Hopefully, one of these fixes worked for you, and you’re back working on your Linux system normally. Remember that the best way to handle a totally out-of-hand situation is to try to return to the way it was before. Don’t try to power through and add more things to solve a breakage by adding more things unless you know exactly what you’re doing. Chances are you’ll end up with a tangled mess of broken things that’ll be harder to sort out.

You may also need to clean up your Linux system to get rid of unnecessary and broken packages. If the cause is due to an old PC not able to support newer software, you can also make use of these three ways to put your old PC to good use.

Related:

John Perkins
John Perkins

John is a young technical professional with a passion for educating users on the best ways to use their technology. He holds technical certifications covering topics ranging from computer hardware to cybersecurity to Linux system administration.

Is this article useful?

Read More