1. Overview

Software packages in Linux can become broken when they get corrupted during installation or are installed incorrectly. Various factors can break a package, broadly categorized into hardware, software, and user-related issues.

In this tutorial, we’ll explore broken packages, how to locate them, and how to uninstall them to correct the errors.

2. Handling Broken Packages Using apt-get

Let’s explore using the apt-get command to locate, fix, and uninstall broken packages.

2.1. Locating Broken Packages

Now, we can examine the apt-get command with its option for checking broken packages in Linux:

$ sudo apt-get check
Reading package lists... Done
...
 vlc : Depends: vlc-bin (= 3.0.21-1) but it is not installed
E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).

As shown above, the command outputs show a broken package, vlc, with unmet dependency vlc-bin. In addition, the result provides a solution to the issue by recommending the apt –fix-broken install command.

In short, the sudo apt-get check command checks for broken dependencies in installed packages on Debian-based Linux System.

2.2. Fixing Broken Packages

With the knowledge of the broken package, we can fix or uninstall the affected package using the apt-get command.

The sudo apt-get install -f command is used to fix issues with broken packages and it performs the same function as apt –fix-broken install:

$ sudo apt-get install -f
Reading package lists... Done
...
The following additional packages will be installed:
  vlc-bin
The following NEW packages will be installed:
  vlc-bin
0 upgraded, 1 newly installed, 0 to remove and 17 not upgraded.
...
Get:1 http://kali.download/kali kali-rolling/main amd64 vlc-bin amd64 3.0.21-1 [128 kB]
Fetched 128 kB in 1s (97.3 kB/s)
...
Processing triggers for kali-menu (2023.4.7) ...
Processing triggers for man-db (2.12.1-2) ...

Now, we can see that our command installed vlc-bin, the unmet dependency noted in our previous result. Thereby, fixing the broken package.

Let’s double-check to confirm if the broken package issue is resolved:

$ sudo apt-get check
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done

Consequently, the output from checking for broken packages on our Linux system shows no issues whatsoever with any of our packages.

2.3. Uninstalling Broken Packages

Uninstalling broken packages might be the best option in some cases when there is no longer a need for such packages. So, let’s proceed to uninstall the broken package we identified earlier:

$ sudo apt-get remove --purge vlc
Reading package lists... Done
...
The following packages were automatically installed and are no longer required:
  amass-common libvncclient1 vlc-bin vlc-l10n vlc-plugin-access-extra vlc-plugin-notify vlc-plugin-qt vlc-plugin-samba vlc-plugin-skins2
...
The following packages will be REMOVED:
  vlc*
0 upgraded, 0 newly installed, 1 to remove and 17 not upgraded.
2 not fully installed or removed.
... 
(Reading database ... 508493 files and directories currently installed.)
Removing vlc (3.0.21-1) ...
Processing triggers for gnome-menus (3.36.0-1.1+b2) ...
Processing triggers for desktop-file-utils (0.27-2) ...

From the output above, we can see that vlc has been successfully removed and the few packages that are no longer needed were listed. Moreover, the uninstalling process recommends removing them, so we use sudo apt-get autoremove to operate:

$ sudo apt-get autoremove
Reading package lists... Done
...
Removing vlc-plugin-access-extra:amd64 (3.0.21-1) ...
..
Processing triggers for libvlc-bin:amd64 (3.0.21-1) ...
Processing triggers for wordlists (2023.2.0) ...
Processing triggers for kali-menu (2023.4.7) ...

All the packages that were not needed were removed from the package cache. Subsequently, it’s a good practice to clear out the local repository of retrieved package files.

Let’s proceed to clear the local repository:

$ sudo apt-get clean

The command above doesn’t output anything when it runs comfortably. However, the command ran successfully.

3. Handling Broken Packages Using dpkg

dpkg (Debian Package) is the low-level package manager for Debian-based systems. It’s used to install, remove, and provide information about .deb packages.

3.1. Locating Broken Packages

Let’s use the dpkg command and the appropriate option to locate broken packages:

