在开始打包前,需确保系统已安装Go编译器和打包工具(如git、debhelper)。
sudo apt update && sudo apt install -y golang-go git
go version确认安装成功(如go1.22.5 linux/amd64)。~/.bashrc,避免每次手动配置:echo 'export GOPATH=$HOME/go' >> ~/.bashrc
echo 'export PATH=$PATH:$GOPATH/bin' >> ~/.bashrc
source ~/.bashrc
将项目编译为静态链接的可执行文件(便于在无Go环境的Debian系统上运行):
cd /path/to/your-golang-project # 进入项目根目录
go build -o your_project_name -ldflags '-extldflags "-static"' . # 生成静态二进制文件
-o:指定输出文件名(如myapp);-ldflags:强制静态链接所有依赖(避免动态库依赖)。在项目根目录下创建debian/目录(用于存放打包元数据和规则文件):
mkdir -p debian
debian/目录下需创建以下关键文件,定义包的元数据、构建规则和安装路径:
debian/control(必选)定义包的基本信息(名称、版本、依赖、维护者等):
Source: your-go-app # 源码包名(小写字母、无特殊字符)
Section: utils # 所属分类(如utils/tools/net)
Priority: optional # 优先级(optional/recommended/essential)
Maintainer: Your Name <your.email@example.com> # 维护者信息
Build-Depends: debhelper-compat (= 13), dh-golang # 构建依赖(必须包含dh-golang)
Standards-Version: 4.5.1 # 符合Debian标准版本
Homepage: https://github.com/your/repo # 项目主页(可选)
Package: your-go-app # 二进制包名(与Source关联)
Architecture: amd64 # 目标架构(amd64/arm64等)
Depends: ${misc:Depends}, ${shlibs:Depends} # 运行时依赖(自动处理)
Description: A brief description of your Go application. # 短描述(一行)
A longer description of your application's features and usage. # 长描述(多行,缩进)
debian/rules(必选)定义构建流程(使用dh-golang简化Go项目构建):
#!/usr/bin/make -f
# 启用详细模式(调试用,正式打包时可注释)
# export DH_VERBOSE = 1
# 设置临时Go工作目录和二进制输出路径(避免污染系统)
export GOPATH := $(CURDIR)/.gopath
export GOBIN := $(CURDIR)/bin
# 调用debhelper并加载dh-golang插件
%:
dh $@ --with golang
# 自定义安装步骤(将编译后的二进制文件安装到目标路径)
override_dh_auto_install:
dh_golang_install # 使用dh-golang自动安装Go二进制文件
# 若有其他资源文件(如配置文件),可添加dh_install命令:
# dh_install -X.go -X.mod -X.sum config.toml /etc/your-go-app/
dh $@ --with golang会自动处理Go模块依赖、编译和安装;override_dh_auto_install用于自定义安装路径。debian/install(可选)指定需安装的额外文件(如配置文件、静态资源)及目标路径:
path/to/config.toml /etc/your-go-app/ # 将config.toml复制到/etc/your-go-app/
path/to/static/ /usr/share/your-go-app/static/ # 复制静态资源目录
dh_golang_install会默认安装到/usr/bin/)。debian/copyright(必选)声明软件版权和许可证信息:
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: your-go-app
Source: https://github.com/your/repo
Files: *
Copyright: 2025 Your Name
License: MIT # 替换为项目实际许可证(如MIT/GPL-3.0)
License: MIT
Permission is hereby granted... # 粘贴许可证全文(或保留此占位符)
debian/changelog(必选)记录包的版本更新历史(格式严格,需用dch工具生成):
sudo apt install devscripts # 安装dch工具
dch --create -v 1.0.0-1 --package your-go-app # 初始化changelog(版本号格式:upstream-version-debian_revision)
debian/changelog,添加更新说明(如修复bug、新增功能):your-go-app (1.0.0-1) unstable; urgency=medium
* Initial release.
* Compile as static binary for better portability.
– Your Name your.email@example.com Mon, 25 Oct 2025 10:00:00 +0800
##### **(6) `debian/compat`(可选但推荐)**
指定`debhelper`兼容级别(建议使用最新稳定版,如13):
```text
13
在项目根目录下运行以下命令,生成.deb包:
debuild -us -uc # -us:不签名源码;-uc:不签名.changes文件(仅测试用)
your-go-app_1.0.0-1_amd64.deb(文件名格式:package_version_architecture.deb)。使用dpkg安装生成的.deb包,并验证程序是否正常运行:
sudo dpkg -i ../your-go-app_1.0.0-1_amd64.deb # 安装包
sudo apt --fix-broken install # 修复可能的依赖问题(若有)
your_project_name # 运行程序(需在PATH路径中,或使用绝对路径)
sudo apt install -f自动修复依赖。import "C"),需确保编译时静态链接(-ldflags '-extldflags "-static"'),否则生成的二进制文件会依赖系统动态库(如libc.so),导致在无Go环境的系统上无法运行。Build-Depends需包含dh-golang(用于Go项目构建),Depends会自动处理Go模块依赖(通过${misc:Depends}和${shlibs:Depends})。debian/目录及其中的文件需具有正确的权限(通常为755目录、644文件),避免打包时出现权限错误。通过以上步骤,即可将Golang项目打包为符合Debian规范的.deb软件包,便于在Debian及其衍生系统(如Ubuntu)上分发和安装。