Clone Your Ubuntu installation

If you want to create a system that is similar to a different system you have already set up, it can be difficult to remember each and every package you had installed.This method works best when you are exporting to and importing from the same distribution and, specifically, the same releasefor example, exporting from Ubuntu Dapper to Ubuntu Dapper or ubuntu edgy to ubuntu edgy.

Ubuntu uses the APT package management system which handles installed packages and their dependencies. If we can get a list of currently installed packages you can very easily duplicate exactly what you have installed now on your new machine. Below is a command you can use to export a list of your installed packages.

sudo dpkg --get-selections | grep '[[:space:]]install$='| awk '{print $1}' > installedpackages

Now you should end up with a file called “installedpackages” which consists of a long list of every package your currently have installed.

The next step would be to tell the clone machine to install each of those packages. You’ll have to copy that file to the clone machine (via network, usb drive, email, etc) and also make sure to duplicate the /etc/apt/sources.list file. Without the same access to repositories it may not be able to find the packages.

To tell your system to use the previously exported package list use the following command (after making sure to also clone your /etc/apt/sources.list file)

Update the source list using the following command

sudo aptitude update

Import the package list using the following command

cat installedpackages | xargs sudo aptitude install

16 thoughts on “Clone Your Ubuntu installation

  1. Not an expert with regular expressions but found that grep ‘[[:space:]]install$=’ worked when the ‘=’ was removed.

    Very useful article though

    Thanks

  2. What about using a more simple approach?

    sudo dpkg –get-selections > packages.txt

    Copy packages.txt to the remote machine and do:

    sudo dpkg –set-selections

  3. (QUOTE)
    What about using a more simple approach?

    sudo dpkg –get-selections > packages.txt

    Copy packages.txt to the remote machine and do:

    sudo dpkg –set-selections
    (/QUOTE)

    It took me a while to find the answer to this but the last step of this process is:

    sudo apt-get dselect-upgrade

    -GZ

  4. I don’t know why but this, from above, did not work for me:
    sudo dpkg –get-selections | grep ‘[[:space:]]install$=’| awk ‘{print $1}’ > installedpackages

    However, this worked great:
    sudo dpkg –get-selections | perl -e ‘@lines = ;foreach(@lines){ $_ =~ m/(^[a-z,A-Z,0-9,-_]+)\s+.*/;print “$1\n”;}’ > installedpackages

  5. I use the scripts and have some troubles, but the idea is great to clonning ubuntu instalations. So, i use this scripts as root user and work fine:

    Extract the packages:
    dpkg –get-selections | grep install | awk -F” ” ‘{print $1}’ > pkg_files

    To install:
    cat pkg_files | xargs apt-get install -y

    regards,

  6. Hi,

    that sounds good, I was looking for something like that, but as a real basic user, I’d have two questions :
    1 – what if we have uninstalled something from the vanilla ubuntu installation ? Will it get uninstalled as well with these scripts ? Or just ignored by the script and therefore left on the target system ?
    2 – I see several scripts here above, in the article and the comments, all Chinese to me. I’m good only at copy-pasting, is there one that you gurus agree is better than the others ? (as in, more robust ?)

    many thanks in advance, cheers

  7. sudo dpkg –get-selections > packages.txt
    sudo dpkg –set-selections &lt packages.txt ## The less-than sign got eaten before, along with everything after it.
    sudo apt-get dselect-upgrade

    That system works well and is easy to use. No regexp required.

  8. I get errors running any of the:
    dpkg –get-selections | grep install | awk -F” ” ‘{print $1}’ > pkg_fil
    Ubuntu Intrepid 8.10, trying to store system before upgrade to 9.10

  9. I disagree with all of these solutions.

    If you do a fresh install, you need to do a fresh install of all your packages as well. Sorry, but in theory it sounds great, but in practice does not work. The list is so long that you don’t know what your installing, and the config files will all be new as well. Sorry, but in my opinion, there is no easy way to re-install all the software from an old system to a fresh new install. You would spend much less time just doing it all over again. Keep a simple text list of programs that you really need and go from there. Even then, things will change. Change is the only thing you can count on…

  10. @RickBragg.
    I think the dpkg method is effectively a fresh install over the new system of a list of (presumably kept up to date) packages. The list is only a list of each package to install, not the code.
    However on the config side, you are correct I think. Without some way of collecting (from no well defined place) all the config files anyone using this will be lost.
    @ALL
    BUT… for ubuntu there is ‘remastersys’ you can create a snapshot of a working install onto a live DVD. I think (though I’ve not tried it) it also allows you to select install to disk, effectively cloning your system.
    http://www.geekconnection.org/remastersys/

  11. I’m cloning an Ubuntu 10.10 box. This worked for me:

    Server A: dpkg –get-selections | grep ‘[[:space:]]install$’| awk ‘{print $1}’ > installedpackages

    Server B: cat installedpackages | xargs apt-get install -y

  12. Yeah, seem’s overly complex. On my 10.04.2 systems:

    sudo dpkg –get-selections | grep ‘[[:space:]]install$=’| awk ‘{print $1}’ > installedpackages

    That ” grep ‘[[:space:]]install$=’ ” part actually returns nadda. So then printing $1 is useless. The result is a blank file. dpkg –set-selections > installedpackages seems to work best.

    Then on the target system, not sure what ‘cat installedpackages | xargs sudo aptitude install’ would have done but “dpkg –set-selections < installedpackages" followed by "apt-get -u dselect-upgrade" seemed to work much easier.

    Also, @RickBragg, it's all subjective to your specific needs. I spent hours with a fresh 10.04.2 installation installing packages and learning which packages were needed, which weren't, removing unneeded packages, etc. no 'custom configuration' but getting my package list right. I also had installed perl modules from aptitude and after testing our application, learned which ones were needed, it took the better part of 2 hours of testing to learn all the various modules that were needed. I then cloned that server to 4 physical servers using a disk image, this was easiest. But then, I wanted to test various use cases but couldn't use a real physical server to do this, so I created a Virtual Server. However the specs are too radically different for me to just restore my image, so I did a clean Ubuntu 10.04.2 install on the virtual server and rather than go through my history on one of the physical servers and see what I did and didn't install, etc. etc. I just used this method. Like I said, specific use examples.

  13. I very strongly recommend remastersys for three reasons particularly for the desktop systems.
    1. You have a snapshot of your system as a backup
    2.You have an installation media with all your packages on it. Thus no need to download them again.
    3.You save bandwidth and reduce load on the download server

    I have been using this approach

  14. You all are forgetting the most important thing in cloning… Cloning a server is having the same packages and the same VERSIONS of the packages… apt uses the official repositries which are up to date : you can never find an old version in these repos. Their is this website launchpad.net that contains old versions of packages, but their is a big problem in the duplication : package dependencies. And in order to have a real clone you may not use the apt to install the desired packages as versions won’t be respected… I’m actually wotking on a project that needs a server duplication, it has been more than 3 months and we still can’t figure out the solution.

Leave a comment

Your email address will not be published. Required fields are marked *