With CGO_ENABLED=0 you got a staticaly-linked binary (see: https://en.wikipedia.org/wiki/Static_build ) so it will run without any external dependencies (you can buld your dockers from 'scratch' image) Like that: https://github.com/s0rg/microapp/blob/master/Dockerfile Answer from Swimming-Medicine-67 on reddit.com
🌐
Reddit
reddit.com › r/golang › what is the consequence of using cgo_enabled=0?
r/golang on Reddit: What is the consequence of using CGO_ENABLED=0?
September 5, 2021 -

Is it a bad idea to set this environment variable to 0? From what I read, setting it to 1 means there should be a gcc compiler installed in the working system. Is this a correct interpretation? Why is it 1 by default?

I was trying to containerise my go application using the below docker file:

FROM golang:latest as builder

ENV GOOS=linux

COPY ./ /go/src/hello_world

WORKDIR /go/src/hello_world

RUN go build .

FROM alpine:latest

WORKDIR /usr/home

COPY --from=builder /go/src/hello_world/hello_world /usr/home

ENTRYPOINT ["./hello_world"]

This was giving me error but based on my findings I put ENV CGO_ENABLED=0 during build and it started working fine.

When is it required to be set to 1 and why did I have to explicitly set to 0 in my case?

P.S. I am very new to go so any resources on this would be appreciated.

🌐
Medium
akyriako.medium.com › reduce-the-size-of-golang-images-with-multi-stage-build-1bf6b896e3de
Reduce the size of Golang images with multi-stage build | by Akriotis Kyriakos | Medium
July 11, 2022 - # BUILDFROM golang:1.16-alpine as BUILDENV GO111MODULE=onWORKDIR /appCOPY go.mod . COPY go.sum .RUN go mod downloadCOPY . .RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go buildENV HTTP_PORT=8080 EXPOSE 8080# BINARIESFROM alpine:latestCOPY --from=BUILD /app/reduce-docker-size /app/reduce-docker-sizeENTRYPOINT ["/app/reduce-docker-size"]
🌐
Medium
medium.com › @diogok › on-golang-static-binaries-cross-compiling-and-plugins-1aed33499671
On Golang, “static” binaries, cross-compiling and plugins | by Diogok | Medium
May 10, 2017 - $ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -tags netgo -ldflags '-w -extldflags "-static"' -o mybin *.go
Top answer
1 of 3
31

What it's saying you need to do is rebuild the library and runtime for linux-amd64. You can do that this way:

  1. Find the root of your Go installation (if you don't know where this is, running which go may help - the binary is often installed with the rest of the sources).
  2. cd into the src directory
  3. Run GOOS=linux GOARCH=amd64 ./make.bash --no-clean (or GOOS=linux GOARCH=amd64 bash make.bash --no-clean if make.bash is not executable). This will rebuild the library and runtime using the specified OS and architecture.

Once you've done this, you can build a go package or binary for this architecture using GOOS=linux GOARCH=amd64 go build. You can follow the same instructions for other architectures and operating systems.

Edit (08/13/15):

As of Go 1.5, cross compiling is much easier. Since the runtime is written in Go, there's no need to set anything up in order to be able to cross-compile. You can now just run GOOS=<os> GOARCH=<arch> go build from a vanilla Go installation and it will work.

However, there's one exception to this. If you're using cgo, you'll still need to set stuff up ahead of time. And you'll need to inform the tooling that you want to enable cgo cross-compiling by setting the CGO_ENABLED environment variable to 1. So, to be precise:

  1. cd into the src directory of your Go installation (see the instructions above).
  2. Run CGO_ENABLED=1 GOOS=<os> GOARCH=<arch> ./make.bash --no-clean
  3. Run CGO_ENABLED=1 go build to build your project. It is important that you specify CGO_ENABLED=1 even when you're compiling.
2 of 3
1

Following the above answer https://stackoverflow.com/a/27413148/3675575, I needed to set GOROOT_BOOTSTRAP to recompile my GO source tree:

GOROOT_BOOTSTRAP=/usr/lib/golang/ CGO_ENABLED=1 GOOS=linux GOARCH=386 ./make.bash --no-clean

(I'm using Fedora 23, so the GOROOT_BOOTSTRAP may vary in your operating system)

🌐
Reddit
reddit.com › r/golang › is -installsuffix cgo needed with cgo_enabled=0 goos=linux goarch=amd64 go build
r/golang on Reddit: is -installsuffix cgo needed with CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
December 23, 2018 -
  • My project is a simple web API app which uses Docker.

  • I went though https://golang.org/cmd/go/ but didn't see any special case of -installsuffix cgo when using CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o ./build/api ./cmd/api .

Do we really need -installsuffix cgo when running go build inside go-alpine images?

🌐
Go Forum
forum.golangbridge.org › getting help
Go run main works, go build -o main.go fails with GOOS and GOARCH specified - Getting Help - Go Forum
July 2, 2023 - Hi all. when I do a go run main.go it runs - on my MBP -M1 when I do a go build . it builds, when I run try and build it with the below exports or execute the Makefile it fails - error below BINARY_NAME=main export GOOS=linux export GOARCH=amd64 export CGO_ENABLED=0 .DEFAULT_GOAL := deploy deploy: go build -o ${BINARY_NAME} . zip -r function.zip main aws lambda...
🌐
GitHub
github.com › golang › go › issues › 69288
`go build` produces bad error message when CGO_ENABLED=0 but need to build using cgo · Issue #69288 · golang/go
September 5, 2024 - https://pkg.go.dev/cmd/cgo does explain that cgo produces a build constraint, and even goes on to explain that CGO_ENABLED=0 is set if the default C compiler is not found (or CC is unset).
Author   golang
🌐
GitHub
github.com › golang › go › issues › 52816
Go command looks for runtime/cgo with CGO_ENABLED=0 · Issue #52816 · golang/go
May 10, 2022 - $ go env GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/home/fviel/poky/build/tmp/work/core2-64-poky-linux/backend/0.1+gitAUTOINC+f03090758d-r0/build/.cache" GOENV="/home/fviel/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/home/fviel/poky/build/tmp/work/core2-64-poky-linux/backend/0.1+gitAUTOINC+f03090758d-r0/build/pkg/mod" GONOPROXY="" GONOSUMDB="redacted" GOOS="linux" GOPATH="/home/fviel/poky/build/tmp/work/core2-64-poky-linux/backend/0.1+gitAUTOINC+f03090758d-r0/build" GOPRIVATE="" GOPROXY="redacted,https://proxy.golang.
Author   golang
Find elsewhere
Top answer
1 of 2
19

Many things are only available as C libraries, and re-implementing that all in Go would be costly. cgo has its downsides, but it can be a good trade-off. Even the standard library uses it (net for DNS lookups, os/user for user lookups) because it doesn't re-implement 100% of the behaviour in Go.

Cross-compiling C code is still rather hard; you'll need the target architecture's C compiler and toolchain (e.g. CC=aarch64-linux-musl-gccgo build to build an arm64 binary). None of that is installed by default so for most people cgo simply won't work when cross-compiling; they need to take manual steps to set it up first.

cgo often isn't strictly required (like in the net and os/user packages), so disabling it by default seems the most user-friendly option.

But there are no such constraints on the native platform, and it's expected to work by default without any user setup; so why not enable it by default?

2 of 2
8

If you're running on an Alpine image, it is impossible to compile and run Go programs in Alpine images right away. You must disable CGO by setting the environment variable CGO_ENABLED to false (the default value is true).

You can do this either by:

  • Adding go env -w CGO_ENABLED=0 like Robert mentions in his comment or,
  • Setting the env value prefixing it in the go command, e.g.: CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -ldflags '-s -w' -tags lambda.norpc -o bin/<YOUR_FUNC>/bootstrap <YOUR_PATH>/main.go

See: https://megamorf.gitlab.io/2019/09/08/alpine-go-builds-with-cgo-enabled/

🌐
Google Groups
groups.google.com › g › golang-nuts › c › dQxQ9O7u11g
Cross-compiling with the go tool
You can try "export CGO_ENABLED=0" (and maybe ./make.bash instead of ./all.bash).
🌐
OneUptime
oneuptime.com › home › blog › how to containerize go apps with multi-stage dockerfiles
How to Containerize Go Apps with Multi-Stage Dockerfiles
January 7, 2026 - # Stage 1: Build stage FROM golang:1.22-alpine AS builder WORKDIR /app # Install required packages RUN apk --no-cache add ca-certificates tzdata COPY go.mod go.sum* ./ RUN go mod download COPY . . RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \ -ldflags="-w -s" \ -a \ -installsuffix cgo \ -o main .
🌐
GitHub
github.com › aws › aws-xray-daemon › issues › 15
Provide statically linked binary (CGO_ENABLED=0) to allow smaller docker containers. · Issue #15 · aws/aws-xray-daemon
September 28, 2018 - # source for ca-bundle so we can do https requests FROM amazonlinux:2 as trusted # build statically linked binary FROM golang:1.11 as builder RUN go get -d -u github.com/aws/aws-xray-daemon/... RUN cd ${GOPATH}/src/github.com/aws/aws-xray-daemon && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/xray ./daemon/daemon.go ./daemon/tracing.go # start a container from scratch containing only necessary files FROM scratch COPY --from=trusted /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem COPY --from=builder /go/src/github.com/aws/aws-xray-daemon/build/xray /xray EXPOSE 2000 ENTRYPOINT ["/xray","--bind=0.0.0.0:2000"] If the binary i need was available (e.g.
Author   aws
🌐
GitHub
github.com › golang › go › issues › 60869
CGO_ENABLED=0 not working on latest golang image · Issue #60869 · golang/go
June 19, 2023 - I didn't expect to have any problem when starting the freshly build app on a different linux, but I did: /lib/x86_64-linux-gnu/libc.so.6: version GLIBC_2.32' not found (required by ...(my app)... ) /lib/x86_64-linux-gnu/libc.so.6: version GLIBC_2.34' not found (required by ...(my app)... ) I googled and set an CGO_ENABLED=0 environment property when building, expecting this to solve my problem.
Author   golang
🌐
Sailfish OS Forum
forum.sailfishos.org › applications
Go Applications Using Cgo - Applications - Sailfish OS Forum
November 18, 2020 - Has anyone got experience building go programs that rely on cgo? I’ve been trying with sb2 env GOARCH=arm GOOS=linux CGO_ENABLED=1 go build and I always run into the unrecognized command line option -marm on my system SDK. I confirmed that invoking gcc directly via sb2 is making arm binaries; ...
🌐
Stoolap
stoolap.io › blog › 2026 › 04 › 08 › calling-a-rust-library-from-go-with-cgo-disabled
Calling a Rust library from Go with CGO_ENABLED=0 | Stoolap
April 8, 2026 - You need to pin the glibc version, or statically link, or switch to musl and accept the behavioral differences. None of this is a blocker; it is friction for every user. A binary built with CGO_ENABLED=1 loses some portability.
🌐
OneUptime
oneuptime.com › home › blog › how to cross-compile go applications on ubuntu
How to Cross-Compile Go Applications on Ubuntu
March 2, 2026 - # Cross-compile for ARM 32-bit Linux GOOS=linux \ GOARCH=arm \ GOARM=7 \ CGO_ENABLED=1 \ CC=arm-linux-gnueabihf-gcc \ go build -o myapp-linux-armv7 . If possible, disable CGO to avoid the cross-compiler requirement: # Disable CGO entirely - pure Go compilation # Works as long as your code and all dependencies don't need C CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o myapp-linux-arm64 .
🌐
DoltHub
dolthub.com › blog › 2024-05-01-cgo-tradeoffs
Dolt Gets a cgo Dependency | DoltHub Blog
May 1, 2024 - In the case of cgo dependencies, this naturally extends to the system libraries that make up the toolchain, including the C and C++ standard libraries, the compiler support libraries, etc. The reality is that CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ./cmd/dolt with a specific version of the Go SDK has quite predictable behavior across a wide range of developer environments.