温馨提示×

ubuntu golang编译如何静态

小樊
33
2026-10-10 06:31:17
栏目: 编程语言

在 Ubuntu 上用 Go 编译静态可执行文件,核心思路是:禁用 CGO,且不依赖系统动态库。

下面按常见情况说明。


一、最常用:纯 Go 程序(无 CGO)

方法 1:禁用 CGO(推荐)

CGO_ENABLED=0 go build -o app main.go

✅ 生成的 app 是完全静态的
✅ 可直接在 Alpine / 容器 / 最小系统运行

检查是否静态:

file app
# 应显示:statically linked

ldd app
# 显示:not a dynamic executable

二、使用 -ldflags 加强静态(可选)

CGO_ENABLED=0 go build -ldflags "-s -w" -o app main.go
  • -s:去掉符号表
  • -w:去掉调试信息
  • 文件更小

三、如果用了 CGO(如 sqlite、net 某些情况)

情况 1:只是 net 包(DNS)

Go 默认可能用 libnss:

go env -w GODEBUG=netdns=go
CGO_ENABLED=0 go build -o app main.go

情况 2:必须 CGO(如 cgo + sqlite)

这时无法纯静态,但可以静态链接 libc:

sudo apt install gcc musl-tools

CGO_ENABLED=1 \
CC=musl-gcc \
go build -o app main.go

或使用静态参数:

CGO_ENABLED=1 go build \
-ldflags "-linkmode external -extldflags '-static'" \
-o app main.go

⚠️ 需要系统有静态库(如 libc.a)

sudo apt install libc6-dev

四、交叉编译静态(例如给 Alpine)

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o app main.go

五、Docker 最小镜像示例(推荐)

FROM golang:1.22 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o app .

FROM scratch
COPY --from=build /src/app /app
ENTRYPOINT ["/app"]

总结

场景 命令
纯 Go CGO_ENABLED=0 go build ✅
最小体积 CGO_ENABLED=0 -ldflags "-s -w"
必须 CGO musl-gcc 或 -static
容器 scratch + CGO_ENABLED=0

如果你愿意,可以告诉我:

  • Go 版本
  • 是否使用 sqlite / grpc / net
  • 目标运行环境(Ubuntu / Alpine / 容器)

我可以给你精确命令。

0 踩