4

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
    done
    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?

Zanna
  • 70,465
lucky62
  • 141

2 Answers2

3

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:
                seen.add(ver.package)
                pkg.mark_auto()
                mark_deps(ver.package)


for pkg in pkgs:
    mark_deps(pkg)

print(c.get_changes())
c.commit()

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.

muru
  • 197,895
  • 55
  • 485
  • 740
0

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:

?and(?or(?reverse-predepends(~i),?reverse-depends(~i)),?and(~i,!~M))

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

?and(?not(?or(?reverse-predepends(?and(~i,!~M)),?reverse-depends(?and(~i,!~M)))),?and(~i,!~M))

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