Using deborphan to Remove Unused Libraries in Debian

This article explains the role of deborphan in maintaining a Debian system by identifying and removing redundant shared libraries. Over time, package installations and removals can leave behind library files that are no longer required by any active software, wasting disk space and complicating system maintenance. The following sections cover what orphaned packages are, how deborphan detects them, how to safely delete them, and how the tool complements native package management utilities.

Understanding Orphaned Libraries in Debian

When software is installed on Debian, package managers like apt install both the requested program and its required shared libraries. If the main application is later removed, its supporting libraries can sometimes remain on the storage drive—especially when removals are performed using low-level tools such as dpkg or when dependencies are altered across distribution upgrades. An "orphaned" library is a package, typically located in the libs or oldlibs package sections, that has no other installed package depending on it.

How deborphan Operates

The deborphan utility scans the Debian package database located at /var/lib/dpkg/status. By default, it restricts its search to the library sections (libs and oldlibs) and cross-references them against the reverse dependencies of all currently installed packages.

If deborphan finds a library package that is not listed as a dependency, recommendation, or suggestion for any other installed software, it flags the package as orphaned. Because it evaluates the actual dependency tree rather than metadata tracking installation origins, it can detect unneeded files that standard package management logs might overlook.

Basic Usage and Identification

To use the tool, it must first be installed:

sudo apt update
sudo apt install deborphan

Running deborphan without any flags outputs a list of orphaned library packages:

deborphan

To see the disk space consumed by these packages, you can use the -s (show size) flag:

deborphan -s

Removing Orphaned Libraries

Once orphaned packages are identified, they can be safely purged from the system. A common approach is piping the output of deborphan directly into apt-get:

sudo apt-get purge $(deborphan)

Because removing one set of libraries may cause another parent library to become orphaned, the process often needs to be repeated until deborphan returns no output. Alternatively, Debian provides orphaner, a menu-driven front end included with the deborphan package, which allows administrators to interactively select which packages to keep or delete.

Managing False Positives with Keep Files

Some libraries are installed for manual compiling, dynamic runtime loading (such as plugins), or proprietary software not tracked by Debian’s package manager. Removing these can break software functionality.

deborphan allows users to whitelist specific packages to prevent them from appearing in orphan scans. To exclude a package permanently:

sudo deborphan -A package-name

This updates the /var/lib/deborphan/keep file. To view the current exclusion list, run:

deborphan --list-keep

deborphan vs. apt autoremove

Modern versions of apt include the apt autoremove command, which removes packages that were automatically installed to satisfy dependencies and are no longer needed. However, apt autoremove relies on the "automatically installed" flag assigned at the time of installation.

If a package was installed manually, installed via dpkg -i, or had its flags altered during an upgrade, apt autoremove will ignore it. deborphan fills this gap by directly inspecting the dependency relationships in the package database, ensuring that unused libraries are detected regardless of how they entered the system.