Download Latest Version apt-sel.py (13.9 kB)
Email in envelope

Get an email when there's a new version of apt-sel

Home
Name Modified Size InfoDownloads / Week
apt-sel.py 2026-01-10 13.9 kB
README.rst 2026-01-10 4.4 kB
LICENSE.txt 2026-01-06 903 Bytes
Totals: 3 Items   19.2 kB 0

apt selections backup (and restore)

Suppose you have an Ubuntu Linux installation, or Debian, or any other flavour that uses the apt and dpkg package management system. Suppose you want to set up another one the same way, specifically install the same packages, e.g. when simultaneously upgrading OS version and root disk. In that case you want to save a list of installed packages on one system and apply it on the other.

If you google for that problem, the usual solution boils down to:

dpkg --get-selections > the_packages.txt
dpkg --set-selections < the_packages.txt

which kinda works but loses the information of what was installed explicitly and what is an automatically installed dependency. Which sucks very hard later when old cruft doesn’t get auto-uninstalled.

Another solution is to use the existing solution apt-clone, which clones everything, package sources, versions, everything. Which might exactly be what you need, so have a look at it!

In my case, I just wanted a better get-selections/set-selections that keeps auto info intact and works reasonably well accross versions. There is an answer on unix.stackexchange that describes what apt-clone does, and this tool basically does a subset.

Use

On the source machine do:

apt-sel.py the_packages.txt

Transfer the file to the target machine. Make sure the available-packages-list is up-to-date (aptitude or apt update), then do:

sudo apt-sel.py --restore the_packages.txt

If you get errors about files, packages or dependencies not found, which would be normal when transferring selections between different OS versions, you can try to resolve as many as possible with:

sudo apt-sel.py --repair the_packages.txt

…but my experience is mixed.

Now call whatever you normally use to apply these selections – I would use aptitude to edit remaining problems and actually install, but apt-get dselect-upgrade probably will do the trick, too.

For more options see:

apt-sel.py -h

Old drive mounted in new machine

There is a special case where you do not have two machines but one machine with a new harddisk that install a new system on but want to clone the old selections as a starting point. In that case the source is not in the normal locations under /var/lib but rather in the old filesystem under /mnt/var/lib (or wherever you have mounted it).

The underlying dpkg has command line options and environment variables for dealing with that situation, but the also ungerlying apt-mark has not, therefore apt-sel.py cannot deal with this by itself.

What you can do – provided your old drive contains a working system including python3 and assuming that you have mounted it under /:

cp apt-sel.py /mnt/root
sudo chroot /mnt /root/apt-sel.py /root/the_packages.txt

sudo apt-sel.py --restore /mnt/root/the_packages.txt

Prerequisites

The script is written in Python3, any old version should do. It uses the external executables apt-mark (backup) and additionally dpkg and xargs (restore).

How it works

Backup mode does something very similar to dpkg --get-selections, except that it uses apt-mark showinstall, which avoids the need to filter for non-install selections. It also runs apt-mark showauto for a list of automatically installed pckages. It dumps that information into a file compatible with dpkg --set-selections but retains the extra information in some magic comments.

Also it filters /etc/apt/sources.list and /etc/apt/sources.list.d/* and puts the result into a comment. For details look into the generated file with a text editor.

Restore mode feeds the file to dpkg --set-selections as-is (it’s compatible, remember?) and then calls apt-mark auto for each package that should be automatically installed. If uses xargs to cut down on the number of actual calls.

Source: README.rst, updated 2026-01-10