Raspberry Pi

How to fix broken packages on Raspberry Pi OS

The package managers in Raspberry Pi OS are extremely powerful which allow users to install, uninstall, update, upgrade or customize different packages that add more capabilities to your OS. However, that doesn’t mean that things will remain perfect every time because you may experience an error of broken packages during the installation which puts you in a situation to fix the broken packages in order to install the required application. Usually this type of problem occurs due to incomplete or unnecessary packages installation as well as improper package management that prevent users from installing the packages. At this moment, the user will be left with no choice to look for other solutions until the problem is fixed.

To make things easier for the Raspberry Pi users, this article is introduced to provide ways to fix such issues linked to broken packages on Raspberry Pi OS.

How to fix broken packages on Raspberry Pi OS

Here, you will see some methods which include terminal commands that will help you out in fixing the broken packages on Raspberry Pi OS. So, let’s discuss their solutions to fix these packages..

How to use apt command to fix broken packages on Raspberry Pi OS

The apt command is a terminal based package management tool used for installing, updating or upgrading the packages on Raspberry Pi OS. Along with these features, it also has the potential to fix those broken packages that prevent you from installing any package onto your Raspberry Pi OS.

If at any stage, you will encounter a broken packages error while installing a package onto your Raspberry Pi, then you should run the following command in the terminal which might fix the issue.

$ sudo apt --fix-missing update

Now, when you perform the following command, you will need to choose an option between “y” or “n” and you should press “y” key on the three requests which appears on your terminal window. This will prepare the installation of required packages for your Raspberry Pi.

Once it is done, you will need to provide a force for the installation of required packages which are ready to be upgraded and for that you will be required to execute the below given command in the terminal.

$ sudo apt install -f

Once it is done, you can repeat the installation process again to check whether your package installs without any error.

There is another way which you can fix the broken packages error, this recommendation will also appear on your terminal screen when you install an application. To encounter the error of “broken install”, you should execute the below mentioned command the terminal.

$ sudo apt --fix-broken install

Once you execute the above command, you can then go on to install the package again and hope it works.

If the problem still won’t be solved while trying everything possible then you should provide a full-upgrade of your Raspberry Pi OS as it might install the required packages needed to install an application and also it will remove the older packages on your Raspberry Pi OS that prevents your OS from installing a package.

$ sudo apt full-upgrade

How to configure dpkg to fix broken packages on Raspberry Pi OS

If you are still experiencing the broken packages error while attempting every apt command then there might be some issue in the setup process which is handled by dpkg. So, instead of selecting to go with the apt command, you will have to fix the problem through dpkg configuration. The following command will need to be executed in the terminal first which will force the dpkg to reconfigure the broken packages which have not yet been installed on the Raspberry Pi device..

$ sudo dpkg --configure -a

Thereafter, if the above command won’t solve the issue, then you can move one step further and execute the below command to check which package will need a reinstall.

$ sudo dpkg -l | grep ^..r

After executing the above command, you will be able to see the packages which dpkg marked as reinstall and then you can forcefully remove those broken packages that are causing problems in the installation process through the following command.

$ sudo dpkg --remove --force-remove-reinstreq [Name of package]

Once it is done, you can then use the apt command to cleanup the system.

$ sudo apt clean

After the cleanup, install the packages update through the update command and once it is completed you can then try and install the packages again and hopefully it will work fine.

$ sudo apt update

Conclusion

Installing packages on Raspberry Pi is quite a basic operation till you encounter the error of broken packages. You are suggested to stay with installing those programs which are useful as installing unnecessary packages would create a mess and problems that would be hard to figure out. Incase, if broken packages problem occurs, then you might need help in sorting them out and the above methods will be handy in that case which provides you with some commands to correct such errors.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.