How to Set Up a Local Debian Repository on Debian 12 (Bookworm)

Learn how to set up a local Debian repository on Debian 12 (Bookworm).

Setting up a local Debian repository can significantly improve package management in a controlled environment, reduce internet bandwidth usage, and speed up package installations across multiple systems. This guide walks you through the step-by-step process of setting up a local Debian repository on Debian 12 (Bookworm).

Why Set Up a Local Debian Repository?

A local repository provides several advantages, including:

  • Faster package installations – Reduces dependency on external mirrors.
  • Lower bandwidth usage – Ideal for multiple systems in a network.
  • Customization – You can host specific versions of packages required by your environment.
  • Offline installations – Useful in environments with limited or no internet access.

Prerequisites

Before starting, ensure the following:

  • A Debian 12 (Bookworm) system with root or sudo access.
  • A stable internet connection (for initial package downloads).
  • Adequate disk space to store packages.
  • The dpkg-dev, apt-mirror, and apache2 (or another web server) packages installed.

Step 1: Install Required Packages

First, update your system:

sudo apt update && sudo apt upgrade -y

Then, install the necessary packages:

sudo apt install dpkg-dev apt-mirror apache2 -y

The dpkg-dev package provides tools to create a Debian repository, apt-mirror is used to sync repositories, and apache2 serves packages over HTTP.

Step 2: Set Up the Repository Directory

Create a directory to store the repository:

sudo mkdir -p /var/local/debian-repo

Set proper permissions:

sudo chown -R $USER:$USER /var/local/debian-repo

This ensures the current user has write access.

Step 3: Download Debian Packages

Use apt-mirror to sync packages from an official mirror. Configure /etc/apt/mirror.list:

sudo nano /etc/apt/mirror.list

Add the following lines:

set base_path /var/local/debian-repo
set nthreads 20
set _tilde 0

deb http://deb.debian.org/debian bookworm main contrib non-free non-free-firmware
deb-src http://deb.debian.org/debian bookworm main contrib non-free non-free-firmware

deb http://security.debian.org/debian-security bookworm-security main contrib non-free

deb http://deb.debian.org/debian bookworm-updates main contrib non-free
clean http://deb.debian.org/debian
clean http://security.debian.org/

Save and exit. Then, run:

sudo apt-mirror

This may take some time, depending on your internet speed and selected packages.

Step 4: Organizing the Repository

Once the download completes, create the necessary metadata files:

cd /var/local/debian-repo/mirror/deb.debian.org/debian
sudo dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz

For sources:

sudo dpkg-scansources . /dev/null | gzip -9c > Sources.gz

Ensure proper permissions:

sudo chown -R www-data:www-data /var/local/debian-repo

Step 5: Configure Apache to Serve the Repository

Create a configuration file for Apache:

sudo nano /etc/apache2/sites-available/debian-repo.conf

Add the following content:

<VirtualHost *:80>
    ServerAdmin admin@yourdomain.com
    DocumentRoot /var/local/debian-repo/mirror/deb.debian.org/debian
    <Directory "/var/local/debian-repo/mirror/deb.debian.org/debian">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/debian-repo-error.log
    CustomLog ${APACHE_LOG_DIR}/debian-repo-access.log combined
</VirtualHost>

Enable the site and restart Apache:

sudo a2ensite debian-repo.conf
sudo systemctl restart apache2

Verify Apache is serving the repository:

curl http://localhost/

Step 6: Configure Clients to Use the Repository

On client machines, add the repository URL to /etc/apt/sources.list:

echo "deb http://your-server-ip/ bookworm main contrib non-free" | sudo tee -a /etc/apt/sources.list

Replace your-server-ip with the actual IP address of your server.

Update the package list:

sudo apt update

Now, you can install packages from your local repository!

Step 7: Automate Repository Updates

To keep the repository up-to-date, add a cron job:

sudo crontab -e

Add the following line to sync daily at midnight:

0 0 * * * /usr/bin/apt-mirror > /var/log/apt-mirror.log 2>&1

Save and exit.

Conclusion

Setting up a local Debian repository on Debian 12 (Bookworm) improves package management, reduces network load, and allows for customized package distributions. By following this guide, you can establish a fully functional local repository and configure client machines to use it efficiently.