Tuesday, November 24, 2009

How to Make an Offline Repository of Your Own Ubuntu System

Introduction

Sometimes when you need to replicate an Ubuntu system to another computer, the usual step will be:
  1. Install Ubuntu using Installer CD/ Live CD
  2. Reboot
  3. Connect to the Internet
  4. Update
  5. Installing additional packages that you need
However, if it occurs that you need to install to another computer at another place with uncertainty of the availability and/or reliability of the Internet connection over there, one would wonder if you could prepare those updates and additional packages yourself in an offline repository.
Assuming the additional packages is the same with your Ubuntu System, this howto will explain how to make such offline repository.

Creating Offline Repository

There are 4 steps to setting up a simple repository for yourself
  1. Import the packages in a directory
  2. Scan the packages and create a file apt-get update can read
  3. Replicate the repository to portable media
  4. Add a line to your sources.list pointing at your repository

Import Packages

In your Ubuntu System, the .deb files you have installed are stored in /var/cache/apt/archives/
To replicate these files, first create the directory you want to paste into. For this example, we'll use ~/mydebs.

mkdir ~/mydebs

Then, replicate the files using rsync

rsync -upt --progress /var/cache/apt/archives/*.deb ~/mydebs

Sometimes some files you have installed is not cached. If so, you need to retrieve them from the internet. First we need to generate the list of files to be downloaded.

sudo apt-get install $(dpkg -l|grep ^ii|awk '{print $2}') --reinstall --print-uris \
-y |awk '{print $1}'|sed "s/'//g"|grep tp:// > downlist

Then download it using wget

wget -ci downlist -P ~/mydebs

Scan Packages

To be included in apt-get process, we net to generate packages list of those .deb files.
First go into mydebs directory.

cd ~/mydebs

then generate Packages.gz using dpkg-scanpackages

dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz

Replicate mydebs Directory to Portable Media

The repository can now be replicated to portable media, such as DVD-ROM or External Hard Drive. Assuming you are using external harddrive with volume name REPO:

rsync -a ~/mydebs /media/REPO/mydebs

Then, after finishing installing the new system, restore the directory to your new home folder

rsync -a /media/REPO/mydebs ~/mydebs

Adding mydebs Directory as Repository Source

To use these repository, these mydebs directory then must be included in the Repository Source file on your new system.

sudo su -pc 'echo deb file:$HOME/mydebs ./ >> /etc/apt/sources.list'

sudo apt-get update

Your repository is now ready to use