HomeTutorialsHow to Remove Broken Packages in Ubuntu Linux

How to Remove Broken Packages in Ubuntu Linux

As a professional Linux user, I need to install, test, and remove tons of packages on my Ubuntu, Fedora, and Arch systems. Installing and removing packages are straightforward, but somehow, if you face a broken package issue on your Ubuntu machine, that is havoc. If you can not remove broken packages from your Ubuntu machine, you will probably face issues while installing it later. Moreover, broken packages also occupy some space in the Ubuntu file system.

Remove Broken Packages in Ubuntu Linux


In Ubuntu, the broken package issue might occur due to dependency error, repository error, lock filesystem, or other issues. Sometimes, you may also need to perform commands as a root user to remove the broken packages from your Ubuntu system.

Normally, the packages are installed inside the root filesystem; when you have the root power, and you do any mismatches with any of those files or directories where the tool is installed, it may get broken.

Another way to be a broken package is to remove the package repository from the system. You can use aptitude commands or the dpkg commands to remove applications from your system, but they won’t remove the broken packages completely. In this post, we will see how to remove broken packages from a Ubuntu system.

1. Clean Specific Package on Ubuntu


When you know exactly which package has a broken issue on your Ubuntu machine, you can use this method to remove that package and clean the disk space. First, you need to clean and remove the cache files of that application. 

For instance, you can run the following commands to clean the cache files for the NodeJS broken package on your Ubuntu system.

sudo rm -rf /var/cache/apt/archives/nodejs_0.10.28-1chl1~trusty1_amd64.deb

Now run the auto clean command to free the disk space, then update and upgrade the system repository.

sudo apt-get autoclean
sudo apt-get update
sudo apt-get upgrade

nodejs_ rm rf on ubuntu

You can also use the move command to move your application cache files to the temporary files. You can then remove your broken package through the Debian package manager (dpkg) tool. For instance, the following commands will remove the broken Ruby version manager (RVM) on a Ubuntu system.

sudo mv /var/lib/dpkg/info/rvm.* /tmp/
sudo dpkg --remove --force-remove-reinstreq rvm

2. Solve Lock Issue


Sometimes, after installing a package on Ubuntu or Debian distributions, the package stores some data inside the filesystem as an archive. So when you try to remove or delete that file, you get an error saying that the file is being used by another tool or another program. 

Normally, when you remove a tool from your machine, the dependency files and other plugins should get wiped away too. But, what to do if some packages choose not no leave your computer! Please remove the locked Debian package files by running the following remove commands with root access.

sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock

3. Force Remove Broken Packages


This method will guide you to remove broken packages on a Ubuntu system forcefully. Force removals are required because, when installing some packages on the system, it happens to install inside the root directory. However, you can run the following dpkg command given below to remove any broken packages from your system. Please be mindful to replace the <packagename> with your broken package name.

sudo dpkg --remove --force-remove-reinstreq <packagename>
sudo apt-get update

You can also generate a list of removable packages on your Ubuntu system by running the following GREP command on the terminal shell. Then remove them by executing the next dpkg command. 

sudo dpkg -l | grep ^..r
sudo dpkg --remove --force-remove --reinstreq

Here is another thing you might need to know, after removing a broken package on a Ubuntu system, please run the aptitude commands to update the system repository. 

sudo apt update --fix-missing update

Now, run the following aptitude command with an F flag to see which packages were installed automatically and are no longer required. Later, you can take action for those applications as well.

$ sudo apt-get install -f

install flag F

4. Auto Clean Broken Packages in Ubuntu


The auto clean and auto-remove commands do not actually remove broken packages from the system. Still, it does help free up the spaces that were used to be occupied by a prior installed/removed application.

Often, developers design an application so that the app files still stay inside your filesystem even after removing the application from the system. In some way, these are actually helpful when you re-install the same application in the same method.

