Tuesday 13 May 2014

Ubuntu: setting up a new box

I need to swap my work computer and being a lazyass programmer I decided to check out how to do this with as little hassle as possible. Here's what I came up with. This assumes basic Ubuntu installation is done on the new comp (and commands are run as sudo).
1. Get installed packages from both computers:
dpkg --get-selections > /safe/location/installed-old
dpkg --get-selection > /safe/location/installed-new
2. Save metadata from old computer:
apt-mark showauto > /safe/location/pkgs_auto.lst
apt-mark showmanual > /safe/location/pkgs_manual.lst
3. Get list of packages to install:
grep -vFf installed-new installed-old > missing 
You can go through that list manually to remove any that you don't want installed anymore.

4.  On the new box, install packages:
cat missing | cut -f 1 | tr '\n' ' ' | sudo xargs apt-get -y --force-yes install
5. Restore metadata: 
sudo apt-mark auto $(cat /safe/location/pkgs_auto.lst) 
sudo apt-mark manual $(cat /safe/location/pkgs_manual.lst)
This way system can still keep track of which packages were manually and which were automatically installed. This means that system can automatically remove packages no longer needed.

You have to add the missing apt repositories before step 4.