预备条件
sudo apt-get install -y gcc dpkg-dev gpg
使用以上命令安装所需工具,以hello world为例下面制作一个deb包
第一步 准备hello word源码
fromdtor@april:~/hello-world$ tree
.
├── bin
└── src
└── hello.c
2 directories, 1 file
fromdtor@april:~/hello-world$ cat src/hello.c
#include <stdio.h>
int main() {
printf("hello packaged world\n");
return 0;
}
fromdtor@april:~/hello-world$
示例目录中有两个目录,src下有一个c源文件,编译并运行:
fromdtor@april:~/hello-world$ gcc -o bin/hello-world src/hello.c
fromdtor@april:~/hello-world$ tree
.
├── bin
│ └── hello-world
└── src
└── hello.c
2 directories, 2 files
fromdtor@april:~/hello-world$ ./bin/hello-world
hello packaged world
fromdtor@april:~/hello-world$
第二步 创建deb包
先创建一个以下命名格式的目录
<package-name>_<version>-<release-number>_<architecture>
fromdtor@april:~/hello-world$ mkdir hello-world_0.0.1-1_amd64
fromdtor@april:~/hello-world$ mkdir -p hello-world_0.0.1-1_amd64/usr/
fromdtor@april:~/hello-world$
fromdtor@april:~/hello-world$ mv bin hello-world_0.0.1-1_amd64/usr/
fromdtor@april:~/hello-world$ tree
.
├── hello-world_0.0.1-1_amd64
│ └── usr
│ └── bin
│ └── hello-world
└── src
└── hello.c
4 directories, 2 files
添加文件hello-world_0.0.1-1_amd64/DEBIAN/control,内容如下
fromdtor@april:~/hello-world$ tree
.
├── hello-world_0.0.1-1_amd64
│ ├── DEBIAN
│ │ └── control
│ └── usr
│ └── bin
│ └── hello-world
└── src
└── hello.c
5 directories, 3 files
fromdtor@april:~/hello-world$ cat hello-world_0.0.1-1_amd64/DEBIAN/control
Package: hello-world
Version: 0.0.1
Maintainer: example <example@example.com>
Depends: libc6
Architecture: amd64
Homepage: http://example.com
Description: A program that prints hello
fromdtor@april:~/hello-world$
然后运行以下命令
dpkg --build hello-world_0.0.1-1_amd64
现在目录中多了一个文件hello-world_0.0.1-1_amd64.deb
fromdtor@april:~/hello-world$ dpkg --build hello-world_0.0.1-1_amd64
dpkg-deb: building package 'hello-world' in 'hello-world_0.0.1-1_amd64.deb'.
fromdtor@april:~/hello-world$ ls
hello-world_0.0.1-1_amd64 hello-world_0.0.1-1_amd64.deb src
fromdtor@april:~/hello-world$
检查一下包含的内容
fromdtor@april:~/hello-world$ dpkg-deb --info hello-world_0.0.1-1_amd64.deb
new Debian package, version 2.0.
size 2744 bytes: control archive=340 bytes.
183 bytes, 7 lines control
Package: hello-world
Version: 0.0.1
Maintainer: example <example@example.com>
Depends: libc6
Architecture: amd64
Homepage: http://example.com
Description: A program that prints hello
fromdtor@april:~/hello-world$ dpkg-deb --contents hello-world_0.0.1-1_amd64.deb
drwxrwxr-x fromdtor/fromdtor 0 2022-08-31 14:19 ./
drwxrwxr-x fromdtor/fromdtor 0 2022-08-31 14:16 ./usr/
drwxrwxr-x fromdtor/fromdtor 0 2022-08-31 14:12 ./usr/bin/
-rwxrwxr-x fromdtor/fromdtor 8304 2022-08-31 14:12 ./usr/bin/hello-world
fromdtor@april:~/hello-world$
使用以下命令也可以
less hello-world_0.0.1-1_amd64.deb
第三步 安装测试
安装之前
fromdtor@april:~/hello-world$ which hello-world
指定文件安装
fromdtor@april:~/hello-world$ sudo apt-get install -f ~/example/hello-world_0.0.1-1_amd64.deb
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'hello-world' instead of '/home/fromdtor/example/hello-world_0.0.1-1_amd64.deb'
The following packages were automatically installed and are no longer required:
alien at bc cups-bsd cups-client debhelper debugedit dh-autoreconf dh-strip-nondeterminism golang-1.10 golang-1.10-doc golang-1.10-go
golang-1.10-race-detector-runtime golang-1.10-src golang-race-detector-runtime golang-src intltool-debian libarchive-cpio-perl libarchive-zip-perl
libarchive13 libcupsfilters1 libcupsimage2 libdw1 libfile-stripnondeterminism-perl liblua5.2-0 libmail-sendmail-perl libnetplan0 librpm8 librpmbuild8
librpmio8 librpmsign8 libsys-hostname-long-perl libyaml-0-2 lsb-security pax pkg-config po-debconf rpm rpm-common rpm2cpio systemtap-common
systemtap-runtime
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
hello-world
0 upgraded, 1 newly installed, 0 to remove and 9 not upgraded.
Need to get 0 B/2,740 B of archives.
After this operation, 0 B of additional disk space will be used.
Get:1 /home/fromdtor/example/hello-world_0.0.1-1_amd64.deb hello-world amd64 0.0.1 [2,740 B]
Selecting previously unselected package hello-world.
(Reading database ... 122398 files and directories currently installed.)
Preparing to unpack .../hello-world_0.0.1-1_amd64.deb ...
Unpacking hello-world (0.0.1) ...
Setting up hello-world (0.0.1) ...
安装之后
fromdtor@april:~/hello-world$ which hello-world
/usr/bin/hello-world
验证运行
fromdtor@april:~/hello-world$ hello-world
hello packaged world
fromdtor@april:~/hello-world$
第四步 卸载
sudo apt-get remove hello-world
后续关于怎么托管自己的deb包的详情可以参考这里
Creating and hosting your own deb packages and apt repo
这里就先不管了
代码参见这里https://gitee.com/fromdtor/deb-pkg-demo
恢复apt环境
1.删除锁文件
sudo rm /var/lib/apt/lists/lock
2.更新索引
sudo apt-get update
网友评论