0 %

linux程序打包成deb

2025-09-22 05:57:59

前言

最近在研究如何将自己的程序打包称deb格式,用dpkg或者apt管理起来,最终我们可以使用"dpkg-buildpackage -us -uc"来生成我们的deb文档。以下为我的学习心得,将每一个步骤记录下来。

准备工作

一.创建一个文件夹,里面放自己的程序,或者源码,文件夹命名-

packagename即deb软件包名称,只能含有 小写字母 (a-z), 数字 (0-9), 加号 (+) 和 减号 (-) , 以及 点号 (.)。 软件包名最短长度两个字符;它必须以字母开头;它不能与仓库软件包名发生冲突。还有,把软件包名的长度控制在 30 字符以内是明智之举。

version即deb软件包版本号,只包含字母和数字 (0-9A-Za-z), 以及加号 (+), 波浪号 (~), 还有 点号(.)。它必须以数字开头 (0-9)。如果可能的话,最好把它的长度控制在8字符以内。

范例:我要建立一个helloworld-1.0.0的deb软件包

1.创建一个文件夹:helloworld-1.0.0

2.将自己的程序放进去(这里我写了一个简单的脚本)

1 root@508b9712a279:/data# tree

2 .

3 └── helloworld-1.0.0

4 └── sbin

5 └── helloworld

6

7 2 directories, 1 file

8 root@508b9712a279:/data#

3.打包:将helloworld-1.0.0打包成tar.gz

1 root@508b9712a279:/data# tar -zcvf helloworld-1.0.0.tar.gz helloworld-1.0.0/

2 helloworld-1.0.0/

3 helloworld-1.0.0/sbin/

4 helloworld-1.0.0/sbin/helloworld

5 root@508b9712a279:/data# ls -l

6 total 8

7 drwxrwxr-x 3 moxa moxa 4096 Oct 27 11:29 helloworld-1.0.0

8 -rw-rw-r-- 1 moxa moxa 226 Oct 27 11:35 helloworld-1.0.0.tar.gz

9 root@508b9712a279:/data#

二.使用dh_make初始化软件包

1.安装dh_make:apt install -y dh-make

2.设置dh_make默认环境变量:$DEBEMAIL 和 $DEBFULLNAME,这样大多数 Debian 维护工具就能够正确识别你用于维护软件包的姓名和电子邮件地址。我的做法是将其加在.bashrc的最后,然后再source ~/.bashrc生效。

1 root@508b9712a279:/data/helloworld-1.0.0# tail -n 4 ~/.bashrc

2 DEBEMAIL="ethandl.wang@moxa.com"

3 DEBFULLNAME="ethan wang"

4 export DEBEMAIL DEBFULLNAME

5

6 root@508b9712a279:/data/helloworld-1.0.0# source ~/.bashrc

3.检验环境变量已生效

1 root@508b9712a279:/data/helloworld-1.0.0# export | grep DEB

2 declare -x DEBEMAIL="ethandl.wang@moxa.com"

3 declare -x DEBFULLNAME="ethan wang"

4.初始化软件包

1 root@508b9712a279:/data# ls

2 helloworld-1.0.0 helloworld-1.0.0.tar.gz

3 root@508b9712a279:/data# ls

4 helloworld-1.0.0 helloworld-1.0.0.tar.gz

5 root@508b9712a279:/data# cd helloworld-1.0.0

6 root@508b9712a279:/data/helloworld-1.0.0# dh_make -f ../helloworld-1.0.0.tar.gz -n -s -y

7 Maintainer Name : ethan wang

8 Email-Address : ethandl.wang@moxa.com

9 Date : Thu, 27 Oct 2022 03:56:40 +0000

10 Package Name : helloworld

11 Version : 1.0.0

12 License : gpl3

13 Package Type : single

14 Currently there is not top level Makefile. This may require additional tuning

15 Done. Please edit the files in the debian/ subdirectory now.

16

17 root@508b9712a279:/data/helloworld-1.0.0#

-s:Automatically set the package class to Single binary, skipping the question.

-n:Create a native Debian packages

-y:Automatic yes to prompts and run non-interactively. The package class needs to be set for dh_make to run fully automatically.

在helloworld-1.0.0内就会生成一个debian的文件夹

5. debian目录中的必需文件

1 -rw-r--r-- 1 root root 176 Oct 27 10:36 debian/changelog

2 -rw-r--r-- 1 root root 3 Oct 27 10:36 debian/compat

3 -rw-r--r-- 1 root root 279 Oct 27 10:36 debian/control

4 -rw-r--r-- 1 root root 1675 Oct 27 10:36 debian/copyright

5 -rw-r--r-- 1 root root 143 Oct 27 10:36 debian/install

6 -rwxr-xr-x 1 root root 678 Oct 27 10:36 debian/rules

7

8 debian/source:

9 total 4

