In order to facilitate management of some programs or scripts, these programs and scripts can be made in a deb package. This chapter will introduce how to make a deb package. There are many ways to make a deb, such as using the dpkg-deb method, using the checkinstall method, using the dh_make method and modifying the original deb package. This chapter will introduce how to make your own deb package from scratch and modifying the original deb package.
What is a deb package
The deb package is an installation package under the Linux system. Sometimes the Linux software installation package we download online will also appear in the form of a deb package. Since it is based on the tar package, it will also record the file's permission information (read, write, executable), owner, user group, etc.
We can use the command: dpkg -l to view the system and which deb packages are installed
The composition structure of the deb package
The installed content, this part is similar to the root directory of linux, means that the software needs to be installed into the file directory on the linux system.
Control information (placed in the DEBIAN directory), usually there are several files below in the DEBIAN directory.
changelog: The file records the author, version, and the last update date of the deb package;
control: The file records the package name, version number, architecture, maintainer and description and other information;
copyright: The file records some copyright information;
Called before preins installation
# !/bin/bash echo 'Prepare to install <your filename>' #Execute the preparatory command before installation, you can omitYour order
Postinst installation completed call
# !/bin/bash #Update the desktop icon databaseupdate-desktop-database /usr/share/applications || true #Get the current usernameusername=`getent passwd \`who\` | head -n 1 | cut -d : -f 1` #Judge whether the desktop folder existsif [ -d "/home/${username}/Desktop" ]; then echo 'Desktop exist' #Copy your desktop file to the desktopcp your.desktopdocument /home/${username}/Desktop else echo 'Desktop folder exists' #The Chinese system automatically copy to the Chinese desktopcp your.desktopdocument /home/${username}/desktop fi
postrm: Uninstall and complete call
# !/bin/bash #Can be omittedrm -rf The soft link file you created echo '<your filename> has been uninstalled'
preerm: Called before uninstall
# !/bin/bash echo 'Prepare to uninstall <your filename>' #Execute the preparation command before uninstallation, you can omit itYour order
Among them, control, postinst, and postrm are necessary files.
Create your own deb package from scratch
sudo apt-get install build-essential debhelper make autoconf automake dpkg-dev fakeroot pbuilder gnupg -y
my-package/
├── DEBIAN
│ ├── control
│ ├── preinst (optional)
│ ├──postinst (optional)
│ ├── preerm (optional)
│ └── postrm (optional)
└── usr
└── share
└── my-package
└──
Create the DEBIAN and opt/hello_deb directories in the hello_deb directory. The DEBIAN directory contains the control information files. Create the hello_deb.sh file in the opt/hello_deb directory means that we need to install the hello_deb.sh file into the opt/hello_deb directory of the linux system.
Then give the postinst, postrm, and hello_deb.sh files executable permissions respectively. The permissions of postinst and postrm must be >=0555 and <=0775.
hello_deb/DEBIAN/control
Package: test-deb
Version: 1.0.0
Architecture: arm64
Maintainer: Your Name <@>
Description: A sample package
This is a sample package to demonstrate how to create a deb package.
A blank line needs to be added at the end of the control file, otherwise an error "missing line break character" will be reported.
If you want to upgrade this deb package in the future, you can modify the version number of the package. It is worth noting that Architecture. As we mentioned earlier, it is the processor architecture supported by this deb package. Because the deb package must be installed on the board of the arm64 processor, we should fill in the arm64 attribute in the Architecture. You can make corresponding modifications according to your needs. If you don’t know that your processor rack can use the dpkg -l command to view the architecture supported by the installed deb package, or enter lscpu to view processor information, aarch64 is the arm64 architecture. If you want to support all architectures, you can fill in the all attributes. If the Architecture attribute does not match the current processor architecture attributes, the deb package will not be installed successfully, and the control attribute information must start with letters or numbers, otherwise it may lead to packaging errors.
hello_deb/DEBIAN/postinst
#!/bin/bash echo "deb installing"
After installing the deb package, the system will execute the postinst script by default. Usually we use this script to build some environments for software execution (such as creating directories, modifying permissions, etc.). It is worth noting that the file needs to have executable permissions. The writing here is relatively simple. The first parameter is judged for reference only.
Finally, let’s take a look at the real program body. For the sake of simplicity, here is a simple script as an example.
hello_deb/opt/hello_deb/hello_deb.sh
#! /bin/bash echo Hello deb! echo This is a test script!!!
The script is just to print two sentences of information, and the user can set the program to be executed by himself.
Build package
Everything is ready, but it is only the east wind. After preparing the basic raw materials for making the deb package, we can start making our own deb package. Enter the hello_deb directory, which is the directory where the DEBIAN and home folders are located, and then enter the following command to build the software package.
sudo dpkg-deb -b ../hello_deb ../hello_deb_1.0.0_arm64.deb
where dpkg-deb is the command to build a deb package. The -b parameter indicates that you want to build a deb package. The …/hello_deb parameter indicates the path to build the deb package raw material. The …/hello_deb_1.0.0_arm64.deb parameter indicates that the deb package is built in the upper directory of the current directory. Generally, the name of the deb package will follow this principle. The naming method is: software name + software version number + the processor architecture supported by the software. For example, the software name is hello_deb, the version number is 1.0.0, and the supported processor architecture is arm64.
After the package is successful, the following information will be output, and the deb installation package can be viewed in the upper directory.
#Orderdpkg -c hello_deb_1.0.0_arm64.deb #Print informationdrwxrwxr-x jiawen/jiawen 0 2022-10-12 09:27 ./ drwxrwxr-x jiawen/jiawen 0 2022-10-12 09:28 ./opt/ drwxrwxr-x jiawen/jiawen 0 2022-10-12 09:28 ./opt/hello_deb/ -rwxrwxrwx jiawen/jiawen 59 2022-10-12 09:41 ./opt/hello_deb/hello_deb.sh
You can also use the following command to view the deb package information:
#Orderdpkg --info hello_deb_1.0.0_arm64.deb #Print informationnew Debian package, version 2.0. size 976 bytes: control archive=496 bytes. 190 byte, 9 OK control 100 byte, 4 OK * postinst #!/bin/bash 138 byte, 7 OK * postrm #!/bin/bash Package: hello-deb Version: 1.0.0 Section: free Priority: optional Essential: no Architecture: arm64 Maintainer: embedfire <embedfire@> Provides: hell_deb Description: deb test
Enter the "sudo dpkg -i hello_deb_1.0.0_arm64.deb" command to install, where the -i parameter indicates the installation software, that is, install
Unpacking
Create a new update_deb directory and use the dpkg -X command to extract the deb package into the update_deb directory.
$ ls Desktop Downloads Pictures Templates hello_deb_1.0.0_arm64.deb Documents Music Public Videos $ mkdir update_deb $ sudo dpkg -X hello_deb_1.0.0_arm64.deb update_deb/ ./ ./opt/ ./opt/hello_deb/ ./opt/hello_deb/hello_deb.sh $
Enter the update_deb directory and you can see that there is no DEBIAN-related directory. Use dpkg -e to decompress the relevant information of the control file in the update_deb directory.
~/update_deb$ ls opt ~/update_deb$ sudo dpkg -e ../hello_deb_1.0.0_arm64.deb ~/update_deb$ ls -al total 16 drwxrwxr-x 4 1001 1001 4096 Oct 12 13:37 . drwxr-xr-x 15 cat cat 4096 Oct 12 13:35 .. drwxr-xr-x 2 root root 4096 Oct 12 09:28 DEBIAN drwxrwxr-x 3 1001 1001 4096 Oct 12 09:28 opt ~/update_deb$ tree . |-- DEBIAN | |-- control | |-- postinst | `-- postrm `-- opt `-- hello_deb `-- hello_deb.sh 3 directories, 4 files
Now you can modify the program body
The above is a detailed explanation of the method of making deb packages in Linux. For more information about making deb packages in Linux, please pay attention to my other related articles!