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.
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
- sudo apt-get update
- sudo apt-get upgrade
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
- When it’s done, it’s time to update the list of packages from the available sources.
- sudo apt update
- Now, use the -f flag in the next command to force install broken packages. Ubuntu will look for any broken package and force install it.
- sudo apt install -f
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]
- Perform cleanup of the package.
- sudo apt clean
- Update the packages’ list from the available sources
- sudo apt update
- sudo apt update –fix-missing update
- Run the below command to see which packages were installed.
- $ sudo apt-get install -f
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
- Remove unnecessary files and dependency data by using root access and the below command
- sudo apt-get autoremove
- After the removal, update the list of the installed packages
- sudo apt-get update
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’.
- Then, pick ‘Ubuntu, with Linux……. (Recovery Mode)’.
- When the recovery menu appears, select dpkg and press enter.
- It will ask to remount your filesystem in read/write mode. Select ‘Yes’.
- Once it’s done with its process, it may ask for an upgrade if available. Type yN to continue.
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
- Upon confirmation, press Y and hit the Enter key.
- After the installation, run it by clicking ‘Activities’, typing ‘synaptic package manager’, and opening the app.
- Checkmark the package that is broken.
- Then, click ‘Edit’ on the menu bar and pick ‘Fix broken packages’.
- Upon repairing the package, use any of the above-mentioned methods to remove the package.
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.
Conclusion
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.