Debian中Golang打包流程(以现代dh-golang方法为例)
首先确保系统安装了Go编译器、git(用于获取源代码)和debhelper(Debian打包核心工具):
sudo apt update
sudo apt install golang-go git debhelper dh-golang
将Golang应用源代码放置在项目根目录(如myapp/),并创建debian/目录(用于存放打包配置文件):
mkdir -p myapp/debian
定义软件包元数据,包括名称、版本、维护者、依赖等:
Source: myapp
Section: utils
Priority: optional
Maintainer: Your Name <your.email@example.com>
Build-Depends: debhelper-compat (= 13), dh-golang
Standards-Version: 4.5.1
Homepage: https://example.com/myapp
Package: myapp
Architecture: amd64
Depends: ${misc:Depends}, ${shlibs:Depends}
Description: A brief description of your application.
A longer description of your application's functionality and features.
Build-Depends:指定构建时依赖(debhelper-compat用于兼容性,dh-golang用于Go特定处理);Architecture:Go应用通常编译为静态二进制文件,故设为amd64(或all用于纯脚本);Depends:${misc:Depends}和${shlibs:Depends}会自动填充运行时依赖。配置构建脚本,使用dh(debhelper)调用dh-golang处理Go构建流程:
#!/usr/bin/make -f
%:
dh $@ --with=golang
--with=golang启用dh-golang插件,自动处理Go模块下载、编译和安装。记录软件包版本更新历史(需符合Debian格式):
myapp (1.0.0-1) unstable; urgency=medium
* Initial release.
-- Your Name <your.email@example.com> Mon, 01 Nov 2025 12:00:00 +0000
dch命令快速创建(需安装devscripts包):sudo apt install devscripts
dch --create -v 1.0.0-1 --package myapp
声明软件版权和许可证信息(需包含应用本身及依赖的Go模块许可证):
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: myapp
Source: https://github.com/yourname/myapp
Files: *
Copyright: 2025 Your Name
License: MIT
License: MIT
Permission is hereby granted...
dh-make生成模板:dh-make --createorig -y -p myapp_1.0.0
若需安装额外文件(如配置文件、静态资源),列出源路径与目标路径:
myapp /usr/bin/
config.toml /etc/myapp/
static/ /usr/share/myapp/static/
在项目根目录(myapp/)运行以下命令:
debuild -us -uc -b
-us -uc:不签名源代码包和变更日志(仅测试用);-b:仅构建二进制包(.deb文件)。myapp_1.0.0-1_amd64.deb(文件名格式为<包名>_<版本>_<架构>.deb)。使用dpkg安装生成的.deb文件:
sudo dpkg -i ../myapp_1.0.0-1_amd64.deb
/usr/bin/下:which myapp # 应输出 /usr/bin/myapp
将.deb文件上传至服务器或通过工具(如dput)发布到Debian仓库(需配置GPG密钥和仓库服务器)。
debian/rules和debian/control中的Architecture字段。binary-without-manpage),可在debian/changelog中添加覆盖注释:* [lintian-override] myapp: binary-without-manpage
Please ignore this warning for a simple utility.
dh-golang会自动处理Go模块依赖,确保go.mod和go.sum文件存在且完整。