How to backup and restore installed packages

If you ever wondered about how to backup list of installed packages and then restore it on another machine then you need to know that it does not require anything more then three basic utilities - dpkg to import/export list of packages, apt-get to install selected packages and apt-mark to restore extended package states.

Backup and restore installed packages

Export list of the installed packages on the local machine to packages.list file.

local$ dpkg --get-selections > packages.list

On the remote machine you need to select packages exported in the previous step.

remote$ sudo dpkg --set-selections < packages.list

Execute update action on the remote machine.

remote$ sudo apt-get update
I assume that local machine is up to date.

Install earlier selected packages.

remote$ sudo apt-get -u dselect-upgrade

Backup and restore extended package states

Read apt-mark manual page to learn more about extended package states.

Export list of automatically installed packages.

local$ apt-mark showauto > package.states.auto.list

Export list of manually installed packages.

local$ apt-mark showmanual > package.states.manual.list

Export list of packages on hold.

local$ apt-mark showhold > package.states.hold.list

The last step on the remote machine is to import states exported a moment ago.

remote$ sudo apt-mark auto $(cat package.states.auto.list)
remote$ sudo apt-mark manual $(cat package.states.manual.list)
remote$ sudo apt-mark manual $(cat package.states.auto.list)
Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system. He is also open for new opportunities and challenges.

Gdansk, Poland https://sleeplessbeastie.eu