Package managers like apt are one of the big selling points of Linux operating systems and Ubuntu – a vast curated collection of software that can do just about anything, available with a few keypresses. A vetted and (usually) reliable source of great software to meet any task.
However, when something can go wrong, eventually it will go wrong. A package may only partially install or conflict with something else in your system environment. Maybe an update is pushed out that breaks an installation.
When this happens, Ubuntu has a few tools to help fix the issue.
Read on to find out which tools to use and how to use them when you need to fix broken packages in Ubuntu.
apt and dpkg
apt and dpkg are commands for Ubuntu distributions, which handle package management.
apt is a full-blown package manager that can query and install from online package repositories.
dpkg installs, configures, and removes packages from local files (which may have been retrieved via apt)
Back-Up!
If your system has become unstable, make sure you back up before trying to resolve the issue, just in case you make things worse!
Remove No Longer Required Packages
First, make sure things are tidy by removing any packages that may have been installed as dependencies that are no longer required.
sudo apt autoremove
his may not work depending on the nature of the issue you are having with your packages, but it’s a good first step to run if it works
Update and Fix Missing Packages
Next, update currently installed packages while fixing any missing packages or dependencies:
sudo apt update --fix-missing
Any one of these steps might resolve your issue – so re-check after each step to see if you’re good to go.
Force Install Missing or Broken Packages
If this has failed to resolve the issue, you can force the install of any missing packages:
sudo apt install -f
Reconfigure Broken, Partially Configured, or Misconfigured Packages using dpkg
Some software requires additional configuration after installation, which is handled by dpkg. For example, when installing MySQL, further configuration prompts may be displayed to ask you to set up database users.
If this configuration failed or wasn’t completed for the package in question, the following will re-run that configuration process:
sudo dpkg --configure -a
Forcefully Remove Packages Requiring Reinstall
We’ve exhausted what apt can do for us. dpkg is a lower-level tool that can forcefully remove packages installed by apt and those that may have been manually installed.
List Packages Requiring Reinstall
Run the following to list the packages which are marked as requiring reinstallation:
sudo dpkg -l | grep ^..r
Force Remove Packages Requiring Reinstallation
If the list generated by the above command doesn’t include anything unexpected, run the following to force remove the packages in that list:
sudo dpkg --remove --force-remove --reinstreq
And then re-install any packages that are still required.
Force Remove a Specific Package With Configuration
When you uninstall a package, it still leaves its configuration files, so if it is re-installed, the old configuration will be retained.
You can remove a package, along with its configuration files entirely by running:
sudo apt purge <package-name>
Or if that fails, using dpkg by running:
sudo dpkg --purge <package-name>
Cleaning Up
Regardless of which method worked to fix your packages, it’s worth cleaning up and updating when you’re done to make sure everything is ship-shape going forward by running the following commands in succession:
sudo apt clean sudo apt update sudo apt upgrade sudo apt autoremove
dpkg Locks
dpkg will, on occasion, lock up and prevent you from performing any further package management. To release this lock, you just need to delete the lock files, which are sometimes erroneously left behind after package operations.
sudo rm /var/lib/apt/lists/lock sudo rm /var/cache/apt/archives/lock
Last Resorts
Linux is highly customizable, so it should generally be possible to restore the system to its working state – using your package manager or even by manually editing files if you are adventurous – however it might be more time consuming than it is worth to narrow down and properly resolve the issue.
If it looks like this is the case – restore from a backup! Because you should always have a backup, or three.