Imagine the following situation. You have been happily installing lots of stuff on your Ubuntu. All was going fine until you face a problem XYZ, the only feasible solution to which is a complete re-installation! You can probably back up all your precious personal data safely, but what about all those software you had installed? You need to re-install each and every of those in your new install of Ubuntu, but if your Internet connection is slow and/or expensive, that could be a major problem. A solution is to create a local repository with all those installed packages and use it temporarily in your new install to reinstall those packages.That is what this guide will attempt to teach you.
This guide should be helpful to those as well who wants to install Ubuntu on more than one machines. they will be able to install the same software in all of those machines, but download them from net only once, thereby saving precious bandwidth.
Backing up the deb packages from your current Ubuntu installation
This step needs to be performed in your existing Ubuntu installtion
You must regularly back up your deb files from your current (old) installation in some place which is not affected by a reinstall (e.g on a removable device like a pen drive). Just copy all the deb-s from /var/cache/apt/archives to some such location. Depending on the number of deb-s you have accumulated, this may take some time. For example,
sudo cp /var/cache/apt/archives/*.deb /path/to/backups/
Obviously, replace /path/to/backup with your actual backup directory.
At this stage, it is probably a good idea to take a rough note of the names of the ‘extra’ software you have installed.
Install dpkg-dev package in your new installation
From this step onwards, everything is to be done on your new Ubuntu installation.
Before being able to create a local repository, you need to install the dpkg-dev package. Don’t worry, it is included in your Ubuntu live CD package pool, so no need for internet! Just make sure that the Ubuntu CD repository is enabled in your Software Sources and the CD is in your drive, and do
sudo apt-get install dpkg-dev
Creating the local Repository
After the reinstall, in your new Ubuntu, create a directory and put all the .debs from your backup in it . For example:
mkdir ~/debs
cp /path/to/backup/*.deb ~/debs
cd
While still in your home directory (i.e. ~/) do:
dpkg-scanpackages debs /dev/null | gzip > debs/Packages.gz
In the above line, the command generates a file Packages.gz that contains various information about the packages, which are used by APT.
Add the local repository to sources.lst
To use the newly created local repo, you have to add it to your sources.lst file.
First of all, take a backup:
sudo cp /etc/apt/sources.list /etc/apt/sources.list.orig
Now open the file in your text editor :
sudo gedit /etc/apt/sources.list
Delete everything except the line which looks like:
deb cdrom:[Ubuntu 9.04 _Jaunty Jackalope_ - Release i386 (20090420.1)]/ jaunty main restricted
(Depending on the version of Ubuntu you are using, the line will change accordingly. This line represents the package repo on your Live CD)
Add the following as a new line after the above:
deb file:/home/<your_username> debs/
Make sure that you replace <your_username> with your actual username.
Save and exit the text editor.
Update the apt database to use the local repo:
sudo apt-get update
Install the software as usual. This is where the rough note containing the names of previously installed applications will come in handy.
sudo apt-get install <package_name>
You’d probably be able to use the GUI tools like Synaptic Package Manager normally to install packages as well.
Source: http://www.debian.org/doc/manuals/apt-howto/ch-basico.en.html#s-dpkg-scanpackages
Reverting back to “natural” state
Right now, the only software repo you are able to use is your own local one. That is not really a good thing, because you have no access to the huge number of other software available online, nor can you update your packages to the latest versions! So, after you have finished with installing all the old packages from your backups, it is time to revert the system back to its “natural” state like it was before. To do so:
sudo cp /etc/apt/sources.list.orig /etc/apt/sources.list
sudo apt-get update
This will sync your local apt database with the online servers. This step requires active internet, though the downloaded data would be relatively small in size.
At this stage, you’d probably want to delete the files in your local repo, as it will no longer be needed (assuming of course that you are completely done with installing packages from your old backups). This could save a lot of hard disk space:
rm -rf ~/debs
Important Notes
- In your old installation, make sure you back up each and every deb file that had ever been downloaded to your cache. If, at any point of time in your old install, you had run #apt-get clean or had manually deleted any of the existing deb-s from your cache, this method is liable to fail, complaining of missing dependencies for some packages.
- Running #apt-get autoclean (which purges only the outdated packages from cache) on your old installation should be safe, though. In fact, it is probably a good idea to do so, just to keep you backup size to minimal.
- The packages you install from your local repository will generally not be cached again in your new installation. So, if you are looking for another reinstall sometime in near future, do not delete the backup deb-s as yet!
- The main disadvantage of this method is that your installed packages will be at an older version than what is available on the online repos. If the version difference is too much, it may sometimes create problems while installing newer packages from online repos.
- This guide will, in all probabilities. not work for cross-version reinstalltions of Ubuntu. I.e. if your backups are from Ubuntu Intrepid, and your new reinstall is Ubuntu Jaunty, the method won’t work, excpet on isolated cases.