This in-depth technical tutorial will work for any Debian based system, but is written specifically for Ubuntu.
I’ve recently had the occasion to make a complete list of software installed on one of my Ubuntu boxes, and then reinstall it from scratch. Here’s a quick and easy way to generate a list of installed .deb packages, and then use that list to quickly reinstall them.
First, let’s make the list. You’ll be doing all of this in a Terminal Session:
dpkg –get-selections | grep -v deinstall > ubuntu-files
NOTE: WordPress interprets two dashes (- -) as one dash (–). When you’re putting this into your CLI, make sure it’s dropping two dashes ‘- -’ without the space between them.
Now you’ve got a list of all of your installed debs in a fairly small file. In my case, I simply moved this file to a thumb-drive. You could also store it on a separate partition or on a disk somewhere. Heck, it’s not that big, email it to your gmail account.
So now you’ve got this list and all is well, until you’re Ubuntu install either dies or has to be reinstalled for some reason. Go ahead and do the base install.
Once you’ve got Ubuntu back up and running, copy your ubuntu-files back into your home directory and do the following:
sudo apt-get update
sudo apt-get dist-upgrade
dpkg –set-selections < ubuntu-files
NOTE: WordPress interprets two dashes (- -) as one dash (–). When you’re putting this into your CLI, make sure it’s dropping two dashes ‘- -’ without the space between them.
Now you’ve told your system what it needs to install, so let’s install it all.
sudo dselect
This will open up a dselect session. Type ‘I‘ and allow dselect to install of the the packages listed in your ubuntu-files document. When it’s finished, type ‘Q‘ and hit the ENTER key to exit dselect.
Now you’re a lot closer to where you were before.
EDIT: As my friend fak3r points out, you can modify the fist dpkg command line to have it mail yourself the list after creatoin. Like so:
dpkg –get-selections | grep -v deinstall > ubuntu-files; cat ubuntu-files | mailx -s “ubuntu-files” my.mail@my.address

Leave a Reply
32 Comments on "Ubuntu Tricks – how to generate a list of installed packages and use it to reinstall packages"
You’ve got my attention. I am planning to perform a reformat of my PC, so this will definintely come in handy. Cheers!
Hmm. Thats a nifty one. Thanks. Will certainly come in handy.
– Mskadu
Damn, that’s what I was looking for!!
Just fyi, the 1st command, “dpkg –get-selections | grep -v deinstall > ubuntu-files”, does not work if you simply copy/paste it. Because when you typed, probably it merged the two “–” into one “–”, so, simply do this:
dpkg –get-selections | grep -v deinstall > ubuntu-files
Ok, ok, it’s quite obvious. But it took me around 5s to figure out.
No os esforceis, el sistema se come los 2 guiones y los transforma en uno. Lo correcto son dos guiones antes de la orden (get-selections)
Hi all!
First, thanks for pointing out how wordpress interprets the two dashes. I’ve edited the post.
Second, we’ve been getting lots of hits today thanks to digg! Our little server is making a valiant effort to keep up, so bear with us if the site loads slow, or not well!
geek out
Thanks, I’ve been looking for docs on how to do this, much easier than I suspected! Hey, for a slicker way to backup your ubuntu-file file, cat it and pipe it to mailx for quick email action!
dpkg -–get-selections | grep -v deinstall > ubuntu-files; cat ubuntu-files | mailx -s “ubuntu-files” fak3r@fak3r.com
fak3r
just saw your comment on Digg and I was coming back here to add it to the post! Thanks for the tip!
geek out
Why use a file at all? Isn’t the point to have your dpkg selections backed up in email?
dpkg –get-selections | grep -v deinstall | mailx -s “dpkg-selections for $HOSTNAME” address@example.com
This long list of unique words will probably be flagged as spam.
I’m a Ubuntu/Linux newbiew, and I’m not near an ubuntu system, so please bare with me if is obvious…
Will this work without the sources list being updated? Or would you need to keep a copy of your sources as well?
Thanks!
Hey HipJiveGuy,
It would depend on what you had on your system, but that’s a good point. You’ll need your sources.list file to look similar if not the same to install all of the same packages.
geek out.
HipJiveGuy,
If your sources list has been modified, it would need to be backed up as well.
–Lee
Wow so the Ubuntu users are discovering one of the many wonders of Debian. ‘Bout time you got caught up. 😉
great trick.thanks 🙂
Hi, nice trick, thank you. But when you save the list of file by invoking grep -v deinstall, you pick up the “purge” packages too. So I would rather use:
sudo dpkg –get-sellection | grep ” install” > ubuntu-files
To insure that just the right packages are selected. I know that this caution implies only a fw of cases so, sorry for the useless comment 😉
Bye
Nice tip:
That space-install ” install” tip earlier doesn’t work.
This one seems to do it:
dpkg –get-selections | grep “\binstall”
The slash-b matches empty space on the edge of a word, so this ensure only the complete word ‘install’ is matched.
At least I hope so… 🙂
Donn,
Thanks for the \b tip! Very useful.
Is there a way to list ALL ubuntu packages from CLI, I mean both installed and not-yet-installed ?
I wonder where did synaptic pkg mgr retrieve those categorized pkg info from ?
Hey man, thanks a lot! That was a breeze with Ubuntu Feisty and really simplified things for me.
oops, you will also need to make the file in /home/user/.local/bin executable by in a terminal executing chmod +x /home/user/.local/bin/name_of_your_script.sh
Thanks a lot, first in google and answers directly.
I’d rather postpone the grep -v till *after* saving the file, either with tee or by grepping on the ‘set-selections’ command instead. That way, I suppress the nerves of ‘discarding’ the information I might find usesful at a later day (after I wiped my system…)
Cheers
Excellent Matt. Glad it went smoothly for you.
you can also perform the final step using apt-get instead of dselect:
sudo apt-get update
sudo apt-get dselect-upgrade
You can have this list auto-update by adding the “dpkg –get-selections” command like so:
1. Open Menu – System – Preferences – Sessions
2. On the “Startup Programs” tab click “Add”
3. Name: Update installed files
Command: {as noted in article}
Comment: Will write a list of all installed files, which can be used to reinstall them (see {wherever you save this webpage to}
4. Click OK and Close.
Now whenever you log in the list will be updated.
sudo gedit /etc/cron.d/my_cron_file
Left out “gedit”.
So, what happens if any of these previously installed packages had required a gpg key? Will it halt the installation process gracefully or wait for input?
In the “dpkg –get-selections | grep -v deinstall > ubuntu-files” command, what does the “deinstall” option do?
nice!
you can also execute the following if you replaced the hard disk.
You make a fresh install on the new disk, connect the old disk and execute:
dpkg –admindir=/media/disk-1/var/lib/dpkg –get-selections | grep -v deinstall > ubuntu-files
One little tidbit. My clean Ubuntu 11.04 install did not recognize the “sudo dselect” command. I searched the software center for that word, and installed it from there, and the restore process went just fine from there. That sure makes for a nice way to restore the software environment after screwing it up! Thanks!
Hello ArsGeek,
many thanks, it’s a very helpful post.
Is there also a way to save and restore all config files of the installed packages in the same manner?
Best regards,
Erwin
Thnks, it was what i was looking for !!!
Thanks a ton