$ sudo dpkg --audit 
The following packages are in a mess due to serious problems during
installation.  They must be reinstalled for them (and any packages
that depend on them) to function properly:
 7zip                 7-Zip file archiver with a high compression ratio
 accountsservice      query and manipulate user account information

The command we executed above reports packages that are in an inconsistent or broken state. From the result, we can see that the command identified 7zip ad accountsservice as packages in mess and in need of reinstallation.

3.2. Fixing the Broken Packages

Next, we can now proceed to fix the identified packages using sudo dpkg –configure -a to fix partially installed packages. Let’s proceed to use the command to reconfigure all partially installed packages:

$ sudo dpkg --configure -a

We had no result from the execution of the command, as shown above.

3.3. Uninstall Broken Packages

Here, let’s attempt to remove the identified broken packages, 7zip, and accountsservice, using the dpkg command:

$ sudo dpkg --purge 7zip accountsservice
dpkg: dependency problems prevent removal of 7zip:
 file-roller depends on 7zip.
...
 gnome-control-center depends on accountsservice.
 gdm3 depends on accountsservice (>= 0.6.35).

dpkg: error processing package accountsservice (--purge):
 dependency problems - not removing
Errors were encountered while processing:
 7zip
 accountsservice

As shown above, dependencies can prevent packages from being uninstalled; however, the process can be forced.

Let’s force the removal of the packages:

$ sudo dpkg --remove --force-remove-reinstreq --ignore-depends=7zip --ignore-depends=accountsservice 7zip accountsservice
...
dpkg: warning: overriding problem because --force enabled:
dpkg: warning: package is in a very bad inconsistent state; you should
 reinstall it before attempting a removal
...
Processing triggers for dbus (1.14.10-4+b1) ...

As shown above, the code executed above ignores dependencies of other packages that could prevent the uninstalling process, which is why we add –ignore-depends options to the code.

4. Handling Broken Packages Using rpm

The rpm command is native to Fedora, CentOS, and RHEL (Red Hat Enterprise Linux) Linux distributions. So, let’s focus on the method of locating broken packages that applies to all these distros.

4.1. Identifying Broken Packages With Missing Files Using rpm

Now, we can proceed to use the RPM (Red Hat Package Manager) to locate and uninstall broken packages on the above-listed distros:

$ sudo rpm -Va | grep '^missing'
missing     /run/gluster/metrics

The command sudo rpm -Va | grep ‘^missing’ verifies all installed RPM packages on the system and filters the output to show only the files that are missing.

Furthermore, let’s identify the package that owns the file by using the /run/gluster/metrics pathname:

$ sudo rpm -qf /run/gluster/metrics
glusterfs-11.1-3.fc40.x86_64

As shown above, the command determines which RPM package owns the specified file. The result showed that the file belongs to glusterfs-11.1-3.fc40.x86_64.

4.2. Uninstalling Broken Packages With rpm

After identifying the broken package, let’s uninstall it using rpm:

$ sudo rpm -e --nodeps glusterfs-11.1-3.fc40.x86_64
warning: file /run/gluster/metrics: remove failed: No such file or directory

With the command above, we forcefully remove glusterfs-11.1-3.fc40.x86_64. In addition, the warning message calls our attention to the missing file that the command wanted to remove along with the package.

Lastly, it’s a good practice to rebuild the database after uninstalling packages, so let’s proceed to rebuild our database:

$ sudo rpm --rebuilddb

Consequently, the command outputs nothing, indicating that it was completed successfully without error.

5. Handling Packages Using yum

As in the previous section, yum (Yellowdog Updater, Modified) is native to the mentioned distros. So, let’s perform the task of locating, fixing, and uninstalling broken packages.

5.1. Locating Broken Packages

Now, let’s proceed to use the yum command:

$ sudo yum check 
glusterfs-fuse-11.1-3.fc40.x86_64 has missing requires of glusterfs = 11.1-3.fc40
Error: Check discovered 1 problem(s)

As shown above, the result shows that glusterfs-fuse-11.1-3.fc40.x86_64 has a problem. So, it’s time we move on to propose a solution to the problem.

