Creating a local/network accessible Ubuntu package repository

A recently I have been playing around with Ubuntu Server 10.04 and various little projects etc. I thought I’d write a tutorial on how you can create a network accessable or local Ubuntu package repository.

For this tutorial I am using Ubuntu 10.04 Server release on an old Compaq Proliant DL320.

The sole purpose of having such a setup is to enable all your Ubuntu machines (Desktops and Server) to be able to update and install software packages using a local server which saves your internet bandwidth 🙂

I assume that you already have Ubuntu installed and ready to go…

So lets get started…

Firstly we need to install apt-mirror, apt-mirror is the software that we will use to syncronise the data from the internet package repository servers on to our own server.

So lets install it now…

apt-get install apt-mirror

After the package has downloaded you can now move on to the part where we will edit the configuration file for apt-mirror so we can add or remove other versions of Ubuntu, artitectures and/or repository types…

nano /etc/apt/mirror.list

By default the file will show you that it will get both the binaries (deb …..) and source (deb-src….) packages for main, restricted and universe.

I want to mirror the main, restricted, and universe sections of Ubuntu Lucid Lynx, but I don’t want to mirror the security updates as these should be downloaded directly from the internet. Also, I don’t want to mirror the source packages because in 95% of all installations you don’t need source packages, and they need a lot of space on the hard disk. So my /etc/apt/mirror.list looks like this:

############# config ##################
#
# set base_path    /var/spool/apt-mirror
#
# set mirror_path  $base_path/mirror
# set skel_path    $base_path/skel
# set var_path     $base_path/var
# set cleanscript $var_path/clean.sh
# set defaultarch  <running host architecture>
# set postmirror_script $var_path/postmirror.sh
# set run_postmirror 0
set nthreads     20
set _tilde 0
#
############# end config ##############

deb http://archive.ubuntu.com/ubuntu lucid main restricted universe
#deb http://archive.ubuntu.com/ubuntu lucid-security main restricted universe multiverse
#deb http://archive.ubuntu.com/ubuntu lucid-updates main restricted universe multiverse
#deb http://archive.ubuntu.com/ubuntu lucid-proposed main restricted universe multiverse
#deb http://archive.ubuntu.com/ubuntu lucid-backports main restricted universe multiverse

#deb-src http://archive.ubuntu.com/ubuntu lucid main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu lucid-security main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu lucid-updates main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu lucid-proposed main restricted universe multiverse
#deb-src http://archive.ubuntu.com/ubuntu lucid-backports main restricted universe multiverse

clean http://archive.ubuntu.com/ubuntu

Make note that I have removed multiverse from the end of the the first repository path and commented out ‘#’ the other repository paths.

Thats pretty much it! – Now you can run the apt-mirror tool using the following command:-

apt-mirror

The first time you run this it’ll take ages to download the inital data but each time after you’ll be able to run apt-mirror which will then update any new packages or cleanup ones that are no longer required so therefore will be much much faster! At the time of writing, the inital mirror to download was 28.3GB (29,434 files)

Making it download packages automatically

If you want to automate the task of the server downloading new and updated packages automatically you can add it as a daily cron job, sSimply edit /etc/cron.d/apt-mirror and uncomment the last line in it so that it looks like this:

nano /etc/cron.d/apt-mirror

#
# Regular cron jobs for the apt-mirror package
#
0 4 * * * apt-mirror /usr/bin/apt-mirror > /var/spool/apt-mirror/var/cron.log

This will make apt-mirror run at 4.00h each day, and it will log to /var/spool/apt-mirror/var/cron.log.

Enabling access to your repository over HTTP

Cherokee Server is a small and very fast, simple to manage and configure using a web configuration tool and traffic reports (run cherokee-admin from the console!), You can install it now using the following command:-

apt-get install cherokee libcherokee-mod-rrd

Or if your happy with Apache, the well known and very widely used, you can install it using:-

apt-get install apache2

So after you’ve choosen what Web server you want to use, lets move onto the next step…

The default web hosting document root under Ubuntu is /var/www, but our repositories are stored in /var/spool/apt-mirror/mirror. In order to make our repositories accessible by http://(server_ip)/ubuntu, we create a symlink for /var/www/ubuntu that point to the real repository:

ln -s /var/spool/apt-mirror/mirror/archive.ubuntu.com/ubuntu /var/www/ubuntu

Remember to adjust these commands if you use different mirrors than archive.ubuntu.com.

Thats it…. (on the server side!)

Configuring your client PC’s/Servers

To use the local ubuntu mirror you can replace the main restricted universe repository in /etc/apt/sources.list like this:

nano /etc/apt/sources.list

You should change the hostname to your server’s IP address simular to this…

[…]
deb http://192.168.0.100/ubuntu/ lucid main restricted universe
[…]

Save the changes and then run apt-get update to confirm you are now using the local mirror server:-

apt-get update

and your done!!!! – If you want to learn more about apt-mirror search it on Google!