在 Ubuntu 上用 Golang 打包,常见场景和对应工具如下:
# 本机
go build -o app main.go
# 交叉编译(如给别的 Linux / Windows)
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o app main.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o app.exe main.go
Ubuntu 一般已装 golang-go 或 golang-1.2x。
用 dh-make + debuild 或 fpm:
sudo apt install ruby ruby-dev
sudo gem install fpm
fpm -s dir -t deb -n myapp -v 1.0 \
--prefix /usr/local/bin \
./app
sudo apt install dh-make devscripts
dh_make -s -p myapp_1.0
# 改 debian/ 规则后
debuild -us -uc
sudo snap install snapcraft --classic
snapcraft init
# 编辑 snap/snapcraft.yaml
snapcraft
适合上 Ubuntu Software。
用 linuxdeploy:
./linuxdeploy-x86_64.AppImage \
--executable app \
--appdir AppDir \
--output appimage
FROM golang:1.22 as build
WORKDIR /src
COPY . .
RUN go build -o app
FROM ubuntu:22.04
COPY --from=build /src/app /usr/local/bin/app
CMD ["app"]
如果你说具体用途(CLI / Web / GUI / 上架商店),我可以给更精确的方案。