Home  >  Article  >  Computer Tutorials  >  It’s so easy to make a deb package in Linux: Analysis of the internal composition and usage principles of Ubuntu’s deb installation package file

It’s so easy to make a deb package in Linux: Analysis of the internal composition and usage principles of Ubuntu’s deb installation package file

WBOY
WBOYforward
2024-02-19 13:21:15823browse

It’s so easy to make a deb package in Linux: Analysis of the internal composition and usage principles of Ubuntu’s deb installation package file

Deb file is an archive file used to distribute and install Linux Debian and its derivative distribution programs, usually containing application data. They come in handy for handling dependencies, desktop integration, and running pre- and post-installation scripts. Another package format similar to the Deb format is the rpm file commonly used in Fedora series distributions.

This article demonstrates how to make a simple deb package through examples, explains the role of each file in the deb package, and how to take effect and maintain it after installation.

deb package analysis

deb is a common Unix ar archive format that contains application and other utility files. The key component is the control file (control), which contains important information about the deb package and the installer.

  • Internally, the deb package contains a collection of files that simulate the typical file system directory structure of Linux, such as /usr, /usr/bin, /opt, etc. During installation, files placed in one of these directories are copied to the same location in the actual file system. For example, binary files such as /usr/bin/binaryfile in the package will be installed to the system's /usr/bin/binaryfile.
  • Externally, all deb package files follow a specific naming convention:
_-_.deb

Suppose you want to publish a program named mynano, version 1.0, which is built for 64-bit processors (AMD64). Your deb filename will be something like mynano_1.0-0_amd64.deb

Create deb package

We are ready to generate the package. Please make sure your system has the dpkg-deb tool (provided by the dpkg package) installed. The final deb package will be generated using dpkg-deb later.

(1) Create a working directory to store the package files and name them according to the previous naming convention.

mkdir mynano_1.0-1_amd64/

(2) Create the internal structure and place the program files where they should be installed on the target system. Let's say you want to install the executable to: /usr/bin/

First create the directory:

mkdir -p mynano_1.0-1_amd64/usr/bin/

The -p flag of the mkdir command will create nested directories. If any of the directories does not exist, it will be created automatically. Then copy the executable into it:

# 假设你开发的程序可执行文件为 ~/YourProjects/mynano/src/targets/release/mynano
cp ~/YourProjects/mynano/src/targets/release/mynanomynano_1.0-1_amd64/usr/bin/

(3) Create the file control. This file is located in the DEBIAN directory (note that the directory name is in capital letters)

Create the folder first: DEBIAN

mkdir mynano_1.0-1_amd64/DEBIAN

Then create an empty file: control

touch mynano_1.0-1_amd64/DEBIAN/control

Fill in the control file content:

Package: mynano
Version: 1.0
Architecture: amd64
Maintainer: linuxlibs 
Description: 基于nano的自定义编辑器
Depends: nano (>= 5.0)

in:

  • Package–Program name;
  • Version–Program version;
  • Architecture—Target architecture;
  • Maintainer– Name and email address of the person responsible for package maintenance;
  • Description – A brief description of the program.
  • Depends- Other packages this package depends on.

The file may contain other useful fields, such as Depends pointing out the deb package's dependency list. Then if you use the apt command to install the deb package, the nano>=5.0 version of the software package will be installed first, and then mynano will be installed.

(5) The last step: Build the deb package and call dpkg-deb as follows:

dpkg-deb --build --root-owner-group 

In our example:

dpkg-deb --build --root-owner-group 

The –root-owner-group flag here makes all deb package contents owned by the root user, which is the standard method. If there is no such flag, the owner of all files and folders is your current user, but considering that the system where the deb package will be installed does not necessarily have an account with the same name as yours, use –root-owner-group more reasonable.

The above command will generate a .deb file next to the working directory, or print errors if there are errors or missing in the package. If the operation is successful, you can distribute the generated deb package to others.

(6) Use deb package to install into the system: You can see that when installing the deb package we made through apt method, the dependency will be automatically installed: nano software package

# apt install ./mynano_1.0-1_amd64.deb
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成
注意,选中 'mynano' 而非 './mynano_1.0-1_amd64.deb'
将会同时安装下列软件:
nano
建议安装:
hunspell
下列【新】软件包将被安装:
mynano nano
升级了 0 个软件包,新安装了 2 个软件包,要卸载 0 个软件包,有 79 个软件包未被升级。
需要下载 280 kB/1,135 kB 的归档。
解压缩后会消耗 881 kB 的额外空间。
您希望继续执行吗? [Y/n] y
获取:1 /root/my-nano-editor-src/mynano_1.0-1_amd64.deb mynano amd64 1.0.0 [855 kB]
获取:2 https://mirrors.ustc.edu.cn/ubuntu jammy/main amd64 nano amd64 6.2-1 [280 kB]
已下载 280 kB,耗时 1秒 (422 kB/s)
正在选中未选择的软件包 nano。
(正在读取数据库 ... 系统当前共安装有 231799 个文件和目录。)
准备解压 .../archives/nano_6.2-1_amd64.deb...
正在解压 nano (6.2-1) ...
正在选中未选择的软件包 mynano。
准备解压 .../mynano_1.0-1_amd64.deb...
正在解压 mynano (1.0.0) ...
正在设置 nano (6.2-1) ...
update-alternatives: 使用 /bin/nano 来在自动模式中提供 /usr/bin/editor (editor)
update-alternatives: 使用 /bin/nano 来在自动模式中提供 /usr/bin/pico (pico)
正在设置 mynano (1.0.0) ...
正在处理用于 install-info (6.8-4build1) 的触发器 ...
正在处理用于 man-db (2.10.2-1) 的触发器 ...
Scanning processes...
Scanning processor microcode...
Scanning linux images...

(7) [Optional] Uninstall the installed software mynano:

# apt remove mynamo -y
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成
下列软件包将被【卸载】:
mynano
升级了 0 个软件包,新安装了 0 个软件包,要卸载 1 个软件包,有 79 个软件包未被升级。
解压缩后会消耗 0 B 的额外空间。
您希望继续执行吗? [Y/n] y
(正在读取数据库 ... 系统当前共安装有 231872 个文件和目录。)
正在卸载 mynano (1.0.0) ...

(8) [Optional] Check the dependencies of mynano_0.1-1_amd64.deb: dpkg -I ./mynano*deb

What can be improved in the above way of making deb packages:

The above is the detailed content of It’s so easy to make a deb package in Linux: Analysis of the internal composition and usage principles of Ubuntu’s deb installation package file. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:mryunwei.com. If there is any infringement, please contact admin@php.cn delete