Install (almost) the same list of Debian packages on multiple machines

Last modified on 2017-07-14

Scenario: I have a whole bunch of packages installed on Machine A and I want to replicate that package selection on Machine B.

Let’s go!

Machine A serves as the original source of the list of packages.

0. Make

Generate the list …

dpkg --get-selections > pkg-list

The pkg-list file now contains the list of installed Debian packages on Machine A. However I want to make a few changes (hence the “almost” in the title) to the list.

Delete entries for packages that have been installed then removed (marked as deinstall) …

sed -i '/deinstall/d' pkg-list

I also delete entries for packages that I built myself (identified using apt-show-versions) and are not available in the Debian package archives …

ARRAY=( $(apt-show-versions | grep -v "stretch" | awk -F: '{print $1}') )
for package in "${ARRAY[@]}"
    sed -i "/$package/d" pkg-list

I made the generatePkgList shell script to create modified package lists.

1. Install

Copy pkg-list to Machine B and update its dpkg database of known packages as root …

apt-cache dumpavail > "$avail"
dpkg --merge-avail "$avail"
rm -f "$avail"

Update the dpkg selections …

dpkg --set-selections < pkg-list

Use apt-get to install the selected packages …

apt-get dselect-upgrade

Link: Debian Administrator’s Handbook - 6.2. aptitude, apt-get, and apt Commands

🐧 Part of the HOME slash bin project.

Happy hacking!