How to Fix "Sub-process /usr/bin/dpkg Returned an Error Code" in Linux
The "Sub-process /usr/bin/dpkg returned an error code" message appears when dpkg fails during package installation or removal. The error code indicates the type of failure. Here is how to fix the most common causes.
Fix 1: Identify the Error Code
The error message includes a number like "error code (1)" or "error code (2)". Error code 1 typically means a problem with the package scripts. Error code 2 means a fatal error. Look above the error message in the terminal output to find which package caused the problem and what the specific error was.
Fix 2: Run dpkg --configure -a
When an installation is interrupted, packages may be left in an inconsistent state. Run sudo dpkg --configure -a to reconfigure all partially installed packages. This command processes packages that were left unconfigured and often resolves the error code problem.
Fix 3: Fix Failing Post-Installation Scripts
A failing post-installation script is a common cause of error code 1. Find the script in /var/lib/dpkg/info/packagename.postinst. Read it to understand what is failing. You may need to modify the script or install missing dependencies. If the script is unnecessary, you can rename it to bypass it, but this is not recommended.
Fix 4: Force Remove the Problematic Package
If a package consistently fails, force remove it with sudo dpkg --remove --force-remove-reinstreq packagename. Then clean up with sudo apt --fix-broken install. After removal, try reinstalling the package with sudo apt install packagename.