However, run the following clean and auto-clean commands on the terminal shell to clean the unused spaces on your system by wiping up the cache files. 

sudo apt-get autoclean
sudo apt-get clean

Now, run the following command to auto-remove command with root access to remove the unnecessary application and dependency data. 

sudo apt-get autoremove

sudo apt-get autoremove

After cleaning up the system, please run a repository update command to run your Pc smoothly.

sudo apt-get update

5. Repair Broken Packages via Recovery Mode


In this method, we will show the most unused method of removing a broken package on a Ubuntu system. We will use the Ubuntu recovery mode to repair the broken packages. So, technically this method doesn’t guide you to remove the broken applications, but it does some sort of tasks that also repair the broken apps. When the dpkg tool can’t repair the package, and you can then remove it from your system. 

First, fire up your Ubuntu system and press the Shift button to enter into recovery mode. If the Shift key does not work for you, you may need to find the recovery button combination for your PC/Laptop model by performing a quick google search. 

After restarting the PC, choose the Advanced options for Ubuntu from the boot menu, then when the recovery mode opens, select the ‘Repair broken package’ menu from the drop-down list. 

repair brpken packages from recovery mode on ubuntu

Now, you will need to press the Yes button for mounting your system inside the filesystem. This will remount the filesystem in read/write mode, and the other file systems will be mounted in the /etc/fstab directory.

build cache and load files

6. Fix Broken System Package


Normally, in Ubuntu machine, we use the Software Updater application to update the repository, cache files, and other package data. But sometimes, due to some dependency error or system error, you might not be able to execute the software updater application. 

To get rid of that issue, you can clear the system cache and wipe up a little of your broken filesystem. Run the following commands with root access on the terminal shell to clean up the cache files and install the missing dependencies.

clean cache 
sudo apt-get clean
sudo apt-get install -f

software updater the package system is broken

7. Get Synaptic Package Manager


The Synaptic package manager is the most used GUI-based package manager tool for Ubuntu and other Debina distributions. It has features for both installing, removing, updating, and fixing broken packages. If you don’t have the Synaptic tool installed on your machine, please run the following commands on the terminal shell with root privilege to install it.

sudo apt-get update
sudo apt-get install synaptic

install synaptic on ubuntu

When the Synaptic package manager installing finishes, open it and click on the ‘Edit’ button from the top tray. Now, browse the ‘Fix Broken Packages’ option. Usually, to update, remove or fix any packages through the Synaptic tool, you need to mark that application first from the ‘All package’ list. 

fix broken packages on ubuntu via synoptic

After fixing the package, you can now decide and choose any of the above-mentioned methods to remove that application from your Ubuntu system. 

Extra Tips: Recovery Mode for Removing Broken Package


Here is an extra suggestion that I follow on my Ubuntu system now and then ensure that the system packages don’t get broken. You need to clean and remove the repository files when you’re not using that software anymore; otherwise, every time, you will see an error while you update the repository. 

To remove unused repositories from your Ubuntu machine, open the ‘Software & Updates‘ tool and click on the ‘Other Software‘ tab. In the ‘Other Software’ tab, you will see the repository list currently installed on your system. To remove any repository, checkmark the repository name and finally click on the ‘Remove’ button from the bottom tray.

other software repo

After removing any repositories, please run a quick aptitude update command to update the system. 

If you can’t find any repository for a specific package, you can always use the whereis command to know where the files are located. 

where is command

Final Words


All the above-mentioned methods are useful for those who are struggling simultaneously with both single and multiple broken package issues on their Ubuntu system. Sometimes, corrupt systems cause errors that require fixing without re-installing the entire system. In the entire post, I’ve described several methods of removing broken packages on a Ubuntu system.

I hope this post has been informative for you; please share this post with your friends and the Linux community. You can also write down your opinions in the comment section regarding this post.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.

LEAVE A REPLY

Please enter your comment!
Please enter your name here


You May Like It!

Trending Now