16

How do I export a list of installed Debian packages on a system, and then install those same packages on a new system?

jes5199
  • 377
  • 1
  • 4
  • 8

5 Answers5

19

To backup:

sudo dpkg --get-selections > /tmp/dpkglist.txt

To Restore:

sudo dpkg --set-selections < /tmp/dpkglist.txt
sudo apt-get -y update
sudo apt-get dselect-upgrade

Also see this question for additional options and info: Ubuntu, how to setup a new machine like an existing one

I have the above running in a daily cronjob that checks the dpgklist into SVN as part of our server inventory. This will allow you to keep a reasonable accurate inventory of installed packages across your servers and its easy to do a quick side-by-side diff to see if a server is missing a particular package.

faultyserver
  • 1,914
  • 1
  • 16
  • 20
13

aptitude also satisfies this usecase, and it preserves information about "automatically installed" packages that other methods do not. Run the following on the reference machine:

aptitude search -F '%p' '~i!~M' > package_list 

Copy package_list to the other machine and run

xargs aptitude --schedule-only install < package_list; aptitude install; 
TomRoche
  • 243
  • 4
  • 11
Vihang D
  • 630
  • 4
  • 7
  • Just a minor note. According to the aptitude reference manual, the "package" field is "expandable" by default, so `aptitude search -F '%p' '~i!~M' > package_list` should also work fine. – chronos Oct 17 '13 at 23:34
  • Regarding package backup: see plug for `deborphan` @ http://bogdan.org.ua/2013/10/18/saving-restoring-list-of-packages-installed-on-debian-using-aptitude-deborphan.html – TomRoche Nov 17 '15 at 05:43
  • `[insert your own linebreaks, since http://serverfault.com/editing-help#linebreaks lies]` Regarding package restore: this will need to be done as root, no? If so,
    The `xargs` in your restore commandline= xargs aptitude --schedule-only install < package_list; aptitude install; makes me want to know,
    1. Why does `aptitude` need the `xargs`? `dpkg --set-selections` doesn't.
    2. Presuming `aptitude` *does* need the `xargs`, where to put one or more `sudo`s in your restore commandline? Or otherwise get root for running that line, presuming that's necessary.
    – TomRoche Nov 17 '15 at 06:00
  • Presumably, `aptitude` does not accept a list of packages on stdin but only on the command-line. The `xargs` command will do that, even calling the command multiple times if there are more than some limit (500 by default?) of values. `sudo` would need to be specified before `xargs` which will then run `aptitude` as the same user. And again before `aptitude` after the semicolon (`;`). – Brian White Jun 09 '22 at 18:20
0

In recent versions of Debian/Ubuntu/Mint, dpkg needs available packages to be in its "avail" database for dpkg --set-selections to work.

Example sequence:

  1. (On other system) dpkg --get-selections > installed.dselect
  2. sudo apt update
  3. apt-cache dumpavail | sudo dpkg --merge-avail
  4. sudo dpkg --set-selections < installed.dselect
  5. sudo apt-get dselect-upgrade

The third command populates dpkg's "avail" database. It's important to run this before setting the selections of which additional packages to install.

This reqires dpkg v1.17.7 and later. See Q: Why does ''dpkg --set-selections'' not record selections for unknown packages? on the Debian wiki for more details.

Alastair Irvine
  • 1,182
  • 11
  • 24
0

That's a good idea, and you might also set up one server with apt-proxy if you make a habit of this.

  • After playing around with several proxies for apt, I ended up deciding on apt-cacher-ng. apt-cacher-ng is very simple to setup and from several accounts it seems to be more robust than the original apt-proxy. Each person has their own favourite though. http://www.unix-ag.uni-kl.de/~bloch/acng/ – faultyserver Aug 20 '09 at 21:06
  • My vote is for approx, it's the only one that isn't some kind of insane. – womble Aug 20 '09 at 23:08
  • Interesting... I've not actually tried any of the alternatives. –  Aug 21 '09 at 16:00
0

faultyservers answer worked for me only after running a different command as per http://rayslinux.blogspot.de/2012/10/ubuntu-1210-dpkg-warning-package-not-in.html

sudo apt-get install dselect sudo dselect access sudo dselect update

Before that running

sudo apt-get dselect-upgrade

only returned

[...]
dpkg: warning: package not in database at line 302: xfonts-utils
dpkg: warning: found unknown packages; this might mean the available database is outdated, and needs to be updated through a frontend method
pi@FHEM-new:/tmp $ sudo apt-get dselect-upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

I was trying to install the same packages from my old Raspberry Pi (running Raspbian GNU/Linux 7 (wheezy)) on my new Raspberry (Raspbian GNU/Linux 8 (jessie)).

adiuva
  • 394
  • 2
  • 7