Find minimal equivalent of packages currently installed

Solution 1:

aptitude can get you most of the way with its search feature. Here's how you find everything installed that wasn't only an automatic dependency:

aptitude search -F "%p" "?installed ?not(?automatic)"

This wont be optimal some of these may be able to be removed but I don't know a simple way to work that out. Perhaps the answer lies in man aptitude.

On second thought, with that list you could loop through it and find the dependant packages (not dependencies) for each one. If one of those is in the master list, remove the current package from the list... but only after you've parsed the whole list or you'll miss intermediates in a 3+ level dep tree.

You find dependants with a query like this:

aptitude search '~i~Dpackage'

I'd try and write the script but I'm typing this on a tiny phone keyboard. Even I have limits.

Edit: After five minute of trying to sleep, I started hacking away on this. something like this should do the job:

orig=$(aptitude search -F "%p" "?installed ?not(?automatic)")
newlist="";
for p in $orig; do
    depended=0;
    for dependant in `aptitude search -F "%p" "~i~D$p"`; do
        if [[ $orig == "* $dependant *" ]]; then
            depended=1;
        fi;
    done;
    if [[ $depended == 0 ]]; then
        newlist="$newlist $p";
    fi;
done;
echo $newlist

Note this takes a really long time to run and it might be over-keen (eg it will remove thing that you manually installed, that you want to be manually installed, if they are depended on by something else in the $orig list that you perhaps installed afterwards).

Solution 2:

I have written a small python script to do that, posted as a gist here.

You give it a set of package names on the command line, and it should give you a smaller, minimal set of packages that imply all the others you have given as arguments. Here, x11-proto-fixes-dev is implied (a dependency of) by libgtk2.0-dev:

fuwaad:~$ python mindeps.py x11proto-fixes-dev libgtk2.0-dev
libgtk2.0-dev