[savvyadmin] 0:home1:processes2:recipes3:explain4:archive linux & server reference since 2007
david@web-1:~$ less recipes/backup-and-restore-package-lists-in-ubuntu.md
BACKUP-AND-RESTORE-PACKAGE-LISTS-IN-UBUNTU recipes

Backup and Restore Package Lists in Ubuntu

published 2008-10-19 · refreshed 2026-10-20 · tested on Ubuntu 22.04 · apt 2.4 · dpkg 1.21 and Ubuntu 26.04 · apt 3.2 · apt-clone · tags: debian, linux, ubuntu

The 2008 method (dpkg --get-selections, --set-selections, then aptitude install) still works, with one change: aptitude is no longer installed by default, and apt-get dselect-upgrade is the command that acts on the restored selections. The better list to keep today is the one apt keeps itself, apt-mark showmanual: only the packages you asked for, so the new machine resolves dependencies fresh instead of pinning the old ones. Both are below, plus apt-clone for when the repositories and keys should travel with the list.

What to back up

apt-mark showmanual > packages-manual.txt
dpkg --get-selections > packages-all.txt

packages-manual.txt is the short list, everything installed on purpose (44 lines on a stock Ubuntu 22.04 image that has 681 packages installed). packages-all.txt is the full dpkg state, every package with its install or deinstall mark, the same file the 2008 text used. Keep both; they answer different questions.

Third-party repositories are in neither list, so copy them along:

sudo tar czf apt-sources-$(hostname).tgz /etc/apt/sources.list /etc/apt/sources.list.d /etc/apt/keyrings

Restore the manual list (clean install, any release)

This is the one for a fresh machine, including a newer Ubuntu release: apt resolves today’s dependencies and tells you about anything it cannot find.

sudo apt-get update
xargs -a packages-manual.txt sudo apt-get install -y

xargs -a turns the file into arguments. If a package in the list no longer exists in the new release, apt-get refuses the whole run and names it; delete that line and run it again.

Restore the exact state (same release)

To reproduce a machine package for package, automatically installed ones and deinstall marks included, feed dpkg the full selections and let apt act on them:

sudo dpkg --clear-selections
sudo dpkg --set-selections < packages-all.txt
sudo apt-get dselect-upgrade -y

--clear-selections marks every installed package for removal, --set-selections reinstates the ones in your file, and dselect-upgrade installs what is missing and removes what is extra. That is still the baseline trick from 2008: save the selections right after a clean install, and this sequence later strips everything added since. Leave out -y the first time to read the plan.

apt-clone: list, sources and keys in one archive

sudo apt install apt-clone
sudo apt-clone clone ~/apt-clone-$(hostname)

That writes apt-clone-<host>.apt-clone.tar.gz containing the package list, the apt sources, the trusted keys and, with --with-dpkg-repack, packages that are not available from any repository. apt-clone info <file> shows what is inside, and apt-clone restore <file> puts it back on the new machine.

AS PUBLISHED, 2008

ORIGINALThe text below is the 2008 version, unchanged, so that links from that era still point at the document they cited.

Here’s a simple tutorial on how to backup a list of all your installed repository applications, and restore them to another machine, perhaps even the same machine after a clean installation. This can save you an incredible amount of time, especially when this task must be repeated often. Of course, being that Ubuntu is based on Debian, this will work for any Debian based platform.

First, from a computer with all the applications preinstalled, retrieve your installed package list and redirect the output to a file called packages.txt. Save this package list somewhere so that you can use it for the restore process.

sudo dpkg --get-selections > packages.txt

To restore all the applications from your list, you must follow a three step process very carefully.

sudo dpkg --clear-selections
sudo dpkg --set-selections < packages.txt
sudo aptitude install

You will be prompted to install all the new applications in the list.

Another example of what this process allows you to do is create a baseline of all the applications after a clean installation of Ubuntu. Let’s say you would like to remove any applications installed since the clean install, perform the exact same process, and any package not defined in that list will be removed.

sudo dpkg --get-selections > clean-install-package-list.txt
sudo dpkg --clear-selections
sudo dpkg --set-selections < clean-install-package-list.txt
sudo aptitude install

The very first command of “–clear-selections” marks all currently installed packages to the state “deinstall”. When you restore the list of applications using “–set-selections”, only packages ommited from the list will remain in the “deinstall” state. Aptitude will honor the deinstall state and remove the extra packages, leaving you only with packages from the list. Most excellent. :)

savvyadmin.com 2026-10-20 BACKUP-AND-RESTORE-PACKAGE-LISTS-IN-UBUNTU
recipes/backup-and-restore-package-lists-in-ubuntu.md (END)