Introduction

This page describes the installation and configuration of apt-cacher. It was drawn from http://www.debuntu.org/how-to-set-up-a-repository-cache-with-apt-cacher and Ubuntu Hacks by Oxer, Rankin, and Childers http://www.oreilly.com/catalog/ubuntuhks/

Added by pablodav:In Newer installations like Jaunty I recommend use this cacher instead others like apt-proxy.

Warning: Using Apt-Cacher with more than one distribution

Because Debian and Ubuntu have identically named (but different) .deb packages in their repositories, it is unwise to setup a single apt-cacher to be a caching package server for both Debian and Ubuntu clients at the same time. A workaround for advanced users is to run two separate instances of apt-cacher on two separate ports with two separate caches.

Server Installation

1. Install apt-cacher and apache2 webserver

  •    sudo apt-get install apt-cacher apache2

2. Enable apt-cacher

  • Edit /etc/default/apt-cacher and change autostart to 1

    Note: You may have problems doing this on a machine with apache already installed.

3. Restart apache:

  •    sudo /etc/init.d/apache2 restart

4. Test apt-cacher by pointing your web browser to http://server:3142/apt-cacher to verify that it's running.

Server Configuration

1. Edit /etc/apt-cacher/apt-cacher.conf Uncomment the following line:

  • allowed_hosts = * Or set allowed_hosts to something appropriate.

    Apt Cacher now uses IPv6 addressing so you may find you need something like: allowed_hosts=::ffff:192.168.0.1/24, for example, if you want to restrict hosts to a common home subnet.

2. Change the admin_email email address to something meaningful.

3. By default apt-cacher may have problems with upgrades of ubuntu from one version to another when running do-release-upgrade -d This can be fixed by uncommenting the line beginning with installer_files_regexp and modifying it from:

  • installer_files_regexp = ^(?:vmlinuz|linux|initrd\.gz|changelog|NEWS.Debian|UBUNTU_RELEASE_NAMES\.tar\.gz(?:\.gpg)?|(?:Devel|EOL)?ReleaseAnnouncement(?:\.html)?|meta-release(?:-lts)?(?:-(?:development|proposed))?)$
    to:
    installer_files_regexp = ^(?:vmlinuz|linux|initrd\.gz|changelog|NEWS.Debian|[a-z]+\.tar\.gz(?:\.gpg)?|UBUNTU_RELEASE_NAMES\.tar\.gz(?:\.gpg)?|(?:Devel|EOL)?ReleaseAnnouncement(?:\.html)?|meta-release(?:-lts)?(?:-(?:development|proposed))?)$

    Note the addition of [a-z]+\.tar\.gz(?:\.gpg)? in the regexp

4. Restart apt-cacher:

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

Import existing cached Packages

Import any existing packages into the cache by creating symlinks:

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

Note, older distributions may have different options. Some require the -d option for symlinks. Jaunty may require -s for symlinks.

Load Packages on Server from CD

Around upgrade time, you may find it useful to get the CD image instead of using the slow update servers, and populate your cache using that.

1. Download your CD image(s) of choice.

2. Mount the CD image on the server running apt-cacher :

sudo mount -o loop /home/username_or_other_path/ubuntu-<image-version>-<image-type>-<arch>.iso /media/cdrom0

3. Run the import on the CD image, you need the -R is needed to recurse into the CD directory structure, the -r just makes sure they are copied to the cache instead of trying to link:

sudo /usr/share/apt-cacher/apt-cacher-import.pl -R -r /media/cdrom0

You should see the script saying that it is importing a lot of packages.

Configure the server's Apt process to run through the cache

In a terminal, type:

sudo nano /etc/apt/apt.conf.d/01proxy

Inside your new file, add a line that says:

Acquire::http::Proxy "http://<IP address or hostname of the apt-cacher server>:3142";

Save the file and then on the server run  apt-get update 

If that succeeds without errors then update your server with  apt-get dist-upgrade 

