Saving and restoring the list of packages installed on a Debian system using aptitude or deborphan
18th October 2013
The usual, or even classical way is to create the list of installed packages with sudo dpkg --get-selections > package_list
, and then restore when/if necessary with cat package_list | xargs sudo apt-get -y install
.
As VihangD points out in his serverfault answer, the same can be achieved with aptitude, while also excluding dependent, automatically installed packages (which are included by the classical method). To create the list of packages, run aptitude search -F '%p' '~i!~M' > package_list
. Here, -F '%p'
asks aptitude to only print package names (instead of the default output, which also contains package state and description); search term ‘~i!~M’ asks for all non-automatically installed packages.
To install packages using the created list, run xargs aptitude --schedule-only install < package_list; aptitude install
. The first of these two commands instructs aptitude to mark all the packages from the list as scheduled for installation. The second command actually performs the installation.
Hamish Downer suggests an alternative way of getting the initial package_list: using the deborphan utility, deborphan -a --no-show-section > package_list
. This command asks deborphan to show a list of packages, which have no dependencies on them. Sounds very similar to what we did with aptitude above, but using deborphan will most likely result in a much shorter list of packages (on my system, deborphan printed 291 package names, aptitude printed 847, and dpkg printed 3650 package names). One more potentially important difference between aptitude- and deborphan-produced package lists is that aptitude only specifies package architecture when it is different from native (e.g. 'googleearth:i386' on a 64-bit system), while deborphan specifies architectures for all the packages (resulting in e.g. 'google-talkplugin:amd64' and 'googleearth-package:all' on a 64-bit system).
November 17th, 2015 at 8:16
Thanks for posting. FWIW,
* I referenced this post @ http://unix.stackexchange.com/a/243510/38638
* I have a question about `sudo` and `xargs` in the package-restore commandline
xargs aptitude --schedule-only install < package_list; aptitude install
in a comment on the serverfault answer you cite.
November 17th, 2015 at 13:55
Hi Tom,
first of all,
dpkg --set-selections
seems to accept multiline input on stdin (as per dpkg man page) – this is why it does not need xargs.In the case of aptitude command, package names are added as command arguments; as there can be many packages, maximal command line length might be exceeded.
To avoid that, xargs is used: it feeds arguments (lines) from package_list to aptitude until the command line length limit is reached.
When that happens, another aptitude command will be constructed and run, until all lines from package_list are processed.
Sudo can be added either before xargs, or before aptitude – there should be no visible difference with default sudo configuration. I’d put sudo before xargs, though. I haven’t tested this, so please reply if your experience contradicts my advice