- Debian Package Tutorial (dpkg/.deb)
- List installed packages
- Create a package
- Configuration files
- Pre/post install scripts
- Package interrelationships
- Print package information
- Print package contents
- Extract package contents
- Install a package
- Searching for packages in apt repositories
- Uninstall a package
- Listing files that belong to a package
- Find which package owns a file
- Security notes
- Convert a .deb to .rpm and other formats
- Debhelper tool
- Conclusion
- How to Create DEB Packages for Debian/Ubuntu
- Step 1: Installation of Required Packages
- Step 2: Package Selection
- Step 3: Begin to Prepare the Package
- Step 4: Adding Developer Information
Debian Package Tutorial (dpkg/.deb)
Debian Package tool dpkg is used on Debian, Ubuntu, Kali, Linux Mint, and other Debian based Linux distributions. This tutorial covers the basics of creating custom .deb packages and managing them with dpkg and apt . We will cover the process of creating your own deb file to distribute your package easily. It is aimed at people who are interested in learning the basics of creating and managing packages in Debian based distributions. Read more about The .deb file format
A .deb file is a Debian package, but really it is just a standard Unix archive ( .ar ) file. The dpkg tool is the Debian package manager and apt is a higher level tool for managing packages which uses dpkg under the hood.
List installed packages
List packages with dpkg or apt :
dpkg -l # List installed packages dpkg -s mypkg # Get status of specific package apt list --installed # List installed packages apt list # List *all* packages in available repos
Create a package
You can make and distribute your own .deb package very easily. To start fresh, create a new directory named after your package. For example, mypkg . This directory will store the package metadata and the package contents. Inside the project directory, there needs to be one special directory ( DEBIAN ) with one special file inside it ( control ).
# Minimum required structure for a package mypkg-1.0.0/ # Directory └── DEBIAN/ # Directory └── control # File
A more practical example with a post-installation script:
mypkg/ # Directory ├── DEBIAN/ # Directory │ └── control # Control file │ └── postinst # Post install script ├── etc/my.ini # Optionally any other files that you need to include └── opt/mylib/ # on the destination system during installation
This is a minimal example of the DEBIAN/control file:
Package: mypkg Version: 1.0.0 Maintainer: Your Name Description: My test package, please ignore Homepage: https://github.com/username/projectname Architecture: all Depends: git, python3 (>=3.5), openjdk-8-jre-headless|openjdk-8-jre|openjdk-8-jdk-headless|openjdk-8-jdk
The Depends line is optional, but frequently needed. The example above shows how to add multiple dependencies, how to specify a minimum version with python3, and how to add a dependency that is satisfied by one of many options. There are many versions of java listed and any one will satisfy the requirement. If none of them are installed, it will start from the first one in the list and try to install it. This way, it will work if the user has version 8 of the JDK or JRE in full or headless variant. If none of them are installed it will install the smallest version (the headless jre).
Build the package by running dpkg -b on the project directory like this:
# Build mypkg directory in to .deb package dpkg -b ./mypkg ./mypkg_1.0.0-0_amd64.deb # Naming format: _-_.deb # Version is the version number of the app being packaged # Release number is the version number of the *packaging* itself. # The release number might increment if the package maintainer # updated the packaging, while the version number of the application # being packaged did not change. # Inspect information like size, version, dependencies dpkg -I mypkg.deb
# Example output of `dpkg -i mypkg.deb` new debian package, version 2.0. size 666 bytes: control archive=301 bytes. 238 bytes, 6 lines control Package: mypkg Version: 1.0.0 Maintainer: Your Name Description: My test package, please ignore Architecture: all Depends: git, python3 (>= 3.5), openjdk-8-jre-headless|openjdk-8-jre|openjdk-8-jdk-headless|openjdk-8-jdk
It’s a special little package, but it’s lonely without any files to install. Normally a package has some contents like a shared library, man pages, documentation, an executable program, or systemd service files.
The next thing we’ll need to do is add files to the package. Some examples of things you might include are:
- an executable or a shell script launcher in /usr/local/bin/
- man pages in /usr/share/man
- configuration files in /etc/
- libraries in /usr/local/lib
- header files in /usr/local/include
In this example, we’ll add a shell launch script for a Java JAR file:
#!/bin/bash # Launch script to kick off Java JAR (/usr/bin/mylauncher) java -jar /usr/share/java/myapp.jar "$@"
Be sure to chmod +x the script and place it in the mypkg/usr/local/bin/ directory.
Configuration files
List configuration files in DEBIAN/conffiles This will allow configuration files to optionally remain when uninstalling (remove vs purge). It also prevents updates from overwriting configuration files.
/etc/myapp.conf /etc/some/other/conf.ini
Pre/post install scripts
DEBIAN/preinst DEBIAN/postinst
An example DEBIAN/postinst script:
#!/bin/bash # This `DEBIAN/postinst` script is run post-installation mkdir /dir/needed/for/package
Package interrelationships
There are some other variables you can specify in the control file:
- Depends
- Recommends
- Suggests
- Breaks
- Conflicts
- Provides
- Replaces
- Enhances
These can be useful to warn people if your package conflicts with another package that is already installed. You can also replace or enhance existing packages. If other packages would make your package work better, you can recommend and suggest other packages when the user installs your package.
Print package information
You can use dpkg and apt-cache to get some information about packages.
# Print details about a package file (size, version, dependencies) dpkg -I mypkg.deb # Print status (is it installed?) dpkg -s # Show package info (even uninstalled ones from repos) apt-cache show
Print package contents
If you want to review the contents of a .deb package before installing it, you can print the contents with the -c flag.
dpkg -c mypkg-1.0.0-0_amd64.deb
Extract package contents
If you want to extract the contents of a .deb file, use the -X flag for verbose extraction, or -x for quiet extraction.
dpkg -X mypkg-1.0.0-0_amd64.deb /where/to/exctact/
Install a package
Usually, two options are available for installing .deb packages. One is dpkg and the other is apt which is built on top of dpkg .
If you don’t have the dependencies installed you will have to install them yourself if you use dpkg -i . Alternatively, you can use apt . I recommend using apt because it still uses dpkg under the hood, but it handles dependencies and is more user-friendly.
# Install package along with any dependencies it needs sudo apt install ./mypkg.deb # Install package, ignore and warn about unmet dependencies sudo dpkg -i mypkg.deb # If you need to install dependencies that were not # installed because you used `dpkg -i` instead of `apt install` sudo apt --fix-broken install # Get status of package dpkg -s mypkg
Searching for packages in apt repositories
You can use apt-cache to search for packages in your apt repositories. These will need to be installed with apt install though not dpkg .
Uninstall a package
You can uninstall a package using remove but it will leave the configuration files. To also remove the configuration files, use purge .
dpkg --remove package_name # Leaves config files dpkg --purge package_name # Removes config files too # Or with apt apt remove package_name apt purge package_name
Listing files that belong to a package
This can be useful when trying to figure out what the executable file names are in /usr/bin or when trying to find local documentation.
# List all files that are included in the `ruby` package dpkg -L ruby dpkg -L ruby2.5-doc
Find which package owns a file
If you have a file on your system and you want to see if it belongs to a package, you can use dpkg -S to see which package owns it.
You can also use apt-file search to see which packages in the repositories contain a file with that name.
For example «What package created this /etc/qwerty.conf file?» or «Which package owns this executable?», or «Which packages do I need to install to get gl.h ?» can be answered with:
# Figure out which package owns a specific file dpkg -S /etc/qwerty.conf dpkg -S /usr/bin/python3 # Search apt repositories for packages that contain file # Find any package that includes a `gl.h` apt-file search "gl.h"
Security notes
Only Use trusted packages and repositories only. You install packages with root privileges and you will be giving full reign on your machine with a package. Do not run any package you are not 100% confident it is from a trusted source.
GPG signing is a good way to ensure integrity and authenticity, that is, it has not been modified and the package came from the expected author.
You can learn more about how to sign files with GPG in my GPG Basics Tutorial. GPG is a great cryptography tool to be familiar with and worth taking the time to learn it. You can also use it to encrypt emails so only the intended recipient can read it, encrypting archives or PDFs, verifying signatures from others, and and many other practical uses.
Convert a .deb to .rpm and other formats
If you need a RedHat .rpm file to install in CentOS, RedHat, or Fedora, you can use the alien tool to convert packages. http://joeyh.name/code/alien/
sudo apt install alien alien --help alien --to-rpm my_pkg.deb
-d, --to-deb Generate a Debian deb package (default). -r, --to-rpm Generate a Red Hat rpm package. --to-slp Generate a Stampede slp package. -l, --to-lsb Generate a LSB package. -t, --to-tgz Generate a Slackware tgz package.
Debhelper tool
I do not have much experience with this tool but it is worth a mention. The debhelper tool provides some utility and code generation features. Here is how you can install it and explore more.
sudo apt install debhelper man dh # See what tools are available by entering dh_ # Get the autocomplete options # Generate a new Debian package template sudo apt install dh-make # From inside a directory w/ name like: mypkg-1.0.0 dh_make --createorig
Conclusion
You should now feel comfortable working with and creating your own Debian packages, how to get information about a package, how to find which package a file belongs to, and other common tasks.
How to Create DEB Packages for Debian/Ubuntu
Most of the software on Linux is distributed as packages. Creating DEB packages for Debian-based distros is easier than you might think.
Readers like you help support MUO. When you make a purchase using links on our site, we may earn an affiliate commission. Read More.
A DEB package is an archive containing all the files including the compiled version of the applications, source codes, configuration files, images, and installation commands. DEB files in Debian-based operating systems like Ubuntu and Kali Linux are equivalent to the EXE files found in Windows.
Here’s how you can develop your own DEB packages for a Debian-based Linux distro.
Step 1: Installation of Required Packages
Preparing a Debian package requires some programs. To begin, install these utilities on your system:
sudo apt install build-essential binutils lintian debhelper dh-make devscripts
Step 2: Package Selection
Before creating a Debian package (DEB) for a program, you should consider a few points:
- Check whether the package you’re planning to create is already in the Debian repositories:
apt-cache search package-name
Step 3: Begin to Prepare the Package
Firstly, create a new directory under your home directory to avoid confusion.
cd /home
mkdir package
cd package
Then extract the tar archive containing the source code of the program you’re going to package under this directory. For demonstration, we’ll use the rsyslog archive.
Navigate to the newly-created directory using the cd command:
Usually, the source code of the program comes with INSTALL and README files. Even if you know what the program is and how it works, it will be beneficial for you to spend some time reading these files.
There are commands such as ./configure make and make install that can easily install such archives on your system. But there are several parameters for the ./configure option that you should be aware of. You can use the ./configure —help command to get such information.
Step 4: Adding Developer Information
Before creating a DEB package for your program, pay attention to the package name and version number. You will also need to add some packager information when creating a package. For this, you must export your information with the following commands:
export DEBEMAIL="your@mail.com"
export DEBFULLNAME="Name Lastname"
After this, use the magic command dh_make.
After issuing the dh_make command, you should select your package type and press Enter.
Following this step, you will notice a directory in a parent directory with the «.orig» extension. If this doesn’t work, try running the dh_make command with the —createorig parameter.