Howto: apt-cacher with ubuntu

If you have an office with loads of linux computers, it might be useful to have a local repository to save bandwidth on you local wan. But a local repository needs to be up-to-date and it’s hard to decide which packages should be mirrored. If you want to have a good and useful mirror you’ll probably waste a lot of hard drive space and also a lot of your wan bandwidth updating never used packages.

The better and easier solution is a local apt cache. It’s easy to set up and only needed packages are cached. I use this setup to serve about 15 ubuntu clients. My saving in wan traffic is more than 85%.

How to do it?

Server setup

You need a server with some free disk space, about 10gig should be enough if all your clients are using the same ubuntu version.

Install the package:

sudo apt-get install apt-cacher

Enable apt-cacher at boot time

sudo sed -i s/AUTOSTART=0/AUTOSTART=1/ /etc/default/apt-cacher

Thats all you need to do on the server. Maybe you want to change some more options, like your email address, then just open the following file. In most use cases it’s not necessary to change more config options.

vim /etc/apt-cacher/apt-cacher.conf

[smartads]

Client setup

There are different ways to tell your client computers to use your new apt-cache. An easy way is just to tell them to use a proxy for connecting to the repo. So there is no change with your apt-sources needed.

Add the following line

Acquire::http::Proxy "http://{IP or HOSTNAME of the apt-cacher server}:3142";

to the file

/etc/apt/apt.conf.d/01aptproxy

replace “{IP or HOSTNAME of the apt-cacher server}” with the IP or hostname of your apt-cache server.

sudo echo 'Acquire::http::Proxy  \
  "http://{IP or HOSTNAME of the apt-cacher server}:3142";'  \
  >  /etc/apt/apt.conf.d/01aptproxy

for example:

 sudo echo 'Acquire::http::Proxy "http://192.168.0.2:3142";'  \
  >  /etc/apt/apt.conf.d/01aptproxy

Now your client setup is done. Test your config.

sudo aptitude update

If there are no connection errors, your setup is okay.

Statistics

To check your bandwidth savings, apt-cacher has a nice build in tool. Just open a webbrowser an go to the following url. Replace {IP or HOSTNAME of the apt-cacher server}.

http://{IP or HOSTNAME of the apt-cacher server}:3142/apt-cacher/report
e.g. http://192.168.0.2:3142/apt-cacher/report

This is an example screen shot, of the apt-cacher statistics. You can see i used about 6.5 gig WAN traffic and had about 66 gig LAN traffic. The saving is significant. This setup serves only ubuntu 10.04/lucid computers and needs about 4gig HDD space on the server.

du -sh /var/cache/apt-cacher/ 
3.8G    /var/cache/apt-cacher/