Ubuntu and Debian both use packages to add to the system’s capabilities, allowing users to do more with their machines. However, as great as this package system is, things can still break. Whether it’s a bad install, a bad configuration, or if something externally modified the files after installation, packages can sometimes go wrong and not work anymore, making them ‘broken’. This can also affect apt-get, the built-in package manager, which can lead to you being unable to install any additional packages, updating them, or even removing any altogether.
In this tutorial, we will go over several methods for fixing your broken packages on both Ubuntu 16.04 as well as Debian 9. Since this problem can happen due to several reasons, several solutions exist, some of which may or may not work on your machine. Therefore it is best to see which specific package broke and why, which will help you decide which method to follow first.
Method 1: Using apt-get
More often than not, a broken package that broke after it was installed can be relatively easy to fix, thanks to a built-in tool inside of apt-get. Performing the following commands will try and fix any broken packages currently installed on your system:
sudo apt-get update --fix-missing
sudo apt-get install -f
(the -f option is short for fix-broken.)
Try and see if the first command fixes your problem before executing the second command. Give it a few moments to try and fix any errors that it may find. If it works, then try and use the package that was broken – it will likely be fixed now. In case it still isn’t fixed, proceed to the next solution below.
Method 2: Using dpkg and apt-get
This solution will help out if a software package broke while being installed. You can try using the previous method before continuing, as it may help. First, we clean apt-get and try using dpkg to configure any packages that need it:
sudo dpkg --configure -a sudo apt-get clean sudo apt-get update
After this, see if the package has been fixed. If it hasn’t, you may need to delete the lock files. Lock files are used to keep resources available for certain programs – it helps prevent other programs from accessing files at the same time. This can be done by simply deleting the lockfile list:
sudo rm /var/lib/apt/lists/lock
After this, try and run the above commands once more. From there, try and reinstall the package to see if it can now be successfully installed.
Method 3: Using dpkg for forceful removal
Sometimes, on rare occasions, the above commands won’t work, and a broken package won’t be fixable. In this case, you will need to remove the package manually and with force. This method requires that you already know which package is broken.
You can first try and remove the problematic package normally through apt-get by entering the following command:
sudo apt-get remove
It probably won’t be successful, but it is worth trying before removing it forcefully.
Depending on the errors you get when trying to remove the package normally, you might be able to identify the broken package using dpkg and grep:
dpkg -l | grep ^..r
This command lists all packages, then grep lists any results containing the ‘r’ flag in the third field, which means “reinst-required” (reinstall required). If the broken package shows up, you will be able to remove it.
Execute the following command to forcefully remove the package from your machine. Don’t forget to enter your password to authorize the deletion. Replace with the name of the broken package.
sudo dpkg --remove --force-remove-reinstreq
Once this is done, clean and update apt-get:
sudo apt-get clean sudo apt-get update
After that, try to install the package from scratch. It should install normally now.
These methods are the best ways to fix or remove a broken package from your system – but, it sometimes won’t be enough. In rare cases, the system won’t see the broken package at all, but it will still prevent your apt-get from functioning normally. In these cases, manually going and deleting files is usually the best solution. However, the process for deleting these files varies from package to package and can be risky. Therefore, try these solutions first before attempting to delete files from your system.
Hopefully, by following these methods you will have successfully fixed or removed any broken packages found on your system. Of course, you don’t need to fix broken packages yourself if you use one of our Managed Linux VPS services, in which case you can simply ask our expert Linux admins to do it for you. They’re available 24/7 and will help you fix your broken packages immediately.
PS. If you liked this post on how to fix broken packages on Ubuntu 16.04 and Debian 9, please share it with your friends on the social networks using the buttons below, or simply leave a comment. Thanks.