I'm on Debian and I want to re-partition my drive and reinstall. Is there an easy way to restore all currently installed packages after a fresh installation?

I'm thinking of doing it by making a backup app list:

#generate list of installed packages
dpkg -l | awk '{ print $2 }' ORS="\n" | tail -n +6 > reinstallList.txt

and then after doing a fresh install I should be able to do:

#install from backup at reinstallList.txt
sudo apt-get  install $(< reinstallList.txt)

Is there a better way of doing this?

  • 1
    Don't forget to make a backup of /etc so you can easily restore configuration settings for those packages. – Anthon Dec 9 '13 at 9:26

According to the Debian wiki, you should run this command before the reinstall:

dpkg --get-selections > /backup/installed-software.log

Then, after reinstall, run this:

dpkg --set-selections < /backup/installed-software.log
apt-get dselect-upgrade

Obviously, you should replace /backup/installed-software.log with the name of some file that you can keep during the reinstall process. Putting it on a thumb drive would be good.

For more information, see the wiki: https://wiki.debian.org/ListInstalledPackages

  • I've used this method many times. – bahamat Dec 9 '13 at 8:30
  • Perfect! This is exactly what I was looking for. Thanks! – nyxgeek Dec 9 '13 at 9:14

dpkg -l lists all installed packages (at list when you filter it right). You can get the same list with dpkg --get-selections (which requires no further filtering).

This loses information about manually vs. automatically-installed packages. It's very convenient to have libraries and other packages marked as only indirectly-needed. Packages marked as automatically installed can be removed or replaced by different packages without fuss. Dpkg doesn't know about automatically-installed packages, only apt does.

To list the manually-installed packages, you can use aptitude:

aptitude search -F %p '~i !~M' >reinstallList.txt

Without aptitude, it's a bit more complicated.

dpkg --get-selections | awk '$2 == "install" {print $1}' >installed.txt
apt-mark showauto >automatic.txt
comm -32 installed.txt automatic.txt >reinstallList.txt

To install all the packages that were formerly installed:

apt-get install $(cat reinstallList.txt)

Alternatively, you can use the more roundabout method of copying the list of installed packages, and then restoring the list of packages marked as automatic. To back up:

dpkg --get-selections >selections.txt
apt-mark showauto >automatic.txt

To restore:

dpkg --set-selections <selections.txt
apt-get dselect-upgrade
apt-mark markauto $(cat automatic.txt)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.