Once complete you may reboot the server if necessary and configure your other servers the same way. Note that this will not work well if the client you are configuring will change networks and not be able to communication with this Apt-Cacher server. See below for alternative options.

Client Configuration Options

There are two ways to configure your clients to use apt-cacher. 1. Modify your sources.list file 2. Alternatively (Recommended) configure Apt to use the proxy directly.

Added by pablodav: I don't recommend to change every line with step 1. In Jaunty /apt-cache does not works. Jump to step 2 for Client Configuration.

Method 1: Modify your sources.list

In client /etc/apt/sources.list insert apt-cacher-server:3142 in each line, i.e.

becomes

Note, depending on your version of Ubuntu you may have to remove the :3142 port number. If you are also still having issues you can also add /apt-cacher/ after the port number so that the line looks like this:

Method 2: Use as a proxy to APT

In a terminal, type:

sudo nano /etc/apt/apt.conf.d/01proxy

Inside your new file, add a line that says:

Acquire::http::Proxy "http://<IP address or hostname of the apt-cacher server>:3142";

Configuration Notes and Scripts

For Option 1: Modifying your sources.list

Note: This script is someone outdated. Your mileage may vary.

This bash script modifies client sources.list using server:3142 configuration.

Copy it, paste to a file on your client system ($HOME/somefilename.sh) and remember to make it executable:

chmod 744 $HOME/somefilename.sh

################################################################################
#  Name       : aptcachesources.sh
#  Version    : 1.0
#  Author     : ZComms
#
#  Copyright (c) 2010 ZComms
#
#  Released under the Creative Commons Attribution-ShareAlike 3.0 License.
#            http://creativecommons.org/licenses/by-sa/3.0/
# 
#  You are free to share and adapt the material, provided that you do so under
#  the same or similar license, and that you give credit to the original author.
#
#  The full text of the license can be found on the Creative Commons website.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#  
#  Special Notes:
#  This is one of the little 'tools' I use on my home network to make things a
#  bit easier after a new build or rebuild. I keep them on a flash drive.
#
#  The license is larger than the script - please use the URL above to get a 
#  copy for yourself. Feel free to change this for your own use, but not so free
#  to ask me to change it for you free of charge.
# 
################################################################################
# exit codes
# 0 - all is good, no errors
# 1 - general error, unspecified
# 2 - misuse of shell builtin - hardly ever used
# 3 - Bad environment variable
# 4 - Bad command line argument
# 5 - Missing file or directory
# 6 - Failed OS utility
# 255 - Dave, I can't do that.

function show_use {
    echo -en "\n\nVersion 1.0 - 2011\n"     # set version here
    echo -en "Running this script from your home directory is best\n\n"
    echo -en "Usage: aptcachesources.sh -h -v/V -t"
    echo -en "Where s=proxy server p=port f=local aptsources file\n"
    echo -en "Where -u is update to new source and -r is revert to default configuration\n\n"
    echo -en "Example:   <-u|r> -s 212.121.21.117 -p 3142 -f /etc/apt/sources.list\n\n"
}

function show_help {
    echo -en "This file allows you to set apt to use an apt proxy server. You need to specify\n"
    echo -en "your apt sources.list file, the proxy server IP/name and the proxy port number.\n"
    echo -en "It was written to ease setup for multiple systems pointing to the same proxy but\n"
    echo -en "can be used to swap out 'http://' in any file as that is all it really does.\n\n"
    echo -en "This will leave a backup file in the directory where the script resides.\n\n"
    echo -en "For more information, see man page for apt-cacher or the \n"
    echo -en "Ubuntu info: https://help.ubuntu.com/community/Apt-Cacher-Server\n"
}

START_DIR=`pwd`
NOARG=0
MODE=9
SRC_HOST="ERROR"
SRC_FILE="ERROR"
SRC_PORT="ERROR"
TEST=0

