install the same debian packages on another system

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


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.


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;