How to get a list of all installed packages on an Ubuntu server and install them in another machine.
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.
We explore some methods to achieve this with their pros and cons.
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.
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.
</dd> </dl> ### 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.
$ 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).
- 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.
$ apt-mark showauto > pkgs_auto.lst $ apt-mark showmanual > pkgs_manual.lstThen 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.