paint-brush
Debian Package Managementby@dulithag
21,825 reads
21,825 reads

Debian Package Management

by DulithaMay 17th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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.

People Mentioned

Mention Thumbnail
featured image - Debian Package Management
Dulitha HackerNoon profile picture

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.

Setup

First off, we need to create the folder structure and install the required packages to start building our Debian package.

  • Create a folder that will contain all the code related to our project using the command mkdir deb-repo-example ;
  • Then create two more folders within the main folder:
    • One for our package, which we will name helloworld-package;
    • The second one is for our repository, called helloworld-repo.
  • To have all the necessary packages installed, run the command sudo apt-get install dpkg dpkg-dev.

Creating a .deb Package

To start, let's create a simple .deb package locally and install it on our system.

  • Navigate to helloworld-package folder that we created earlier;
  • Next, we will create a file named helloworld.sh that will represent our program:
    • Run the command echo "echo 'hello world'" > helloworld.sh
    • Give executable permissions to the helloworld.sh file by running chmod +x helloworld.sh.
  • Create a bash script that will generate our .deb package:
    • Run the command touch create-deb.sh to create a new file;
    • Set its permissions to executable by running 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.

Managing Local Packages with DPKG

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:

  • Look up the package information by running dpkg --info helloworld.deb. This will display metadata about the package, such as its name, version, and dependencies;
  • Check the contents of the package by running dpkg --contents helloworld.deb;
  • To install package, run sudo dpkg --install helloworld.deb:
    • Note that the message from the post-install script appears at the end of the installation;
    • Verify that the “helloworld” script is found in the system with which helloworld.sh;
    • Verify that the “helloworld” script runs by executing helloworld.sh.
  • To remove the package run sudo dpkg --remove helloworld:
    • Observe the pre- and post-removal scripts in action.

Hosting a Debian Package in a Local APT Repository

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.

  • To start, let's navigate to the previously created "helloworld-repo" folder by running cd ../helloworld-repo;
  • Copy the previously created .deb file to the repo folder by running cp ../helloworld-package/helloworld.deb helloworld_1.0-1_amd64.deb;
    • Note that we need to change the name to include the package name, version, and distribution: {packageName}_{version}_{distribution}.deb.
  • Execute the following commands:
    • 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.

Managing packages with APT

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.

  • To install a package using APT, we first need to inform the system about the location of the repository that hosts the package. There are three ways to do this:
    • Editing the /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.
    • Creating a new file in the /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.
    • Using the add-apt-repository command.
  • The second step is to fetch all the available packages and their versions. This can be done by calling apt-get update
    • Calling this command updates the files available in /var/lib/apt/lists.
    • The files in /var/lib/apt/lists list the available versions of the packages.
  • To install packages using APT, we have the following options:
    • To update packages that are already installed on the system:
      • Run apt-get upgrade;
      • This command only updates the package on the system and adds or removes dependencies as needed.
    • To update packages that are already installed on the system with the possibility of adding or removing dependent packages:
      • Run apt-get dist-upgrade;
      • This command upgrades all installed packages on the system but adds or removes additional dependencies if necessary.
    • To install a package that does not exist on the current system:
      • Run apt-get install.
  • Installing packages results in the following:
    • The .deb package is downloaded to /var/cache/apt/archives/partial;
    • The .deb file is moved to /var/cache/apt/archives once the download is complete;
    • The package is installed in the system using dpkg.
  • 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.

References: