Package management is one of the core features of a Linux system. It involves handling software packages, their installation, and dependency management. In this guide, we’ll look at package management in the Debian distribution and walk you through how to create, host, and deploy .deb packages.
First off, we need to create the folder structure and install the required packages to start building our Debian package.
mkdir deb-repo-example
;helloworld-package
;helloworld-repo
.sudo apt-get install dpkg dpkg-dev
.To start, let's create a simple .deb package locally and install it on our system.
helloworld-package
folder that we created earlier;helloworld.sh
that will represent our program:
echo "echo 'hello world'" > helloworld.sh
helloworld.sh
file by running chmod +x helloworld.sh
.chmod +x create-deb.sh
.Finally, copy and paste the following code into the create-deb.sh
bash file to generate the Debian package.
#!/bin/bash
VERSION=1
# remove any previously created package
rm -r helloworld
rm helloworld.deb
# make a folder to create deb package
mkdir -p helloworld/DEBIAN
# we want our executable to be placed in /usr/local/bin after deb installation.
mkdir -p helloworld/usr/local/bin
cp helloworld.sh helloworld/usr/local/bin/
# create control file with information about the package
touch helloworld/DEBIAN/control
cat << EOT >> helloworld/DEBIAN/control
Package: helloworld
Version: ${VERSION}
Maintainer: John Doe
Architecture: all
Description: hello world program
EOT
# (optional) create script to run before installing begins
touch helloworld/DEBIAN/preinst
chmod +x helloworld/DEBIAN/preinst
echo "echo 'helloworld: starting installation'" >> helloworld/DEBIAN/preinst
# (optional) create script to run after installing completes
touch helloworld/DEBIAN/postinst
chmod +x helloworld/DEBIAN/postinst
echo "echo 'helloworld: finish installation'" >> helloworld/DEBIAN/postinst
# (optional) create script to run before removing
touch helloworld/DEBIAN/prerm
chmod +x helloworld/DEBIAN/prerm
echo "echo 'helloworld: start removing'" >> helloworld/DEBIAN/prerm
# (optional) create script to run after removing
touch helloworld/DEBIAN/postrm
chmod +x helloworld/DEBIAN/postrm
echo "echo 'helloworld: finish removing'" >> helloworld/DEBIAN/postrm
# build the package
dpkg --build helloworld
Now a package will be created with the name helloworld.deb
.
We can use the dpkg
command to install and remove a .deb package from our system. In this example, we will use the helloworld.deb
package that we just created to demonstrate these commands.
Let's start by managing our locally created helloworld.deb
package using dpkg:
dpkg --info helloworld.deb
. This will display metadata about the package, such as its name, version, and dependencies;dpkg --contents helloworld.deb
;sudo dpkg --install helloworld.deb
:
which helloworld.sh
;helloworld.sh
.sudo dpkg --remove helloworld
:
Now that we have created a deployable .deb package, we may want to distribute it to other users. In this section, we will look at how we can create an apt-repo to host our "helloworld" package. We’ll do this locally, but it can also be done by installing a web server to make it available online.
cd ../helloworld-repo
;cp ../helloworld-package/helloworld.deb helloworld_1.0-1_amd64.deb
;
{packageName}_{version}_{distribution}.deb
.dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz
;dpkg-scanpackages . /dev/null > Release
.This creates a valid repo that can now be used by apt to pull the “helloworld” binary. Let’s see how in the next section.
As previously outlined, dpkg helps add and remove programs packaged as .deb files in our system. APT is a wrapper around dpkg that can do a lot more. It enables users to pull packages from repositories hosted online (or locally on a CD drive), install updates, remove packages, and manage their dependencies.
In this section, we will explore the various commands and files associated with apt-get and demonstrate an example of using it to install the "helloworld" package that we created and hosted on our local file system.
/etc/apt/sources.list
file. By default the sources.list
file contains the URLs of predefined repositories. You can add your source to this file./etc/apt/sources.list.d
directory and adding the URL to that file. This method is better because you can add all the sources that are related in a separate file that can be easily removed later.add-apt-repository
command.apt-get update
/var/lib/apt/lists
./var/lib/apt/lists
list the available versions of the packages.apt-get upgrade
;apt-get dist-upgrade
;apt-get install
./var/cache/apt/archives/partial
;/var/cache/apt/archives
once the download is complete;apt-get clean
command can be used to clean up the .deb files to reclaim space.
Now, let's see APT in action by installing our locally hosted package, helloworld, using apt-get.
# add source (you will have to change the path to where your helloworld-repo is)
sudo touch /etc/apt/sources.list.d/helloworld.list
sudo echo "deb [trusted=yes] file:///home/my_user/deb-repo-example/helloworld-repo ./" > /etc/apt/sources.list.d/helloworld.list
# fetch package versions
sudo apt-get update
# We can see that apt has downloaded the metadata of the package
cat /var/lib/apt/lists/*helloworld*
# install helloworld package on our system
sudo apt-get install helloworld
# see that the helloworld program is found in our system, and execute it.
which helloworld
helloworld.sh
Now that we have installed the application, let's see how apt-get can also update packages installed on the system.
# navigate back to helloworld-package
# now lets create version 2 of helloworld package like we did before
echo "echo 'hello world from version 2'" > helloworld.sh`
# to create package for version 2 change the VERSION value in create-deb.sh then execute it
./create-deb.sh
# examine the package and ensure that package is now version 2
dpkg --info helloworld.deb
# copy package to helloworld-repo with
cp helloworld.deb ../helloworld-repo/helloworld_2.0-1_amd64.deb
# navigate to helloworld-repo and execute
dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz
dpkg-scanpackages . /dev/null > Release
# unfortunately dpkg only allows one version so we will replace our current repo with the new version. If you want to maintain multiple package you can use aptly
# now that our repo has a new version of hello world we can update our system by.
apt-get update
apt-get upgrade
# execute helloworld again and note that version is updated
helloworld.sh
# remove helloworld package from our system
apt-get clean helloworld
You have now successfully upgraded your package.