Tuesday, January 3, 2012

How To Set up a repository cache with apt-cacher


By Pallavi Shastry

When running multiple machine with the same distribution, it is sensible to set up a repository cache on your network so that once a package is downloaded from an official repository, all other machines will download it from your local area network.
Having different machines running the same linux distribution, it becomes interesting to set up a repository cache in your local network. This way, you won't download common packages more than 1 time from official repositories.
Here is the situation, we have one machine called repository-cache, this machine is going to act as the repository cache, basically, any other machines in your network is going to use it as a repository.



1. Getting started : Server configuration

As usual, you need to install the required packages in the first place. So type in a terminal:

$sudo apt-get install apt-cacher apache2

To start the service of the apache web server. 
 $sudo /etc/init.d/apache2 start
 
Then test by going to http://repository-cache:3142/apt-cacher to verify that it's running. 
repository-cache in the URL above is the host name or IP of the cache server in the LAN. 

Once this is done, it is time to get into the configuration files in /etc/apt-cacher/apt-cacher.conf

$sudo gedit /etc/apt-cacher/apt-cacher.conf

The default port apt-cacher is running on is port 3142.

Allowed_hosts: by default, all host are allowed to use the repository cache. You can change this value if you want to only allow certain host.

Generate_reports: This directive makes apt-cacher create a report on how efficient your cache was on a daily basis. Default is 1, if you want to disable this, set it to 0.apt-cacher will generate report on the usage of the cache every day.

Clean cache: This directive makes apt-cacher clean the cache every 24 hours . So reset it , clean cache =0


2. Activating apt-cacher to start

In order to start, apt-cacher needs to be activated from /etc/default/apt-cacher. So open /etc/default/apt-cacher and set AUTOSTART to 1:
AUTOSTART=1
Now restart apt-cacher:
$sudo /etc/init.d/apt-cacher restart
Now that apt-cacher runs, it is time to update all our clients /etc/apt/sources.list files so every host on the network will use our repository-cache machine.


3. Importing existing package from /var/cache/apt/archives/ to apt-cacher repository

It might happen that your server already has got a whole lot of packages cached in its local repository: /var/cache/apt/archives/.  
apt-cacher offers a tool to import those files to apt-cacher repository logically without in fact replicating copies of these packages.
There are whole lot of cool and useful scripts that can be found in /usr/share/apt-cacher/
The one we are interested in here is apt-cacher-import.pl which is handy to import deb files from /var/cache/apt/archives to apt-cacher repository run:

$sudo /usr/share/apt-cacher/apt-cacher-import.pl -s /var/cache/apt/archives


5. Setting up the Clients sources.list

Now it is time to set up the client hosts apt source list files: /etc/apt/sources.list
It make sense to use the repository cache on the server too, as that way, any updates made by the server will fill up the cache.

In each line , ie every instance of the sources.list file, if for example there is an entry

deb http://archive.ubuntu.com/ubuntu/ main restricted

becomes

deb http://repository-server:3142/apt-cacher/archive.ubuntu.com/ubuntu/ main restricted

Cool, now, every host should be able to retrieve the .deb packages from our repository cache once has been ran on every host.

$ sudo apt-get update

Then test report by going to http://repository-cache:3142/reports to verify that it's running. The report is generated once a day, but if you need it at a certain time, you can force it by running:

$sudo /usr/share/apt-cacher/apt-cacher-report.pl


6. Conclusion

apt-cacher is an easy and efficient package which will save you both time and bandwidth when using multiple machines with the same distribution like it could happen in a home network or at a company.