How to fix “E: Unable to correct problems, you have held broken packages” in Ubuntu Linux Systems. When you get error messages such as “the following packages have unmet dependencies” you can easily fix it by fixing broken packages in Ubuntu command line.
Here is how to fix the above errors:
Broken packages can result from an interrupted installations/upgrades, dependency conflicts, using incompatible repositories, incomplete installations or use of corrupted package for downloads and/or manual removal of files while they are required by other packages on the system.
Identify Broken Packages
Run the following commands to identify if your system has broken packages or not. The commands will update package lists on your system, check for broken packages on your system and list packages with issues on your system
sudo apt update
sudo apt --fix-broken check
sudo apt check
sudo dpkg --audit
Use APT to Automatically Fix Broken Packages
Run the command below to automatically fix broken packages. The following command tries to fix dependency problems, install missing packages, and complete all the unfinished operations that may arise broken packages errors.
sudo apt --fix-broken install
Secondly, run the following command to perform a full system upgrade which installs, removes, or upgrades packages as necessary and works on broken dependencies.
sudo apt full-upgrade
Remove Unused Packages
Run the commands below to remove orphaned packages:
sudo apt autoremove
sudo apt autoremove --purge
Run the commands below to clean package cache:
sudo apt clean
sudo apt autoclean
Run the commands below to remove residual config:
dpkg -l | grep '^rc'
sudo apt purge $(dpkg -l | grep '^rc' | awk '{print $2}')
Lastly, you must clean and repair the Package Database to prevent issues caused by incomplete downloads. It is noteworthy to mention that an interrupted installations or upgrades can result in broken packages.
To do so, clean the Cache to remove all downloaded package files and also remove all obsolete files:
sudo apt clean
sudo apt autoclean
Next you will have to remove all kinds of unused Dependencies, unused packages and libraries that were automatically installed and are no longer required and cause conflicts. Run the following command to remove the unused dependencies:
sudo apt autoremove
sudo dpkg --configure -a
Remove Lock Files: Sometimes the apt is locked by another process which can create errors, to resolve this, check for running apt processes and remove lock files:
ps aux | grep -E 'apt|dpkg'
sudo rm /var/lib/dpkg/lock-frontend
sudo rm /var/lib/dpkg/lock
sudo rm /var/cache/apt/archives/lock
sudo dpkg --configure -a
Update and Upgrade
Before you restart the system to apply changes, it is advised to update and upgrade the system. To do so, run the following commands:
sudo apt update
sudo apt upgrade
sudo apt full-upgrade
Remember, package errors are common and can be resolved with apt’s built-in tools. My advice is to regularly update your system which will fetch all the necessary security and bug fixes.
Also, it is very important to always use official, stable repositories for installations and upgrade.
