Backup and restore apt packages on Debian-Ubuntu

Tue 24 July 2018

Suppose you want to install all the apt packages already installed on one server to another one.

On the source machine execute the following commands to make a list of installed packages and their “states” (installed manually or automatically):

sudo dpkg --get-selections > pkgs_selection.lst
sudo apt-mark showauto > pkgs_auto.lst
sudo apt-mark showmanual > pkgs_manual.lst

Copy files pkgs_selection.lst, pkgs_auto.lst and pkgs_manual.lst to the destination machine.

On the destination machine execute the following commands to install the packages that are installed in the source machine and set the correct “state” for packages:

sudo apt-get install dselect
sudo dselect update
sudo dpkg --set-selections < pkgs_selection.lst
sudo apt-get dselect-upgrade
sudo apt-mark auto $(cat pkgs_auto.lst)
sudo apt-mark manual $(cat pkgs_manual.lst)