This is a text-only version of the following page on https://raymii.org:
---
Title       : 	Set up a local Ubuntu (apt) repository with apt-mirror
Author      : 	Remy van Elst
Date        : 	28-03-2012
URL         : 	https://raymii.org/s/tutorials/Set_up_a_local_Ubuntu_debian_apt_mirror.html
Format      : 	Markdown/HTML
---



This tutorial will cover the setup of an ubuntu/debian repository mirror. This
is quite handy if you have multiple machines in your network running ubuntu. You
only have to update the mirror, and the rest downloads the packages from there,
so instead of having to download it 6 times (for 6 computers) you only download
it once. Saves you and the mirrorservers some bandwidth.

<p class="ad"> <b>Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:</b><br><br> <a href="https://leafnode.nl">I'm developing an open source monitoring app called  Leaf Node Monitoring, for windows, linux & android. Go check it out!</a><br><br> <a href="https://github.com/sponsors/RaymiiOrg/">Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.</a><br><br> <a href="https://www.digitalocean.com/?refcode=7435ae6b8212">You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days. </a><br><br> </p>


We will be using the apt-mirror tool to do all the magic.

Please note that all the command need to be executed as root unless otherwise
noted.

## Installing apt-mirror

If you are on a recent ubuntu/debian version (+lucid or +lenny) you can execute
the following command to install apt-mirror:

    
    
    apt-get install apt-mirror
    

## Configuring apt-mirror

Apt-mirrors config is quite easy. Below you can find my config file with
comments, please adapt where needed.

To edit:

    
    
    vim /etc/apt/mirror.list
    

/etc/apt/mirror.list:

    
    
    ############# config ##################
    #
    # Default variables are good enough.
    # set base_path    /var/spool/apt-mirror
    #
    # set mirror_path  $base_path/mirror
    # set skel_path    $base_path/skel
    # set var_path     $base_path/var
    # set cleanscript $var_path/clean.sh
    # set defaultarch  <running host architecture>
    # set postmirror_script $var_path/postmirror.sh
    # set run_postmirror 0
    set nthreads     20
    set _tilde 0
    #
    ############# end config ##############
    
    # Start of repository URLs.
    
    ## Ubuntu Precise (12.04)
    deb http://archive.ubuntu.com/ubuntu precise main restricted universe multiverse
    deb http://archive.ubuntu.com/ubuntu precise-security main restricted universe multiverse
    deb http://archive.ubuntu.com/ubuntu precise-updates main restricted universe multiverse
    
    ## Ubuntu Lucid (10.04 LTS) (Only 32 bit, no 64 bit machines running 10.04)
    deb-i386 http://archive.ubuntu.com/ubuntu lucid main restricted universe multiverse
    deb-i386 http://archive.ubuntu.com/ubuntu lucid-security main restricted universe multiverse
    deb-i386 http://archive.ubuntu.com/ubuntu lucid-updates main restricted universe multiverse
    
    ## Debian Stable (atm 6, squeeze)
    deb http://ftp.nl.debian.org/debian stable main contrib non-free
    
    ## Only 64-bit packages for debian testing
    deb-amd64 http://ftp.nl.debian.org/debian testing main contrib non-free
    
    ## No backports needed for me.
    ## If needed, please uncomment.
    #deb http://archive.ubuntu.com/ubuntu precise-proposed main restricted universe multiverse
    #deb http://archive.ubuntu.com/ubuntu precise-backports main restricted universe multiverse
    
    ## Also no deb-src.  
    #deb-src http://archive.ubuntu.com/ubuntu precise 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
    
    ## I want to be able to downgrade the packages, so I don't want to clean up automatically.    
    # clean http://archive.ubuntu.com/ubuntu
    # clean http://ftp.nl.debian.org/debian
    

## Downloading the packages

Now if the config is set up the way you want, we can start downloading the
packages and filling the repository.

Execute the following command to start downloading. Note that this can take a
while, here it took half an hour, so be patient. The packages will be stored in
subdirectories of the _/var/spool/apt-mirror/mirror_ directory, so make sure the
partition that holds that directory has enough disk space left.

    
    
    root@repo:/var/repo# su - apt-mirror -c apt-mirror
    

Output looks like this:

    
    
    Downloading 14 index files using 14 threads...
    Begin time: Mon Sep 17 14:26:32 2012
    [14]... [13]... [12]... [11]... [10]... [9]... [8]... [7]... [6]... [5]... [4]... [3]... [2]... [1]... [0]... 
    End time: Mon Sep 17 14:26:37 2012
    
    Proceed indexes: [PP]
    
    149.1 GiB will be downloaded into archive.
    Downloading 116234 archive files using 20 threads...
    Begin time: Mon Sep 17 14:26:38 2012
    [20]... [19]... [18]... [17]... [16]... [15]... [14]... [13]... [12]... [11]... [10]... [9]... [8]... [7]... [6]... [5]... [4]... [3]... [2]... [1]... [0]... 
    End time: Mon Sep 17 15:03:22 2012
    
    3.8 GiB in 5631 files and 1808 directories can be freed.
    Run /var/spool/apt-mirror/var/clean.sh for this purpose.
    
    Running the Post Mirror script ...
    (/var/spool/apt-mirror/var/postmirror.sh)
    
    
    Post Mirror script has completed. See above output for any possible errors.
    

