Installing and removing packages on Linux operating systems like Ubuntu is smooth and easy, but it gets complicated if you get errors due to broken packages.
Whether you can’t install or remove a package, this guide is for you and it will work on Ubuntu, Linux Mint, Kali, Fedora, CentOS, and other Linux distributions.
1 Causes of Broken Packages on Linux Operating Systems (Ubuntu and Others)
2 How To Fix Broken Packages In Ubuntu and Debian-Based Distributions?
3 Fixing Broken Packages on Fedora, CentOS, and More Red Hat Enterprises Linux (RHEL)
4 How To Avoid Broken Packages in Ubuntu and Debian-Based Distros?
Causes of Broken Packages on Linux Operating Systems (Ubuntu and Others)
The package installed partially Dependency or repository error A new update interrupting the installation Something in the package is conflicting with something else on your system The package was removed incorrectly
How To Fix Broken Packages In Ubuntu and Debian-Based Distributions?
- Clean the Package Reinstall Broken Packages Ubuntu (APT) Use DPKG to Force Remove the Broken Packages Solve the Lock Issue Auto Clean Broken Packages Fix Broken Files in Recovery Mode Repair Broken System Package Try Synaptic Package Manager
1. Clean the Package
If you know exactly which package is causing the trouble, fixing broken packages in Ubuntu is pretty easy. You can remove the package and clean the disk space.
Open the Terminal by pressing Ctrl + Alt + T on your keyboard and hitting the Enter key. Begin with cleaning the cache of the package with this command: sudo rm -rf /var/cache/apt/archives/[package-name] For example, to clean cache files of the Chrome package, try the command sudo rm -rf /var/cache/apt/archives/google-chrome-stable_current_amd64.deb Enter your sudo account password After this, free the storage space and then update the repository with these commands: sudo apt-get autoclean
Furthermore, another way to do this is by moving the cache files of the package to the temporary files. Then, remove the broken package using the Debian Package Manager (dkpg). (The following command is moving and removing RVM.)
sudo mv /var/lib/dpkg/info/rvm.* /tmp/ sudo dpkg –remove –force-remove-reinstreq rvm
2. Reinstall Broken Packages Ubuntu (APT)
Knowing which package is broken on your Linux system is not always the case. So, try this method to make your system check for missing updates, update the packages’ list, and then force install the broken packages.
Press Ctrl + Alt + T to open the Terminal. Find missing updates by using the command. It will take some time to check and fix updates. sudo apt –fix-missing update
3. Use DPKG to Force Remove the Broken Packages
If you face a problem using the APT package management system to remove troublesome packages, use the dpkg package management system instead.
This is useful if you need to force remove the packages. This is because sometimes the packages are installed in the root directory.
Open the terminal shell and use the below steps:
Force the dpkg system to reconfigure the packages that are unpacked but haven’t been configured. As I’m trying to configure all packages using the -a flag. sudo dpkg –configure -a Get the list of all the packages marked as required by dpkg. sudo dpkg -l | grep ^..r Now that you have the list of the required packages, delete all the broken packages. Replace [package-name] with the package you want to remove. sudo dpkg –remove –force-remove-reinstreq [package-name]
4. Solve the Lock Issue
The dpkg lock issue is not common but it stops you from performing any action. You can access the apt and dpkg package management systems.
Whenever you do, there is an error stating that the file is being used by another file or tool.
The reason for this problem is that during installation, some packages are stored in the filesystem as archive. They show errors when you try to remove them.
Usually, when you remove something from your Ubuntu system, all of its plugins and dependency files are also removed. But some files don’t due to the above-mentioned reason and cause issues.
Get root access and remove the troublesome files:
sudo rm /var/lib/apt/lists/lock sudo rm /var/cache/apt/archives/lock
5. Auto Clean Broken Packages
This method doesn’t remove the broken packages Ubuntu, instead, it cleans the temporary files left by packages upon removal.
Many packages leave some of their files on your filesystem when removed so that if the user decides to install the app again, the process is quicker. We’ll be cleaning those files.
Clean unused space and wipe cache files sudo apt-get autoclean sudo apt-get clean
6. Fix Broken Files in Recovery Mode
This is an effective method of repairing broken packages in Ubuntu which is not commonly used. It, technically, doesn’t fix broken files, but it performs repairs that also solve the issue of broken packages. Duplicate the below steps:
To enter recovery mode, turn on your Ubuntu system. After seeing the manufacturer’s logo, immediately press and hold the Shift key on your keyboard until some options appear on the black (BIOS) screen. Select ‘Advanced options for Ubuntu’.
7. Repair Broken System Package
Software Updater is responsible for keeping your Ubuntu system updated. Any dependency or system error can cause broken system packages, which will stop the software updater application from working properly.
To get rid of this error, wipe the system cache and clean the filesystem.
sudo apt-get clean sudo apt-get install -f
8. Try Synaptic Package Manager
It’s a GUI-based package manager that is easy to use and can install, remove, update, and fix broken packages Ubuntu.
For installing Synaptic Package Manager, use these commands that require root access:
sudo apt-get update sudo apt-get install synaptic
Fixing Broken Packages on Fedora, CentOS, and More Red Hat Enterprises Linux (RHEL)
Usually, yum and dnf do a good job of keeping the packages clean on RHEL operating systems, but some apps cause broken packages anyway.
For rpm-based systems, use the following command. sudo rpm -Va This will give you a list of installed files in the rpm database. After this, you can check for any suspicious packages and apply the below command. sudo dnf –refresh reinstall [package-name] The above command will set the metadata as expired and may help you find broken dependencies with that package and will suggest using the –skip-broken flag.
How To Avoid Broken Packages in Ubuntu and Debian-Based Distros?
Fixing broken packages in Ubuntu takes time so it’s better to prevent this in the first place by cleaning and removing the repository files of the programs that you have removed.
Open ‘Software and Updates’. Switch to the ‘Other Software’ tab. It will have the repository list. Select any to remove.
Finding and repairing or removing broken packages on Ubuntu is a bit complicated, but with the various methods above, you can select the right one for you and keep your Ubuntu system clean and without errors.