I want to remove the recommended packages of a package using apt.

my-package is installed, and I want to keep it and its dependencies.

apt-cache depends <my-package> | grep Recommends

returns a variety of packages, none of which I want.

How can I remove these packages? I can write a bash script to do it but I'm hoping there's an apt way to do it.

I have searched online and haven't found a way to uninstall these recommended packages without uninstalling the package itself and its required dependencies. In other words, I want to keep my-package and its dependencies, but remove the packages it recommends.

Furthermore I ideally don't want to edit config files; I want to keep the system settings untouched.

  • You can construct an aptitude search command, but the easiest way I've found to do this is to just use apt-cache and grep like you have, then use the mouse to copy-paste the unwanted package names to an apt-get purge command line. It's just not something I do often enough to be worth the bother of learning a single-use specialised tool. If you want to get fancy, you can use sed, awk, or perl instead of grep to get rid of the commas and version numbers and alternations.
    – cas
    Jul 11, 2021 at 6:28
  • Warning: be careful - uninstalling a package that's only Recommended by one package may end up uninstalling other packages that actually Depend on it. Use apt's -d (--download-only) option as a dry-run so you don't accidentally uninstall stuff you didn't mean to.
    – cas
    Jul 11, 2021 at 6:31
  • I suspect the most straightforward option is to uninstall the main package with --autoremove and then install it with --no-install-recommends. If you remove rather than purge, you shouldn’t lose anything, other than downtime if any of the removed-and-reinstalled packages provide services. This would also avoid the explicit removal of packages and the consequences mentioned by cas above. Jul 11, 2021 at 7:26

1 Answer 1

apt-cache depends my-package \
| grep Recommends: \
| sed "s/Recommends:/sudo apt-get --purge autoremove/;s/<//;s/>//;s/|//" \
| sudo bash

In my case, my-package is pop-desktop and the recommended packages are the preinstalled packages on Pop!_OS. This is a great way to remove the preinstalled apps! (Be wary though, this does uninstall the Pop!_Shop as well.)

Specifically for Pop!_OS, it is also useful to do

sudo apt-get --purge -y autoremove "^libreoffice.*" && sudo reboot

because they seem to give libreoffice is slightly different section in the UI that is unaddressed by the aforementioned command.

You must log in to answer this question.