## Installing and configuring a web server

To be able to access the repo from other computers you need a webserver. You can
also do it via ftp but I choose to use a webserver. You can install apache,
lighttpd, cherokee, nginx or whatever suits you (boa, thttpd, awk-http).

Install a webserver (in this example, nginx)

    
    
    apt-get install nginx
    

The packages are in _/var/spool/apt-mirror/mirror_. We are going to create a
symlink to make them available. Make sure you edit the names and folders of the
repo if your config is not the same as above.

    
    
    ln -s /var/spool/apt-mirror/mirror/archive.ubuntu.com/ubuntu/ /var/www/ubuntu
    ln -s /var/spool/apt-mirror/mirror/ftp.nl.debian.org/debian/ /var/www/debian
    

Your /var/www/ folder now should have these two items:

    
    
    root@repo:~# ls -alh /var/www
    lrwxrwxrwx  1 root root   54 Sep 17 15:54 debian -> /var/spool/apt-mirror/mirror/ftp.nl.debian.org/debian/
    lrwxrwxrwx  1 root root   55 Sep 17 14:22 ubuntu -> /var/spool/apt-mirror/mirror/archive.ubuntu.com/ubuntu/    
    

Make sure you can see them via the webserver, go to the url of the server. If
you've are doing the repo locally you can use this URL:

    
    
    http://127.0.0.1/ubuntu
    

Note, for lighttpd you need to make sure that in the config the following option
is enabled:

    
    
    server.follow-symlink = enabled    
    

## Setting up the clients

Now we have a working repo, we need to set up the clients to use this repo
instead of the other (main) ubuntu repo's.

If you only have the standard repositories then you can use sed to comment all
the default lines. If you are not sure, or have other repo's, please skip this
step.

sed command to comment the entire /etc/apt/sources.list file:

    
    
    sed -i 's/(.*)/#1/g' /etc/apt/sources.list /etc/apt/sources.list
    

Now edit the /etc/apt/sources.list file. Make sure to replace [[MYREPO]] with
the http address of your repo (http://127.0.0.1 or http://myrepo.localdomain or
whatever)

    
    
    vim /etc/apt/sources.list 
    

And add the following (adapt if you have changed the config file):

    
    
    deb http://[[MYREPO]]/ubuntu precise main restricted universe
    deb http://[[MYREPO]]/ubuntu precise-updates main restricted universe
    deb http://[[MYREPO]]/ubuntu precise-security main restricted universe multiverse
    

Or for debian:

    
    
    deb http://[[MYREPO]]/debian stable main contrib non-free
    

Also comment out the other repositories (by adding a hash # before it).

Now reload the apt sources and see if it works:

    
    
    root@repo:~# apt-get update
    Ign http://127.0.0.1 precise InRelease
    Get:1 http://127.0.0.1 precise Release.gpg [198 B]
    Get:2 http://127.0.0.1 precise Release [49.6 kB]
    Get:3 http://127.0.0.1 precise/main i386 Packages [1274 kB]
    Ign http://127.0.0.1 precise/main TranslationIndex
    Ign http://127.0.0.1 precise/main Translation-en 
    Fetched 1324 kB in 0s (2009 kB/s)                
    Reading package lists... Done
    

As you can see, it works!

## Cleaning up

If the repo becomes to big, you can use apt-mirror to clean up some space. This
will delete older packages. I choose to not do that automatically, so I can
install earlier versions of packages, or downgrade existing ones.

Run the following command to clean up:

    
    
    /var/spool/apt-mirror/var/clean.sh
    

Output may look like:

    
    
    root@repo:~#     /var/spool/apt-mirror/var/clean.sh
    Removing 5631 unnecessary files [4077010944 bytes]...
    [0%]..................................................[8%]..................................................[17%]..................................................[26%]..................................................[35%]..................................................[44%]..................................................[53%]..................................................[62%]..................................................[71%]..................................................[79%]..................................................[88%]..................................................[97%]..............done.
    
    Removing 1808 unnecessary directories...
    

   [1]: https://www.digitalocean.com/?refcode=7435ae6b8212

---

License:
All the text on this website is free as in freedom unless stated otherwise. 
This means you can use it in any way you want, you can copy it, change it 
the way you like and republish it, as long as you release the (modified) 
content under the same license to give others the same freedoms you've got 
and place my name and a link to this site with the article as source.

This site uses Google Analytics for statistics and Google Adwords for 
advertisements. You are tracked and Google knows everything about you. 
Use an adblocker like ublock-origin if you don't want it.

All the code on this website is licensed under the GNU GPL v3 license 
unless already licensed under a license which does not allows this form 
of licensing or if another license is stated on that page / in that software:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    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.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

Just to be clear, the information on this website is for meant for educational 
purposes and you use it at your own risk. I do not take responsibility if you 
screw something up. Use common sense, do not 'rm -rf /' as root for example. 
If you have any questions then do not hesitate to contact me.

See https://raymii.org/s/static/About.html for details.