Speed up multiple debian / ubuntu updates with apt-cacher

Today I upgraded from ubuntu 7.04 to 7.10, and it was 1.4GB. This is a lot, even with fast DSL, given that you will most probably need to do it more than once (desktop, laptop, office etc.). This time I chose to do it more cleverly: using a local cache. Apt-cacher to the rescue. But first, what are the necessary steps?

  1. Have an apache webserver ready
  2. apt-get install apt-cacher on that machine (your server)
  3. Configure apt-cacher (see below)
  4. Configure client machines (see below)

About 1 and 2: Apt-cacher can actually also run in stand-alone mode without the need for an apache server, but I'm not going to cover this here. In fact, I believe that the apache-mode is much nicer (see later).

About 3: Tweak /etc/apt-cacher/apt-cacher.conf to your needs. The default settings are usable out of the box and everything is very well documented in there. Very nice: There is an option for rate limiting and external http proxy configuration (even more redundancy!).

About 4: Now for the easy part: Modify you clients' /etc/apt/sources.list files. Here is how the result should look:

Original line:
deb http://ftp.de.debian.org/debian stable main contrib non-free
Apt-cacher enabled replacement:
deb http://cacheserver/apt-cacher/ftp.de.debian.org/debian stable main contrib non-free

Easy!

But now for the brain part:
Q: How to do this automagically for all entries with one line of sed?
I have two possible answers for you:

About A:
sudo su
cd /etc/apt
cp sources.list sources.list.pre-caching
export APTCACHER="your.server.name"
sed -i "s/\(http:\/\/\)\([^\/]*\)\//\1$APTCACHER\/apt-cacher\/\2\//" sources.list

About B:
Same as A, but
sed -i "/^[^#]/h;s/\(http:\/\/\)\([^\/]*\)\//\1$APTCACHER\/apt-cacher\/\2\//;/^[^#]/{x;p;x}" sources.list

Note that it is not a good idea to execute the sed commands more than once!

For the curious:
Explanation of the regular expression in A:
Explanation of the regular expression in B:
I like regular expressions.


This page is powered by Blogger. Isn't yours?