I have installed Ubuntu on the new Lenovo T470. To have the same packages as on the old notebook installed I have created a list of installed packages on the old notebook and I installed those on new Lenovo using How to list all installed packages.

Now I have almost all packages marked as installed manually (I read the some comments too late) - autoremove will not work correctly.

I would like to correct the manual/auto flags of dependent packages to auto.

I'm thinking I need to take the following steps:

  1. Create a list of all installed packages

    apt-mark showmanual >installed_packages.txt
    apt-mark showauto >>installed_packages.txt
  2. Retrieve the dependencies (quite a long process)

    for PACKAGE in $( sort installed_packages.txt ); do
        apt-cache depends ${PACKAGE} --important -q --installed | awk '/epends:/ {print $2}' >>dependencies_all.txt
    sort -u dependencies_all.txt > dependencies_uniq.txt

    Note the --important option of apt-cache. AWK will print only a package name of Depends: and PreDepends:

  3. Set the auto flag for all dependent packages

    cat dependencies_uniq.txt | xargs sudo apt-mark auto

Are these steps correct? Are the dependencies retrieved by apt-cache depends --important the same as selected for installation by APT when a package is installed?

Is there a better way to correct these flags?


Using the Python Apt API might be more efficient, especially in getting the dependencies:

#! /usr/bin/python3
from apt import cache

c = cache.Cache()
pkgs = set(pkg for pkg in c if pkg.is_installed)
seen = set()

def mark_deps(pkg):
    for dep in pkg.installed.dependencies:
        for ver in dep.installed_target_versions:
            if ver.package not in seen:

for pkg in pkgs:


It will still be slow going, but should be still faster than calling apt-cache thousands of times. Run without sudo to see what changes will be made (c.commit() will fail), and run with sudo to perform those changes.


Using aptitude, you can set a limit (press "l", lowercase L), and type in this incantation to see all the packages that are installed and not marked as automatic, but for which marking them as automatic wouldn't uninstall them because there's another installed package that already depends on them:


The following shows all the packages you have installed that are not marked as automatic and that have nothing that depends on them:


(Press G and make it say "task,status" to make the list easier to read.)

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.