# lets figure out if the user has put enough valid arguments on the command line
if [ $# -eq $NOARG ]
then
    show_use           # explain usage
else
    while getopts ":urthHvVs:p:f:" OPT
    do
        case $OPT in 
            u ) MODE=1;;
            r ) MODE=0;;
            s ) SRC_HOST=$OPTARG;;
            p ) SRC_PORT=$OPTARG;;
            f ) SRC_FILE=$OPTARG;;
            t ) TEST=1;;
            h | H | v ) show_help
                exit 0
                ;;
            V ) cat $0 | grep 'set version here' | grep -v cat | awk '{print $4}'
                exit 0
                ;;
            * ) echo -en "\n\tERROR: Command line option not understood ($OPT)\n\n"
                show_use
                exit 4
                ;;
        esac
    done
fi

# Only two modes - make the change, or reverse it (backout)
#
if [[ $MODE = 1 ]]
then
    if [[ $TEST = 1 ]]
    then
        cat $SRC_FILE | sed -e 's%http://%http://'$SRC_HOST':'$SRC_PORT'/%' | less
        exit 0
    else    
        cat $SRC_FILE | sed -e 's%http://%http://'$SRC_HOST':'$SRC_PORT'/%' > $START_DIR/sources.new
        cp $SRC_FILE $SRC_FILE.original
        cp $START_DIR/sources.new $SRC_FILE
        if [[ -e $SRC_FILE ]]
        then
            cmp ./sources.new $SRC_FILE
            if [[ $? > 0 ]]
            then
                echo -en "\n\tThere was a problem: could not copy file $START_DIR/sources.new to $SRC_FILE"
                exit 1
            fi
            echo -en "\n\tSuccessfully modified sources.list file. See $START_DIR/sources.new\n"
            exit 0
        fi
    fi
fi

if [[ $MODE = 0 ]]
then
    if [[ $TEST = 1 ]]
    then
        cat $SRC_FILE | sed -e 's%http://'$SRC_HOST':'$SRC_PORT'/%http://%' | less
        exit 0
    else
        echo "oops"
        cat $SRC_FILE | sed -e 's%http://'$SRC_HOST':'$SRC_PORT'/%http://%' > $START_DIR/sources.nocache
        sudo cp $SRC_FILE $SRC_FILE.backout
        sudo cp $START_DIR/sources.nocache $SRC_FILE
        if [[ -e $SRC_FILE ]]
        then
            cmp $START_DIR/sources.nocache $SRC_FILE
            if [[ $? > 0 ]]
            then
                echo -en "\n\tThere was a problem: could not copy file $START_DIR/sources.nocache to $SRC_FILE"
                exit 1
            fi
            echo -en "\n\tSuccessfully modified sources.list file. See $START_DIR/sources.nocache\n"
            exit 0
        fi
    fi
fi 

Then run

sudo apt-get update

Configuring Option 2 for "Roaming" mode

This method is useful if you are alternating between office and home with a laptop for example. It involves using the ping command to determine if the apt-cacher server is available at boot-time and then configure the APT proxy or not.

  • Open /etc/rc.local (alt-F2, "gksu gedit /etc/rc.local")
  • Change the top from "#!/bin/sh -e" to "#!/bin/bash"
  • put this near the end (before "exit 0" if present) replacing "SERVER_NAME_HERE" with your server's resolvable name or it's IP:

echo "Sleeping 10 seconds..."
sleep 10s
echo "Ready"
. /lib/lsb/init-functions
log_daemon_msg "Configuring APT cache proxy" "(based on SERVER_NAME_HERE's presence...)"
ping -c 1 SERVER_NAME_HERE &> /dev/null
if [ $? = "0" ]; then
  echo "Acquire::http::Proxy \"http://SERVER_NAME_HERE:3142\";" > /etc/apt/apt.conf.d/01proxy
else
  rm /etc/apt/apt.conf.d/01proxy &> /dev/null
fi
log_end_msg 0

Configuring Option 1 for "Roaming" mode, alternative solution

Added by "nuaimat":

