Ubuntu – How to exclude a package from apt-get autoremove

aptpackage-management

I'm in a situation where the list of orphaned packages to be removed by apt-get autoremove includes a package that I want to keep. Apparently I have accidentally removed a package which was depending on it. How can I now mark the package as explicitly desired, so that apt-get autoremove will not remove it?

Best Answer

Use apt-mark

$ man apt-mark
...
manual
       manual is used to mark a package as being manually installed, which will 
prevent the package from being automatically removed if no other packages 
depend on it.

So

sudo apt-mark manual <package-name>

Now autoremove won't remove it.

To undo

sudo apt-mark auto <package-name>

Now autoremove will remove the package if it is not a dependency of any other package.