10 -rwxr-xr-x 1 root root 13 Oct 27 10:36 format

debian/install : 如果你的软件包需要那些标准的 make install 没有安装的文件,你可以把文件名和目标路径写入 install 文件,它们将被 dh_install(1) 安装。

1 root@508b9712a279:/data/helloworld-1.0.0# cat debian/install

2 sbin/* sbin/

3 root@508b9712a279:/data/helloworld-1.0.0#

更多详细资料可以参考官方文档:https://www.debian.org/doc/manuals/maint-guide/dreq.zh-cn.html

创建软件包

1 root@508b9712a279:/data/helloworld-1.0.0# dpkg-buildpackage -us -uc

2 dpkg-buildpackage: info: source package helloworld

3 dpkg-buildpackage: info: source version 1.0.0

4 dpkg-buildpackage: info: source distribution unstable

5 dpkg-buildpackage: info: source changed by ethan wang

6 dpkg-buildpackage: info: host architecture amd64

7 dpkg-source --before-build .

8 debian/rules clean

9 dh clean

10 dh_clean

11 dpkg-source -b .

12 dpkg-source: info: using source format '3.0 (native)'

13 dpkg-source: info: building helloworld in helloworld_1.0.0.tar.xz

14 dpkg-source: info: building helloworld in helloworld_1.0.0.dsc

15 debian/rules binary

16 dh binary

17 dh_update_autotools_config

18 dh_autoreconf

19 create-stamp debian/debhelper-build-stamp

20 dh_prep

21 dh_install

22 dh_installdocs

23 dh_installchangelogs

24 dh_perl

25 dh_link

26 dh_strip_nondeterminism

27 dh_compress

28 dh_fixperms

29 dh_missing

30 dh_dwz -a

31 dh_strip -a

32 dh_makeshlibs -a

33 dh_shlibdeps -a

34 dh_installdeb

35 dh_gencontrol

36 dpkg-gencontrol: warning: Depends field of package helloworld: substitution variable ${shlibs:Depends} used, but is not defined

37 dh_md5sums

38 dh_builddeb

39 dpkg-deb: building package 'helloworld' in '../helloworld_1.0.0_amd64.deb'.

40 dpkg-genbuildinfo

41 dpkg-genchanges >../helloworld_1.0.0_amd64.changes

42 dpkg-genchanges: info: including full source code in upload

43 dpkg-source --after-build .

44 dpkg-buildpackage: info: full upload; Debian-native package (full source is included)

45 root@508b9712a279:/data/helloworld-1.0.0#

46 root@508b9712a279:/data/helloworld-1.0.0#

47 root@508b9712a279:/data/helloworld-1.0.0#

48 root@508b9712a279:/data/helloworld-1.0.0# dpkg -c ../helloworld_1.0.0_amd64.deb

49 drwxr-xr-x root/root 0 2022-10-27 03:56 ./

50 drwxr-xr-x root/root 0 2022-10-27 03:56 ./sbin/

51 -rwxr-xr-x root/root 53 2022-10-27 03:30 ./sbin/helloworld

52 drwxr-xr-x root/root 0 2022-10-27 03:56 ./usr/

53 drwxr-xr-x root/root 0 2022-10-27 03:56 ./usr/share/

54 drwxr-xr-x root/root 0 2022-10-27 03:56 ./usr/share/doc/

55 drwxr-xr-x root/root 0 2022-10-27 03:56 ./usr/share/doc/helloworld/

56 -rw-r--r-- root/root 185 2022-10-27 03:56 ./usr/share/doc/helloworld/README.Debian

57 -rw-r--r-- root/root 141 2022-10-27 03:56 ./usr/share/doc/helloworld/changelog.gz

58 -rw-r--r-- root/root 1640 2022-10-27 03:56 ./usr/share/doc/helloworld/copyright

59 root@508b9712a279:/data/helloworld-1.0.0#

安装&运行&卸载

1 root@508b9712a279:/data/helloworld-1.0.0# dpkg -i ../helloworld_1.0.0_amd64.deb

2 Selecting previously unselected package helloworld.

3 (Reading database ... 20973 files and directories currently installed.)

4 Preparing to unpack ../helloworld_1.0.0_amd64.deb ...

5 Unpacking helloworld (1.0.0) ...

6 Setting up helloworld (1.0.0) ...

7 root@508b9712a279:/data/helloworld-1.0.0# helloworld

8 hello,world, i am a deb package.

9 root@508b9712a279:/data/helloworld-1.0.0# dpkg --purge helloworld

10 (Reading database ... 20978 files and directories currently installed.)

11 Removing helloworld (1.0.0) ...

12 root@508b9712a279:/data/helloworld-1.0.0#

Posted in 世界杯小组赛分组
Copyright © 2088 2034年世界杯_足球中国世界杯预选赛 - qdhuaxue.com All Rights Reserved.
友情链接