HOWTO: Apt-Cacher

04Jan10

I have four computers running Ubuntu, two 9.10 servers, and two desktops running 9.04. Last night, both of my servers needed 50Mb of updates, and I wondered, why bother downloading two different sets for both of them? Which is when I stumbled onto Apt-Cacher, which is basically a caching proxy server for Apt, and exactly what I wanted. With the help of this post on Ubuntu Forums, I set up my main server to act as a proxy for the rest of the computers in the house. And here, is how its done :)

Install apt-cacher with

 sudo apt-get install apt-cacher apache2

apache2 is installed so that you can see some stats on Apt-Cacher if you go to the proxy’s IP and port 3142, I’ll explain that later.

Ok, now to get the proxy running at boot, edit your services file

sudo nano /etc/services

and add the following to the bottom

apt-cacher           3142/tcp
apt-cacher           3142/udp

3142 is the default port, and there’s probably no need to change it. Exit nano with Ctrl+X and press Y to save. Finally edit this

 sudo nano /etc/default/apt-cacher<

and set AUTOSTART to 1. Thats it, apt-cacher will now run at startup, so now either reboot or use

 sudo /etc/init.d/apt-cacher restart

to get running straight away.

At this stage, you may want to change a few of the default options, so open up apt-cacher’s config file with

sudo nano /etc/apt-cacher/apt-cacher.conf

The only two things here that I’m going to explain are the two I needed to change cache_dir and admin_email. I changed from the default location, which is on my very small / drive, to my larger drive mounted as /home. Also, I changed admin_email really just to be correct, its not essential.

Next thing to do to ease along the transition is to add all the packages your server has already download to the cache, you don’t want to leave all that go to waste like. To do that, run the following,

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

You now have a fully functinal, fully loaded apt-cacher server. But whos going to connect to it? You need to set the server as a proxy for apt and Synaptic for the clients. To do so, run

 echo 'Acquire::http::Proxy "http://hostname:3142";' | sudo tee /etc/apt/apt.conf.d/01proxy

replacing hostname with the hostname or IP of your server. You should also use this on your server, replacing hostname with localhost. That way, any packages your server downloads will be added to the cache and available to the clients.

To set the proxy in Syanptic open up Synaptic (System > Administration > Synaptic) then select Settings > Preferences. Select the Network tab, select Manual Proxy Configuration and enter the hostname or IP of your server, and the port, 3142. Click Apply and Ok and your done.

Thats it, you’ve set up your own Apt-Cacher server. See the post mentioned at the top for a few other details, including a roaming mode for laptop users.

Just a note, this server can be used for different releases, mine is working for both Jaunty and Karmic. However, if you download a package on your Karmic Server and then attempt to download it on your Jaunty client, the Jaunty client will need to download the package again. This is because they use different repositories. Its probably the exact same, but it treats it as being different, and rightly so.

Here’s hoping that this is clear and useful. Leave a comment if you’ve questions :)

About these ads


10 Responses to “HOWTO: Apt-Cacher”

  1. Nice tutorial, i should try it soon……..

  2. 3 Toni

    Hi,
    really like the guide. Now I just have a tiny question and I am not sure if you are able to help.
    So my server is part of a big network, which uses a proxy to access the outside world. But i want that server to be an apt-cacher server for a little subnetwork of computers, which all don’t have access to the outside world, not even through the big proxy.how to I make the apt cacher use a proxy to access repositories?
    My server has this line in its 01proxy file:
    Acquire::http::Proxy “http://localhost:3142″;
    which should be correct, as i want to use localhost right?
    This is the error I get when I am trying to use apt? e.g. apt-get update
    E: Syntax error /etc/apt/apt.conf.d/01proxy:2: Extra junk at end of file
    obviously there is something wrong with the syntax as well as I am not sure if it is using the proxy settings of the big network. Any ideas?
    Thanks

    • 4 evidex

      What I prefer to do to tell computers to use Apt-Cacher is to change each line in the /etc/apt/sources.list file. For example “deb http://ie.archive.ubuntu.com/ubuntu/ karmic restricted main” would change to “deb http://server_IP_or_Hostname:3142/ie.archive.ubuntu.com/ubuntu/ karmic restricted main”. I’ve had no problems setting it up that way, as opposed to using the 01proxy file, which was always dodgy for me. The only downside of this is that you’ll have to change each of the computers that are going to access the Apt-Cacher proxy.

      One other thing I’d recommend is to have a look at Apt-Cacher-NG. It’s a lot quicker and easier on resources than Apt-Cacher. There are some slight differences, but also improvements.

  3. 5 Peter

    I got as far far as the echo command. When I do your suggested command

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

    it returns

    ‘Acquire::http::Proxy “http://10.0.0.30:3142″
    ’: command not found

    Whereby 10.0.0.30 is the ip address of my computer with the apt-cacher directory.

    So, what’s wrong and how to correct this?

    Thanks in advance

    • 6 evidex

      There shouldn’t be an apostrophe [ ‘ ] before Acquire, as far as I know. You can edit the file by through gedit with

      gksudo gedit /etc/apt/apt.conf.d/01proxy

      That said, Apt-Cacher-NG is a much superior program to Apt-Cacher. I intend doing a post on the process needed for that, so check back :) In the meantime,this link should help

  4. 7 inurbase

    I run into the following problem right from the first step (the downloading of apt-cacher)

    E: Syntax error /etc/apt/apt.conf.d/01proxy:2: Extra junk at end of file

    I am running Ubuntu 10.10 with my internet connection through Proxoid on an Android phone. I have no problems with the internet connection, however I am unable to download any updates through the proxy server and unfortunately have no access to LAN internet that doesn’t run through its own proxy.

    Suggestions?

    • 8 evidex

      Could you post up the contents of your /etc/apt/apt.conf.d/01proxy file? There seems to be a problem with the syntax on line two, which I could perhaps fix.

      Apt-Cacher-NG is a much superior program to Apt-Cacher, much lighter on system resources. I’ll be doing a post on how to set it up this week, but in the mean time, this link should provide all the information you need. http://ubuntuforums.org/showthread.php?t=981085

      The fact that you can’t download apt-cacher through your internet normally, without having followed down to the last step of the blog, would make me think that apt is already set up to route through a proxy (possibly your Droid?)

  5. I don’t believe you need to do ANYTHING in Synaptic. Synaptic appears to pass it’s commands to apt and so the 01Proxy will route the Synaptic requests. I’ve had apt-cacher running since ’07 and my access log shows the hits from other computers on the LAN when installing stuff via Synaptic.

    • 10 evidex

      That’s quite possible, but you can set it up either way. Synaptic might even write that file if you give it values, I’m not sure.



Follow

Get every new post delivered to your Inbox.

Join 69 other followers

%d bloggers like this: