Set Up A Local Ubuntu Mirror with Apt-Mirror
If setting apt to dynamically use the closest mirrors just isn't fast enough, you can set up your own mirror on your local network will drastically improve the performance of updates and installations for your other computers. This is particularly useful if you have automatic KVM guest installations as I am. This tutorial will set you up so that you have an ubuntu mirror that is accessable through the web (http). You do not have to have your mirror accessible this way and FTP/NAS are good alternatives, but I chose http because it's common and easy.
Install apache and the apt-mirror program which will sync your computer as a mirror.
sudo apt-get install apache2 apt-mirror
Create a symlink to the downloaded files in your apache www directory so that whenever someone navigates to the "website" they are automatically taken to the mirror files.
sudo ln -s /var/spool/apt-mirror/mirror/archive.ubuntu.com/ubuntu/ /var/www/ubuntu
Set up a cron job to run apt-mirror at a period of your choice by editing the cron file at /etc/cron.d/apt-mirror
. This will be run as the apt-mirror user.
I set my mirror to run daily at midnight by adding the following line:
@daily /usr/bin/apt-mirror
Configure the mirror list at /etc/apt/mirror.list
. Below is an example that will mirror both 64bit and 32 bit architectures since by default, apt-mirror will only mirror the same architecture as the host.
...
############# end config ##############
# copy below this line
# 16.04 mirroring
deb-amd64 http://archive.ubuntu.com/ubuntu xenial main main/debian-installer restricted restricted/debian-installer universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu xenial-security main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu xenial-updates main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu xenial-proposed main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu xenial-backports main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu xenial main main/debian-installer restricted restricted/debian-installer universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu xenial-security main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu xenial-updates main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu xenial-proposed main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu xenial-backports main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu xenial-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu xenial-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu xenial-proposed main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu xenial-backports main restricted universe multiverse
# 14.04 mirroring
deb-amd64 http://archive.ubuntu.com/ubuntu trusty main main/debian-installer restricted restricted/debian-installer universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu trusty-security main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu trusty-proposed main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu trusty main main/debian-installer restricted restricted/debian-installer universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu trusty-security main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu trusty-proposed main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu trusty-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu trusty-proposed main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse
# 12.04 mirroring
deb-amd64 http://archive.ubuntu.com/ubuntu precise main main/debian-installer restricted restricted/debian-installer universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu precise-security main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu precise-updates main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu precise-proposed main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu precise main main/debian-installer restricted restricted/debian-installer universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu precise-security main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu precise-updates main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu precise-proposed main restricted universe multiverse
deb-i386 http://archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu precise-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu precise-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu precise-proposed main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse
clean http://archive.ubuntu.com/ubuntu
main/debian-installer
and restricted/debian-installer
Configure Apache
sudo echo '<VirtualHost *:80>
ServerAdmin webmaster@hostname.com
DocumentRoot /var/www
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>' | sudo tee /etc/apache2/sites-enabled/000-default.conf
sudo chown www-data:www-data /var/www/ubuntu
sudo rm -rf /var/www/html
sudo service apache2 restart
Configure Clients
Once your mirror is up and running, you need to configure your other computers to use it for updates.
Bandwidth Limiting
For your initial syncronization, you may want to implement banwidth limiting. To do this, edit the /etc/apt/mirror.list file and add the following lines within the config section.
#Bandwidth limiting.
set limit_rate [number of Kilobytes]k
set nthreads 1
You will need to ensure that there is only one set nthreads line, and remember that the limit_rate is in kilobytes, not kilobits. 1 megabit is equal to 125 kilobytes.
Manually Running apt-mirror
If you wish to manually run the apt-mirror command, be sure to do it as the apt-mirror user.
sudo su apt-mirror
apt-mirror
Otherwise, you will be shown the following error messages...
flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror line 206.
apt-mirror is already running, exiting at /usr/bin/apt-mirror line 209.
... and be tempted to run it with sudo to resolve the issue. Unfortunately that will cause the files to be owned by root which will cause them to require being run by root in future, so be sure to use the apt-mirror user.