Apt-Cacher

Apt-Cacher is a caching proxy specialized for package files from GNU/Linux distributors, primarily for Debian and Debian based distributions (like Ubuntu). This means that if you have many Debian/Ubuntu machines on your network, you just have to download the deb packages (new software, updates, distro upgrades, etc) from the internet once and the rest of the Debian/Ubuntu machines get them from Apt-Cacher cache, saving time and internet bandwidth.

Apt-Cacher Pools

Pools are where installation packages (software) are stored.

/var/cache/name-repo/pool/

How to add a Package to Apt-Cacher

Locate the Package deb file:

1. Either download the deb file from the vendor or if the package has been installed on the local server, you will find the deb file here: /var/cache/apt/archives/.

2. Copy the deb file to the pool. For all environments you can use the all pool: /var/cache/name-repo/pool/all/

note: you need to make sure that the user:group is correctly www-data:www-data.  Use chown.  Also, set the chmod to 664.

3. Retrieve the package metadata:

cd /var/cache/name-repo/pool/all/
dpkg-scanpackages .

4. Add the package metadata to the Packages.gz file

sudo vim /var/cache/name-repo/dists/name/all/binary-amd64/Packages.gz

5. Make sure the filename is located in the pool folder, and the permissions are correct:

# example file path
Filename: pool/all/sensu_1.0.0-1_amd64.deb
# the example can be found in our repo
ls -l /var/cache/name-repo/pool/all/|grep sensu

6. Test the new package:

# update the package list
sudo apt-get update
# test the package to make sure it is available
apt-cache policy sensu

Find if Package Exists in Apt Repository

Check if Package Exists in Apt Repo

You should run sudo apt-get update first.

apt-cache search --names-only '^sensu.*'

Simulate an Installation

if sudo apt-get –simulate install sensu
then echo “Package exists.”
else echo “Cannot find Package.”
fi

List Installed Packages

dpkg -l | less

To check whether a package is installed or not:

dpkg -l vlc

How to Install Apt-Cacher

Server Installation

1. Install apt-cacher and apache2 webserver

sudo apt-get install apt-cacher apache2

2. Enable apt-cacher

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

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

4. Restart apt-cacher process:

sudo service apt-cacher restart

Server Configuration

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

allowed_hosts = * Or set allowed_hosts to something appropriate.

allowed_ssl_locations = dl.bintray.com:443,dl.bintray.com,archive.cloudera.com[,<additional ssl repos]

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

5. Test apt-cacher by pointing your web browser to http://server01:3142/apt-cacher to verify that it’s running: http://aptproxy.server01:3142/

For example:

The contents of the /etc/apt/apt.conf.d/01proxy file should be:

Acquire::http::Proxy “http://aptproxy.server01:3142/”;
APT::Get::AllowUnauthenticated 1;

6. Import existing cached Packages

7. Import any existing packages into the cache by creating symlinks:

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

Distributions

/var/cache/name-repo/dists/name/

Pools

/var/cache/name-repo/pool/

Note: on the apt-cacher server, the apt-get deb files are located on /var/cache/apt/archives/

Client Configuration

Use as a proxy to APT

1. In a terminal, type:

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

2. Inside your new file, add a line that says:

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

Note: The entire file will look like this:

Acquire::http::Proxy “http://aptproxy.server01:3142/”;

APT::Get::AllowUnauthenticated 1;

3. Update the /etc/apt/apt.conf.d/10periodic file:

Note: The entire file will look like this:

APT::Periodic::Update-Package-Lists “1”;

APT::Periodic::Download-Upgradeable-Packages “1”;

APT::Periodic::AutocleanInterval “1”;

APT::Periodic::Unattended-Upgrade “1”;

APT::Periodic::Verbose “2”;

4. Make sure Unattented-Upgrade::Automatic-Reboot is set to “True” in this file: /etc/apt/apt.conf.d/50unattended-upgrades

Unattended-Upgrade::Automatic-Reboot “true”;

Note: Next time any of your machines needs a deb package, it will ask Apt-Cacher for it. If this package was asked previously by any of the other machines, Apt-Cacher will have a copy and will deliver it. Otherwise, it will fetch it from the internet repositories, keep a local copy and deliver it.