Use apt-cacher to save on bandwidth on multiple PCs

| | 4 min read

When you have more than one system with Ubuntu installed, and you have to install same or similar set of packages on all your PCs, you can save on a lot of bandwidth if you had some ways of sharing the downloaded packages and updates. There is an easy way to do this in Ubuntu. This is done using apt-cacher a proxy server for your package repositories. It is very easy to set up and configure apt-cacher on a PC with Ubuntu inside your LAN.

When fully installed and configured, apt-cacher runs as a server on your intranet and all the PCs in your LAN will query the apt-cacher for any kind of package requirements and apt-cacher will in turn get the package from its local cache, if the package was already downloaded in a previous request, or from the internet repositories if the package is a new package not already requested. The bandwidth savings are directly proportional to the number of PCs in the network running Ubuntu (or any other debian variant)

The following set of information will assume that you have a network of PCs with Ubuntu installed on them and that you have internet access from your network.

First select a computer with enough disk space and which you would like to set up as your local package repository. On this PC install apt-cacher
sudo apt-get install apt-cacher
This will install apt-cacher on the PC. Now the next task is to configure this. The configuration file is located at /etc/apt-cacher/apt-cacher.conf. Create a backup copy and open the file for editing
sudo cp /etc/apt-cacher/apt-cacher.conf /etc/apt-cacher/apt-cacher.conf.bak
sudo gedit /etc/apt-cacher/apt-cacher.conf

By default apt-cacher allows access from any IP address. But if you want to restrict access only to the PCs in the intranet (which is a safe option) set the allowed_hosts parameter to
allowed_hosts=192.168.1.0/24
There are two parameters 'group' and 'user' which sets the group and user for the apt-cacher application. If you comment these the application will run as the native user. In any case make sure that the application has write access to the cache directory set by the cache_dir parameter in the file. If you would like to keep all the packages downloaded you can set clean_cache parameter to 0 to prevent automatic deletion of unreferenced packages.

The last and the most important setting in the configuration file is the path_map parameter. This parameter maps the online repositories to local virtual repositories. These have to be given meaningful names so that the repository can be easily identified from the client machine in the intranet.
path_map = ubuntu archive.ubuntu.com/ubuntu; ubuntu-updates archive.ubuntu.com/ubuntu; ubuntu-security security.ubuntu.com/ubuntu; wine wine.budgetdedicated.com/apt; medibuntu packages.medibuntu.org;

If you want to add any other repository other than what is mentioned above you can very easily add it to the above parameter. For example if you want to add the skype repository which is at http://download.skype.com/linux/repos/debian you can append
skype download.skype.com/linux/repos/debian;
to the end of the above path_map parameter to do that.

Now edit /etc/default/apt-cacher and set AUTOSTART to 1 to enable apt-cacher to boot at system startup.
Restart apt-cacher by running
sudo /etc/init.d/apt-cacher restart
Test your apt-cacher installation by accessing
http://localhost:3142
in your browser. If apt-cacher is running you will see the report when you access this URL.

You can now use a Ubuntu CD/Ubuntu DVD and copy all the packages from the CD/DVD into the apt-cacher cache. Replace '/media/UbuntuCD' with whatever is the correct path where the CD or the DVD is mounted.
sudo /usr/share/apt-cacher/apt-cacher-import.pl /media/UbuntuCD

Once apt-cacher is configured on the local package repository machine it is time to update the sources.list on all the client machines in the intranet to use the local cache. On each PC create a copy of the original sources.list and edit the file to add the local repository.
sudo cp /etc/apt/source.list /etc/apt/sources.list.bak
sudo gedit /etc/apt/sources.list

Copy and paste the following code into sources.list and replace <YourLocalRepositoryMachineIP> with the IP address of the local package repository that you have configured

## Ubuntu Packages
deb http://[YourLocalRepositoryMachineIP]:3142/ubuntu/ gutsy main restricted multiverse universe
deb-src http://[YourLocalRepositoryMachineIP]:3142/ubuntu/ gutsy main restricted multiverse universe

## Ubuntu Major Bug fixes
deb http://[YourLocalRepositoryMachineIP]:3142/ubuntu-updates/ gutsy-updates main restricted multiverse universe
deb-src http://[YourLocalRepositoryMachineIP]:3142/ubuntu-updates/ gutsy-updates main restricted multiverse universe

## Ubuntu Backports
deb http://[YourLocalRepositoryMachineIP]:3142/ubuntu/ gutsy-backports main restricted universe multiverse
deb-src http://[YourLocalRepositoryMachineIP]:3142/ubuntu/ gutsy-backports main restricted universe multiverse

## Ubuntu Security
deb http://[YourLocalRepositoryMachineIP]:3142/ubuntu-security/ gutsy-security main restricted multiverse universe
deb-src http://[YourLocalRepositoryMachineIP]:3142/ubuntu-security/ gutsy-security main restricted multiverse universe

## Other Repositories
deb http://[YourLocalRepositoryMachineIP]:3142/wine gutsy main
deb http://[YourLocalRepositoryMachineIP]:3142/medibuntu gutsy free non-free

Test the apt-cacher setup by running
sudo apt-get update
on the client machine where the sources.list have been updated. If everything is fine copy the modified sources.list to all the client machines in the intranet which has Ubuntu on them.

If you have any problems in setting this up, please use the comment form given below to submit your questions and we will try to help you in whatever way we can.