Tuesday, January 11, 2011

How to install a local ubuntu repository with apt-mirror

Begin by determining where you want your repositories to reside. You'll need plenty of drive space. Just as an example I'm mirroring Ubuntu 10.04 Lucid Lynx; 32 bit, packages only - no source code, all repositories and I'm using almost 40 gigs of space.

I'm using my mirror as the example for this tutorial and assuming your mirror will be used internally, not offered as an official mirror.

Start by installing apt-mirror and apache2:
System – Administration – Synaptic Package Manager.
Searching for apt-mirror then apache2 will give you the packages you need.
If you prefer to install using the terminal:

apt-get install apt-mirror apache2
Back up the configuration files:
sudo cp /etc/apt/mirror.list /etc/apt/mirror.list.orig
sudo cp /etc/apt/sources.list /etc/apt/sources.list.orig
sudo cp /etc/cron/apt-mirror /etc/cron/apt-mirror.orig
sudo cp /var/spool/apt-mirror/var/postmirror.sh /var/spool/apt-mirror/var/postmirror.sh.orig
This can also be done without the terminal by opening each file in your chosen text editor (gedit for example) and choosing “save as” to place the backup copy in your home folder.

Now edit the files. The examples use nano, but gedit can also be used by invoking it within a terminal using gksudo.

Starting with mirror.list:
sudo nano /etc/atp/mirror.list
The config section of the file should not require any editing.
For the most part, the repositories section should be okay with its default entries as well. You might want to add the partner repository by adding this line:
deb http://archive.canonical.com/ubuntu lucid partner
You might also want to pull your security updates from security.ubuntu.com which would require this modification:
deb http://security.ubuntu.com/ubuntu lucid-security main restricted universe multiverse
You can also remove or comment out the lines dealing with source code if you don't expect to need or use them. You can always add repositories later if you want. The time it takes to populate your mirror after adding something is less than the initial population of your mirror, so you can start with the minimum and add more later as the need arises.

Add a couple of lines to the “clean” section to clean up the extra repositories if you added or modified them as above:
clean http://archive.ubuntu.com/ubuntu
clean http://security.ubuntu.com/ubuntu
clean http://archive.canonical.com/ubuntu
Now you can run apt-mirror for the first time. You might want to wait on this step since it will take a while for your mirror to build, overnight for example. Also note that apt-mirror should be run as the user apt-mirror.
sudo su apt-mirror
You'll see it working, but it will appear slow.

Once the mirror is populated, link the repository to the web server:
sudo ln -s /var/spool/apt-mirror/mirror/archive.ubuntu.com/ubuntu /var/www/ubuntu
sudo ln -s /var/spool/apt-mirror/mirror/security.ubuntu.com/ubuntu /var/www/security
sudo ln -s /var/spool/apt-mirror/mirror/archive.canonical.com/ubuntu/ var/www/canonical
NOTE: Only the first entry is necessary if you don't add or modify repositories.

Now edit postmirror.sh to invoke the clean script:
sudo nano /var/spool/apt-mirror/var/postmirror.sh
Add this line:
Then make both scripts executable:
sudo chmod u+x /var/spool/apt-mirror/var/clean.sh
sudo chmod u+x /var/spool/apt-mirror/var/postmirror.sh
Finally, make apt-mirror an automated task:
sudo nano /etc/cron.d/apt-mirror
and remove the comment from the last line.

Moving to the client side:
Edit the sources.list file that will be used to access the mirror.
sudo nano /etc/apt/sources.list
The example below assumes using the mirror from the computer that hosts it. Using it from other computers on the network simply requires changing the IP address from to whatever IP address is assigned to the mirror host.
deb lucid main restricted universe multiverse
deb lucid-updates main restricted universe multiverse
deb lucid-backports main restricted universe multiverse
deb lucid-proposed main restricted universe multiverse
deb lucid-security main restricted universe multiverse
deb lucid partner
On a side note, I've seen it recommended a couple of places that you pull your security updates from their original source rather than a mirror. If you choose to do that you want to leave this line for lucid-security unchanged:
deb http://security.ubuntu.com/ubuntu lucid-security main restricted universe multiverse


Anonymous said...

This is a helpful article! I wanted to mention that you might consider revising the layout to make it more readable. Most of the pre-formatted code blocks are cutoff in the browsers I tried (IE, Safari, Firefox and Chrome). But I was able to read it by looking at the page source.


Anonymous said...

Hi, I hope this comment reaches you. regarding your post on the ubuntu repo. Have you got this system working well now?

I followed the instructions to the letter. When I run apt-get update on a client it states that some of the links are missing. Do I need to wait for the download of the repo to fully complete?

Also I checked one of the directories for an application to test install. libnotify for example. I saw the package there but when I issue apt-get install libnotify it states that the package cannot be found. Any ideas?