The first 'Roaming Mode' method didn't work for me, so I created two sources.list versions, one named sources.list.home and other one named sources.list.work (both inside /etc/apt) I modified sources.list.home as mentioned above (Static configuration Section) then I login as root

   sudo su -

then created a shell script (don't forget to replace "SERVER_NAME_HERE" with your server's resolvable name or it's IP ) :

. /lib/lsb/init-functions
log_daemon_msg "Configuring APT cache proxy" "(based on SERVER_NAME_HERE's presence...)"
unlink /etc/apt/sources.list

ping -c 1 SERVER_NAME_HERE &> /dev/null
if [ $? = "0" ]; then
 log_daemon_msg "found live $?"
 ln -s /etc/apt/sources.list.home /etc/apt/sources.list &> /dev/null

  for i in /etc/apt/sources.list.d/*.list; 
  do 
  local_online=`grep "SERVER_NAME_HERE" $i|wc -l`
  if [ $local_online = "0" ]; then
   sed -i 's|http://|http://SERVER_NAME_HERE/apt-cacher/|g' $i &> /dev/null
  fi;
 done;
else

 log_daemon_msg "not found"
 ln -s /etc/apt/sources.list.work /etc/apt/sources.list &> /dev/null
 
 for i in /etc/apt/sources.list.d/*.list; 
 do 
  sed -i 's|http://SERVER_NAME_HERE/apt-cacher/|http://|g' $i &> /dev/null
 done;
fi


  


log_end_msg 0
  • save it as /root/check-apt-cacher
  • chmod +x /root/check-apt-cacher
  • crontab -e
  • using your favourite editor add the following line to the crontabs

*/1 * * * * /bin/bash /root/check-apt-cacher

this script will run each minute (*/1) you can change it as you see fit.

"Roaming" mode event-driven solution

I didn't like the idea of using cron to poll the network intermittently and the first roaming solution simply didn't work for me, so here's a different one I came up with.

This assumes:

  • that you are running apt-cacher-ng (ACNG) and not just apt-cacher, but it should work with minor edits with a vanilla apt-cacher too
  • ACNG is running both on localhost on the roaming client and in some of the networks you connect to
  • apt is configured to use localhost on the roaming client
  • ACNG on the roaming client is the right place to direct proxy traffic

The same if-up.d trick below could be modified to simply edit a /etc/apt/apt.conf.d/01proxy file as described in the first roaming solution.

To make it event driven I've used if-up.d which can run a script any time a new network interface comes online.

Create a new file /etc/network/if-up.d/apt-cacher-ng-reset-proxy and put the following script in it:

#!/bin/bash

# list of hosts that the proxy might be running on
hosts=(
        acng.on.my.home.network
        acng.on.my.work.network
        acng.at.my.friends.place
)

set_host() {
        host=$1
        line="Proxy: http://$host:3142/"
        if [[ -z $host ]]; then
                line="# Proxy: disabled because none are contactable"
        fi

        # adjust ACNG configuration to use supplied proxy
        sed -i -r "s%^\s*(#|)\s*Proxy: .*$%$line%g" \
                /etc/apt-cacher-ng/acng.conf

        # if apt-cacher-ng is running
        if service apt-cacher-ng status > /dev/null 2>&1; then
                # restart it to take hold of new config
                service apt-cacher-ng restart
        fi
        exit 0
}

try_host() {
        host=$1
        # if we can get to the supplied host
        if ping -c 1 "$host" > /dev/null 2>&1; then
                # tell ACNG to use it
                set_host "$host"
        fi
}

# Run through all possible ACNG hosts trying them one at a time
for host in "${hosts[@]}"; do
        try_host "$host"
done

# no proxies found, unset upstream proxy (i.e. we connect straight to the internet)
set_host

Make sure to adjust the script for your environment and make it executable with:

chmod +x /etc/network/if-up.d/apt-cacher-ng-reset-proxy


CategoryPackageManagement CategoryPackageManagement

Apt-Cacher-Server (last edited 2014-02-24 08:10:53 by pancro)