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.

You might want to combine this with my tutorial on how to set up a mirror for CentOS, which can be run on the same Ubuntu host (just skip step 1).

There is currently a bug whereby Ubuntu 16.04 clients can only update from 16.04 mirrors due to the apt-mirror package still needing to be backported or some other solution. I recommend for now you use a 16.04 host for mirroring.

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 root cron job to run apt-mirror at a period of your choice. I set my mirror to run daily at midnight by editing my crontab (crontab -e) and 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

I did try using gb.archive.ubuntu.com/ubuntu (because I'm in the UK), but it kept failing to fetch the i386 packages

If you don't need this mirror to support net installations (e.g. just an update mirror instead), then you can remove all occurances of the following texts:

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.

References

Author

Programster

Stuart is a software developer with a passion for Linux and open source projects.

comments powered by Disqus
We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to Amazon.com and affiliated sites. More info.