While installing packages and software programs on the Linux desktop using the distribution’s package manager, various other dependencies are also installed along with the desired application.
And when you uninstall that application, the dependencies which were installed along with the package are not always uninstalled and remain on your system, taking up disk space. This issue is circumvented when using sandboxed applications installed as a Flatpak or snap applications, as the whole sandboxed application is removed upon application.
Some package managers such as DNF on Fedora tries to remove the dependencies of the installed application as well when you uninstall anything from the Terminal. However, this does not work when you use the GNOME Software GUI application, which uses the DNF command in the background.
In this tutorial, we will explore commands that we can use to uninstall unused packages and dependencies from your distribution using the major package managers such as Pacman, DNF, or even APT. We will also add the long commands to our bash profile so that you don’t have to remember the long commands every time you wish to remove the unused packages.
Unused packages in Linux can be removed using package managers. For Debian and Ubuntu, use ‘sudo apt-get autoremove && sudo apt-get autoclean’. On Fedora, ‘sudo dnf autoremove’ is used. On Arch Linux, use ‘pacman -R $(pacman -Qdtq)’. All these commands can be added as aliases in your bash profile for easy use.
Clearing Unused Packages in Debian and Ubuntu
Debian and Ubuntu, as well as their derivative Linux distributions such as Pop_OS! and Linux Mint use the APT package manager to install and uninstall packages. In order to identify the unused dependencies on these Linux distributions, just enter the following commands in your Linux Terminal:
sudo apt-get autoremove && sudo apt-get autoclean
As you can notice, the autoremove command tells the APT package manager to automatically remove any unused dependencies and the autoclean flag removes the data which might have been created by the package on your distribution.
You can add this command as an alias to your bash profile by simply typing the following command in your Terminal:
echo 'alias remove="sudo apt-get autoremove && sudo apt-get autoclean"' >> .bashrc source .bashrc
From now on, whenever you type ‘remove’ in your Terminal, this command will automatically run and uninstall unused packages.
Sweeping Out Unwanted Packages in Fedora Workstation
The DNF command handles uninstallation pretty well. Still, if you want to remove unused packages on Fedora manually, then you can use the DNF command along with the
sudo dnf autoremove
Again, if you wish to type a short command instead of this long one, then you can add it to your BASHRC file. To do that, simply type the following commands in your Terminal window on the Fedora Workstation:
echo 'alias remove="sudo dnf autoremove"' >> .bashrc source .bashrc
Purging Redundant Packages on Arch Linux
On Arch Linux and any of its derivative Linux distributions such as Manjaro or Endeavor OS, the Pacman command is used to install and uninstall packages. In order to identify unused dependencies, Pacman command can be used with the following parameters:
Now, once the packages are identified, you can use the output of this command to pass it through the
pacman remove command. Just type the following commands in your Terminal on Arch based distribution:
pacman -R $(pacman -Qdtq)
You can also add this command to your bash profile, so that you don’t have to remember the command arguments. You can do that by using the echo command:
echo 'alias remove="pacman -R $(pacman -Qdtq)"' >> .bashrc source .bashrc
Now, whenever you type ‘remove’ in your terminal, this command will be executed.
Conclusion: Streamlining Package Management in Linux
The efficiency of package management in Linux is highlighted by the ease with which unused packages and dependencies can be removed. Until APT and PACMAN catch up with DNF’s automatic removal feature, bash aliases provide a handy workaround. What other strategies can you think of to optimize package management in Linux?