Monday, 18 May 2009

HOWTO: Using apt-cacher-ng to cache packages

If you have a lot of machines to manage, you don't want them all reaching out to the internet to retrieve updates and using up your precious bandwidth. What you want is a local machine that can keep copies of all the files that your machines download from the repositories so that the next machine that asks for the same file can just be given the copy that has already been downloaded. You could of course use squid, but squid is a less than perfect solution as it is not designed to ensure files are kept on hand until they are no longer needed.

apt-cacher-ng is a purpose built application that keeps track of all the packages that have been downloaded. If a client requests some-package.v123.deb it checks to see if it is already cached locally and if so it provides it to the client.

If the package has not previously been requested, it then downloads the package, provides it to the client, adds it to the cache and, here is the important part, deletes all older, outdated versions of that file!

It will keep cached files indefinitely, unlike squid which is designed to flush files if they remain unrequested for a predefined period.

So, to use apt-cacher-ng, your /etc/apt/sources file doesn't need to be modified or configured in any special way. Just leave it configured as you would normally (in my case these are the official aus repos with the source repos removed);

Example;

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


To install apt-cacher-ng, simply do;

sudo apt-get install apt-cacher-ng

And that is it, there is no further configuration needed!

For each client however, you do need to make a few small changes;

Create (or edit) the file /etc/apt/apt.conf and add the following line (substituting the IP address for the address of your server of course!)

Acquire::http { Proxy "http:10.1.1.1:3142"; };

If you use Synaptic, you also need to modify another file;

sudo vi /root/.synaptic/synaptic.conf

Add the following lines to the end but before the final brace (ie before the " }; ')

useProxy "1";
httpProxy "10.1.1.1";
httpProxyPort "3142";
ftpProxy "10.1.1.1";
ftpProxyPort "3142";
noProxy "";


For the changes to be applied you need to do an apt-get update;

sudo apt-get update

If this completes without error you are done! It is that easy.

No comments: