
Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: December 10, 2023
In the world of package management, installing a package often brings with it a large number of dependencies. When we no longer need the original package and remove it, its orphaned dependencies are often left behind.
In this tutorial, we’ll look at how to remove these orphaned packages using various package managers, including apt, dnf, zypper, pacman, and emerge.
The upcoming installation and removal of packages illustrate the problem of installing numerous dependencies that may become unnecessary after uninstallation. Our choice of packages is purely arbitrary and is for testing purposes only:
# apt, tested on Ubuntu 22.04.3 LTS
$ sudo apt update
$ sudo apt install build-essential
$ sudo apt remove build-essential
# dnf, tested on Fedora 38 Live
$ sudo dnf group install "Development Tools"
$ sudo dnf group remove "Development Tools" --exclude=binutils,bison,elfutils-devel,tbb,zlib-devel
# zypper, tested on openSUSE 20231129 Live
> sudo zypper install -t pattern devel_basis
> sudo zypper remove -t pattern devel_basis
# pacman, tested on Arch Linux 20230617
$ sudo pacman -Syu # system update, it may be necessary
$ sudo pacman-key --refresh-keys # refresh the PGP keys used by pacman, it may be necessary
$ sudo pacman -S base-devel
$ sudo pacman -R base-devel
# emerge, tested on Gentoo 20230604 Live
$ sudo emerge --sync # synchronize the local copy of the Gentoo ebuild repository with the remote repository
$ sudo emerge gcc make autoconf automake
$ sudo emerge --deselect gcc make autoconf automake
On Fedora, dnf remove without –noautoremove or –exclude also removes dependencies that are no longer needed.
As for Gentoo, we’ve included an example similar to the others only for the sake of completeness, but actually without installing those packages on our test machine. In fact, the live distribution we tested already contains some orphaned packages, as we’ll see later.
In general, it’s recommended to always have a backup or snapshot of our machine before removing packages. Unfortunately, we must always bear in mind that the automatic removal of no-longer-used packages can break our Linux installation.
It’s quick and easy to remove orphaned dependencies using apt autoremove:
$ sudo apt autoremove
[...]
The following packages will be REMOVED:
g++ g++-11 libstdc++-11-dev
0 upgraded, 0 newly installed, 3 to remove and 16 not upgraded.
After this operation, 48.8 MB disk space will be freed.
[...]
Fortunately, apt asks for confirmation before proceeding. As a precaution, we should always carefully check which packages it proposes to remove.
dnf autoremove gives us an overview of packages that are ready for removal:
$ sudo dnf autoremove
[...]
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Removing:
binutils x86_64 2.39-16.fc38 @updates 24 M
binutils-gold x86_64 2.39-16.fc38 @updates 2.2 M
[...]
zlib-devel x86_64 1.2.13-3.fc38 @fedora 139 k
Transaction Summary
================================================================================
Remove 11 Packages
[...]
Similar to apt, dnf also prompts us for confirmation before continuing.
zypper provides an easy way to remove unnecessary dependencies only with the –clean-deps option when used in conjunction with the zypper remove command.
Unfortunately, there is no zypper command as simple as apt autoremove or dnf autoremove to remove orphaned dependencies after a package removal performed without –clean-deps.
However, we can work around this problem by creating a shell script that parses the output of zypper packages –unneeded, which gives us the list of unneeded packages:
> sudo zypper packages --unneeded
Loading repository data...
Reading installed packages...
S | Repository | Name | Version | Arch
--+-------------------------+-------------------+--------------------+-------
i | openSUSE-Tumbleweed-Oss | binutils-devel | 2.41-1.2 | x86_64
i | openSUSE-Tumbleweed-Oss | e2fsprogs-devel | 1.47.0-2.1 | x86_64
[...]
i | openSUSE-Tumbleweed-Oss | sparse | 0.6.4+20220627-2.3 | x86_64
So, let’s create the file zypper-clean-deps.sh (authored by gnac), which automates the process of listing unneeded packages, displaying their information, checking for dependencies, and then removing them after asking for confirmation for each one:
#!/bin/sh
zypper packages --unneeded | awk -F'|' 'NR==0 || NR==1 || NR==2 || NR==3 || NR==4 {next} {print $3}' > list
while read p; do zypper info $p && zypper se --requires $p && zypper rm $p; done < list
rm -rf list
Let’s break it down:
Let’s see its execution:
> sudo ./zypper-clean-deps.sh
[...]
Information for package binutils-devel:
---------------------------------------
Repository : openSUSE-Tumbleweed-Oss
Name : binutils-devel
[...]
Summary : GNU binutils (BFD development files)
Description [...]
[...]
[... List of packages that require binutils-devel ...]
The following package is going to be REMOVED:
binutils-devel
1 package to remove.
After the operation, 64.8 MiB will be freed.
Continue? [y/n/v/...? shows all options] (y):
A feature for zypper equivalent to apt autoremove has been actively discussed on GitHub since 2017, so it’s likely that it will be added sooner or later.
We can use pacman -Qdtq to query the package database for orphaned packages:
Let’s see what orphaned dependencies it finds in our case:
$ pacman -Qdtq
autoconf
automake
[...]
texinfo
The next step is to use the previous output to remove the listed packages via pacman -Rns $(pacman -Qdtq):
Let’s see its execution:
$ sudo pacman -Rns $(pacman -Qdtq)
checking dependencies...
[...]
Packages (21) db-6.2.32-1 db5.3-5.3.28-4 gc-8.2.4-1 guile-3.0.9-1 libis1-0.26-1 libmpc-1.3.1-1
m4-1.4.19-3 perl-5.38.1-1 autoconf-2.71-4 automake-1.16.5-2 bison-3.8.2-6
debugedit-5.0-5 fakeroot-1.32.2-1 flex-2.6.4-5 gcc-13.2.1-3 groff-1.23.0-5
libtool-2.4.7+44+gb9b44533-6 make-4.4.1-2 patch-2.7.6-10 pkgconf-2.0.3-1
texinfo-7.1-2
Total Removed Size: 365.18 MiB
:: Do you want to remove these packages? [Y/n] _
As in the previous examples, we should always check the suggested list carefully.
In Gentoo Linux, we should run sudo emerge –update –newuse –deep –with-bdeps=y @world, which is a comprehensive way to update the system before removing old or unused packages. Here’s what each part of the command does:
After updating, sudo emerge –depclean –pretend is often recommended to clean up unnecessary dependencies:
This is what we got on Gentoo Live:
$ sudo emerge --update --newuse --deep --with-bdeps=y @world
[...]
$ sudo emerge --depclean --pretend
[...]
>>> These are the packages that would be unmerged:
sys-auth/passwdqc
selected: 2.0.2-r1
protected: none
omitted: none
[...]
Number to remove: 3
$ sudo emerge --depclean
[...]
>>> Unmerging (1 of 3) sys-auth/passwdqc-2.0.2-r1...
>>> Unmerging (2 of 3) sys-kernel/genkernel-4.3.2-r1...
>>> Unmerging (3 of 3) virtual/python-cffi-1...
Packages installed: 1505
Packages in world: 211
Packages in system: 49
Required packages: 1505
Number removed: 3
None of this works unless the system is properly configured. In our case, we had to make some minor changes to the system to fix some errors and install some missing dependencies.
In this article, we’ve seen how to manage and remove orphaned packages in various Linux distributions using different package managers.
An important point to keep in mind is that while cleaning up these orphaned packages can save disk space, it can also have unwanted side effects or even break our Linux installation. Therefore, we should always proceed with caution and possibly have a disaster recovery solution.