Replicate Installed Package Selections From One Ubuntu System To Another

  • Published: April 24, 2017


This guide shows how to restore all packages used in an Ubuntu instance into another one, to have the same programs available.

Having a backup of the software used is a good practice to easily replicate your environments accross multiple machines.

Here I will show you two methods:

  1. apt-mark: easier and more secure way.
  2. dpkg: if can’t use apt-mark then you can follow this way.

Using apt-mark

apt-mark handles various settings for packages. We can separate our backup files in two files:

  1. One will hold the packages installed automatically
  2. Other file will hold the packages we install manually
apt-mark showauto
showauto is used to print a list of automatically installed packages with each package on a new line. All automatically installed packages will be listed if no package is given. If packages are given only those which are automatically installed will be shown.
apt-mark showmanual
showmanual can be used in the same way as showauto except that it will print a list of manually installed packages instead.

So we create these two files:

$ apt-mark showauto > pkgs_auto.lst
$ apt-mark showmanual > pkgs_manual.lst

Then we restore the files in the target machine:

$ sudo apt-mark auto $(cat pkgs_auto.lst)
$ sudo apt-mark manual $(cat pkgs_manual.lst)

apt-mark auto
auto is used to mark a package as being automatically installed, which will cause the package to be removed when no more manually installed packages depend on this package.
apt-mark manual
manual is used to mark a package as being manually installed, which will prevent the package from being automatically removed if no other packages depend on it.

Using dpkg

dpkg is a package manager for Debian-based systems. It can install, remove, and build packages, but unlike other package management systems, it cannot automatically download and install packages or their dependencies.

In one machine, we make a local copy of the package selection states running:

$ dpkg --get-selections > packages.txt

dpkg --get-selections
Get list of package selections, and write it to stdout. Without a pattern, non-installed packages (i.e. those which have been previously purged) will not be shown.

The packages.txt file will look like:

xterm                                           install
xtrans-dev                                      install
xubuntu-artwork                                 install
xul-ext-ubufox                                  install
xz-utils                                        install
yarssr                                          install
yelp                                            install
youtube-dl                                      install
zeitgeist-core                                  install
zenity                                          install
zenity-common                                   install
zip                                             install

Then we can backup this file to restore it later on another machine.

To install these selections we need to do it in three steps:

Update available file

Update the available file with the package manager.

$ sudo apt-cache dumpavail | sudo dpkg --merge-avail
Updating available packages info, using -.
Information about 85628 packages was updated.

apt-cache dumpavail
dumpavail prints out an available list to stdout. This is suitable for use with dpkg(1) and is used by the dselect method.
dpkg --merge-avail
Update dpkg's and dselect's idea of which packages are available. With action --merge-avail, old information is combined with information from Packages-file. dpkg keeps its record of available packages in /var/lib/dpkg/available.

Set selection state

Set the selection state on the requested packages with dpkg:

$ dpkg --set-selections < packages.txt

dpkg --clear-selections
dpkg --set-selections
Set package selections using file read from stdin.

Download and install

Packages are not installed yet, to download and install the requested packages. Execute apt-get to perform the installation of the packages specified previously in our file.

$ sudo apt-get -u dselect-upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done

apt-get -u dselect-upgrade
dselect-upgrade is used in conjunction with the traditional Debian packaging front-end, dselect. dselect-upgrade follows the changes made by dselect(1) to the Status field of available packages, and performs the actions necessary to realize that state (for instance, the removal of old and the installation of new packages).


The problem with the dpkg method is that package dependencies get lost, so each package don’t know what other packages are related to them, which makes something like apt-get –purge remove … problematic.

On the other side, the apt-mark method is safer and keeps all the dependencies, and even know if they were installed automatically or manually which makes it more suitable for this task.


Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus

How to get a list of all installed packages on an Ubuntu server and install them in another machine.

Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 ·