Host your own apt repo
Server side
Update your system
sudo apt update
Install apt-mirror. NOTE - The apt-mirror isn't updated anymore. I use this fork.
sudo apt install apt-mirror
Edit the mirrors file
sudo nano /etc/apt/mirror.list
By default the repo will be stored in /var/spool/apt-mirror/mirror/.
Change it simply by uncommenting the line # set mirror path and change to whatever path you want.
The last row in the file clean http://archive.ubuntu.com/ubuntu will clean out old packages. Uncomment this if you want to be able to downgrade packages to older versions. Note that the repository will grow a lot faster if you disable this.
To sync the amd64 packages for Ubuntu 20.04 LTS (Focal Fossa) and Ubuntu 21.10 (Impish Indri). Roughly 300GB at the time of writing this guide (February 2022).
Using only the amd64 packages is fine if you use Ubuntu Server. If you're going to use this for Desktops as well you would probably want the i386 packages too. This will add an additional 50GB.
If you do, just remove the comments below.
deb-amd64 http://archive.ubuntu.com/ubuntu impish main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu impish-security main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu impish-updates main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu impish-proposed main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu impish-backports main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu focal main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu focal-security main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu focal-updates main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu focal-proposed main restricted universe multiverse
deb-amd64 http://archive.ubuntu.com/ubuntu focal-backports main restricted universe multiverse
#deb-i386 http://archive.ubuntu.com/ubuntu impish main restricted universe multiverse
#deb-i386 http://archive.ubuntu.com/ubuntu impish-security main restricted universe multiverse
#deb-i386 http://archive.ubuntu.com/ubuntu impish-updates main restricted universe multiverse
#deb-i386 http://archive.ubuntu.com/ubuntu impish-proposed main restricted universe multiverse
#deb-i386 http://archive.ubuntu.com/ubuntu impish-backports main restricted universe multiverse
#deb-i386 http://archive.ubuntu.com/ubuntu focal main restricted universe multiverse
#deb-i386 http://archive.ubuntu.com/ubuntu focal-security main restricted universe multiverse
#deb-i386 http://archive.ubuntu.com/ubuntu focal-updates main restricted universe multiverse
#deb-i386 http://archive.ubuntu.com/ubuntu focal-proposed main restricted universe multiverse
#deb-i386 http://archive.ubuntu.com/ubuntu focal-backports main restricted universe multiverse
To start a manual sync of all packages run
sudo apt-mirror
This will take a long time and is of course highly dependant on your internet connection.
I'd suggest you run it in screen if you're connected with SSH.
This is how screen works.
Automate the download
sudo crontab -e
Add this to a new line to sync every night at 02:30.
(crontab.guru is great for learning the cron schedule expressions)
30 2 * * * /usr/bin/apt-mirror
Keeping track (optional)
Limit bandwidth can be done by setting the limit_rate value in the mirrors.list file. Note that the limit is per thread and is written in kilobytes. Meaning 125k is 1Mbit.
sudo nano /etc/apt/mirror.list
Add set limit_rate 125k
If you want to monitor the bandwidth and connections you could use iftop.
sudo apt install iftop
sudo iftop (press q to quit)
See how much data has been downloaded with
du -hs /var/spool/apt-mirror/mirror/
To get more granularity you can use ncdu
sudo apt install ncdu
sudo ncdu /var/spool/apt-mirror/mirror/ (press q to quit)
Web server
Install Nginx
sudo apt install nginx
Start and enable the service
sudo systemctl enable nginx
sudo systemctl start nginx
Create a new file and replace mirror.domain.com with whatever hostname you're using.
/etc/nginx/conf.d/mirrors.conf
server {
listen 80;
server_name mirror.domain.com;
root /var/spool/apt-mirror/mirror/archive.ubuntu.com/;
location / {
autoindex on;
}
}
Restart Nginx
sudo systemctl restart nginx
Firewall
Enable SSH, HTTP to your server and activate the firewall.
ufw allow 22
ufw allow 80
ufw enable
Client side
On your clients we need to change the /etc/apt/sources.list file.
sudo nano /etc/apt/sources.list
Replace all the URLs with your own, for instance
deb http://archive.ubuntu.com/ubuntu focal main restricted
would be modified to
deb http://my.domain.com/ubuntu focal main restricted
Now run sudo apt update.