How to Create Your Own Repository for Packages on Debian

There are various reasons why you would want to build your own local repository. It is a great way to create a local mirror repository for caching frequently used packages used by many computers to save bandwidth usage, or you might have a few modified packages that you want to make internally available for the dev team.

In this tutorial, we will show you how to easily create a local Debian package repository, compatible with Debian and Ubuntu versions.

Step1: Installing Required Package

On Debian-based systems, all repositories are managed by the APT utilities (apt, apt-get, apt-cache, etc). The dpkg-dev package is needed for local repository creation compatible with APT.

First Update the system packages using the following command:

$ sudo apt update && sudo apt upgrade

Next install the dpkg-dev package by typing the following:

$ sudo apt-get install dpkg-dev

When package installation finishes, proceed with creating a directory for your package files.

Step 2: Create a Directory for Local Repository

Create a directory to keep binary packages. For this example, we'll use /opt/local/debs, but you can use any directory you may like.

$ sudo mkdir -p /opt/local/debs

Next, change directory:

$ cd /opt/local/debs

Step 3: Adding Packages to Local Repo Directory

For the purpose of this tutorial, we will download the chrome-browser package to our local repository, because it is not found in the default Ubuntu repository:

$ sudo wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

You can copy or download as many packages as you like in this step.

Step 4: Create the Required Repository Package Meta for APT

For this, we must run several dpkg-scanpackages commands. I'll switch to the root user account because I'm using the /opt/local directory to skip using sudo.

$ sudo su

First, we will create a Release file by running the following command:

# dpkg-scanpackages . /dev/null > Release

You should get a similar output depending on how many packages you have added to the local repository:

dpkg-scanpackages: warning: Packages in archive but missing from override file:
dpkg-scanpackages: warning: google-chrome-stable
dpkg-scanpackages: info: Wrote 1 entries to output Packages file.

Next, scan all the deb files in the directory and create an appropriate Packages.gz file

# dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz

Output:

dpkg-scanpackages: warning: Packages in archive but missing from override file:
dpkg-scanpackages: warning: google-chrome-stable
dpkg-scanpackages: info: Wrote 1 entries to output Packages file.

Note that you must run these commands every time you add new deb packages to your local repository directory. You could also create a simple bash script and run it whenever you add new packages.

To List local repo directory structure, run ls command:

$ ls -l

Output:

-rw-r--r-- 1 root root 83325072 May 8 02:29 google-chrome-stable_current_amd64.deb
-rw-r--r-- 1 root root 761 May 17 20:44 Packages.gz
-rw-r--r-- 1 root root 1321 May 17 20:39 Release

Step 5: Adding Our Local Repository to Sources.list

The final step is to edit the sources.list file. Edit the file using the editor of your choice:

$ sudo nano /etc/apt/sources.list

Add the following line to your /etc/apt/sources.list:

deb [trusted=yes] file:/opt/local/debs ./

Now we will test our local repository in action.

Step 6: Verification

We can verify by installing or removing the packages from Local Repository.

First we must update the packages:

$ sudo apt-get update

Now we install our package as usual using apt-get:

$ sudo apt-get install google-chrome-stable

Now your local packages can be installed, updated and removed using Synaptic, aptitude and the apt commands: apt-get, apt-cache, etc. When you run apt-get install, any dependencies will be resolved and installed for you, as long as they can be met.

We can easily remove our installed packages the same way as with any installed package on our system:

$ sudo apt-get remove google-chrome-stable

Conclusion

In this tutorial, we have learned how to create our own local Debian repository and add/remove packages locally. These steps apply to most Debian-based distributions.

Leave a Comment