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?


Use apt-mark

$ man apt-mark
       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.


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.

  • 1
    Another handy trick is that you can mark many packages at once like this: sudo apt-mark manual <package1> <package2> <package3> etc – Msencenb May 15 '18 at 21:59

A few more Google attempts brought up a solution:

It is either possible to just install explicitly:

sudo apt-get install <package>

or marking as manually installed via

sudo apt-mark manual <package>

apt won't re-install, the output will just look like:

$ sudo apt-get install tmux
Reading package lists... Done
Building dependency tree       
Reading state information... Done
tmux is already the newest version.
tmux set to manually installed.

This is already answered well, but I found a situation where I did not want to "mark" many of the packages (and then un-mark them after autoremove).

When the list of packages you want to autoremove is easily-defined, then you can pipe/sed/xargs them out.

I don't have a complex example of many packages, but if I have the following scenario:

root@fptc-rsvrd:~# apt-get autoremove
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
  libluajit-5.1-2 libluajit-5.1-common linux-headers-4.4.0-141 linux-headers-4.4.0-141-generic linux-headers-4.4.0-143 linux-headers-4.4.0-143-generic linux-headers-4.4.0-146 linux-headers-4.4.0-146-generic
  linux-image-4.4.0-141-generic linux-image-4.4.0-143-generic linux-image-4.4.0-146-generic linux-image-extra-4.4.0-141-generic linux-modules-4.4.0-143-generic linux-modules-4.4.0-146-generic
  linux-modules-extra-4.4.0-143-generic linux-modules-extra-4.4.0-146-generic linux-signed-image-4.4.0-141-generic pandoc-data
0 upgraded, 0 newly installed, 18 to remove and 19 not upgraded.
After this operation, 907 MB disk space will be freed.

and I want to remove just the linux* packages, I can do this:

root@fptc-rsvrd:~# apt-get autoremove -s | sed -ne 's/Remv \(linux[^[]*\)\[.*/\1/gp'

So from here, it's easy to pass these via xargs as command-line arguments to the simple apt-get remove -y:

apt-get autoremove -s \
  | sed -ne 's/Remv \(linux[^[]*\)\[.*/\1/gp' \
  | xargs apt-get remove -y

Normally when using xargs, I'd guard against spaces in the arguments (e.g., find ... -print0 | xargs -0 ...), but since package names don't have spaces in them, I'm comfortable using newline-delimited arguments.

(I'd think it other situations, it'd be more appropriate to "mark" a hold, the unhold the packages. That can also be done with regexes and xargs, but is probably over-engineering the situation.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.