Moreover, we can also proceed to check for broken dependencies:

$ sudo rpm -Va --nofiles --nodigest
Unsatisfied dependencies for glusterfs-fuse-11.1-3.fc40.x86_64:
    glusterfs = 11.1-3.fc40 is needed by (installed) glusterfs-fuse-11.1-3.fc40.x86_64

This result gave us the same information as the output from sudo yum check. Here we can see the same package has been reported again.

5.2. Fixing the Packages

Invariably, it’s very important to know how to fix problems. Let’s proceed to fix the problem with the package:

$ sudo yum reinstall glusterfs-fuse -y
Last metadata expiration check: 0:01:57 ago on Wed 03 Jul 2024 09:30:27 PM WAT.
Dependencies resolved.
======================================================================================
 Package                    Architecture       Version           Repository      Size
======================================================================================
Reinstalling:
 glusterfs-fuse             x86_64             11.1-3.fc40       fedora          136 k
Installing dependencies:
 glusterfs                  x86_64             11.1-3.fc40       fedora          585 k

Transaction Summary
======================================================================================
Install  1 Package

Total download size: 721 k
Installed size: 3.0 M
Downloading Packages:
(1/2): glusterfs-fuse-11.1-3.fc40.x86_64.rpm           9.5 kB/s | 136 kB     00:14
(2/2): glusterfs-11.1-3.fc40.x86_64.rpm                25 kB/s | 585 kB     00:22
-------------------------------------------------------
Total                                                  18 kB/s | 721 kB     00:40
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                              1/1 
  Running scriptlet: glusterfs-11.1-3.fc40.x86_64                 1/3 
  Installing       : glusterfs-11.1-3.fc40.x86_64                 1/3 
  Running scriptlet: glusterfs-11.1-3.fc40.x86_64                 1/3 
  Reinstalling     : glusterfs-fuse-11.1-3.fc40.x86_64            2/3 
  Cleanup          : glusterfs-fuse-11.1-3.fc40.x86_64            3/3 
  Running scriptlet: glusterfs-fuse-11.1-3.fc40.x86_64            3/3 
Installed:
  glusterfs-11.1-3.fc40.x86_64
Reinstalled:
  glusterfs-fuse-11.1-3.fc40.x86_64

As shown above, the results show that the package and its dependencies were reinstalled. This fixed the issue reported by the sudo yum check.

5.3. Uninstalling the Package

Sometimes, uninstalling the package might be the most viable option. In that case, let’s examine how to use the yum command to uninstall the identified package:

$ sudo yum remove glusterfs-fuse
[sudo] password for hackterminux: 
Waiting for process with pid 3978 to finish.
Dependencies resolved.
======================================================================================
 Package                         Architecture    Version         Repository       Size
======================================================================================
Removing:
 glusterfs-fuse                  x86_64          11.1-3.fc40     @fedora          524 k
Removing dependent packages:
 gnome-boxes

Now, by executing the command, we successfully uninstalled the package and its dependency.

However, it’s best practice to clean the yum cache after carrying out this process:

$ sudo yum clean all
17 files removed

As shown above, the results show that 17 files were removed from the yum cache. Moreover, this helps resolve future issues that could come up.

6. Handling Packages Using dnf

dnf (Dandified Yum) is the next-generation version of yum and is the default package manager for Fedora and other Red Hat-based distributions. As such, there is a close similarity between how to use yum and dnf for resolving broken package issues.

Moreover, all the commands we ran previously for yum are the same for dnf and perform the same functions:

  • sudo dnf check: used for checking broken packages and dependencies
  • sudo dnf reinstall : used for reinstalling the identified broken package
  • sudo dnf remove : used to remove or uninstall the package
  • sudo dnf clean all: to clean the dnf cache and remove redundant files

So, using dnf follows the same logic as using yum.

7. Conclusion

As a Linux administrator, it’s important to understand how to locate broken packages and cleanly remove them from the Linux system.

In this article, we delved into understanding how to locate and uninstall broken packages using package managers such as apt, dpkg, rpm, yum, and dnf.