This should work:

Copygo env -w CGO_ENABLED=1

But if you don't have a C compiler installed on your machine, you will get another error message after setting this variable and trying to use go run -race .:

cgo: C compiler "gcc" not found: exec: "gcc": executable file not found in %PATH%

If so, here are VS Code's instructions on how to install it

  • Windows: https://code.visualstudio.com/docs/cpp/config-mingw
  • Linux: https://code.visualstudio.com/docs/cpp/config-linux
Answer from Krista Korund on Stack Overflow
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/

Discussions

What is the consequence of using CGO_ENABLED=0?

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

More on reddit.com
🌐 r/golang
33
41
September 5, 2021
go - Why is CGO_ENABLED=1 default? - Stack Overflow
CGO_ENABLED=1 I believe is the current default which means that it depends on GLIBC which can have breaking changes between updates & distributions. CGO_ENABLED=0 is the workaround for creating More on stackoverflow.com
🌐 stackoverflow.com
Getting CGO_ENABLED=0 when running a go project that uses github.com/mattn/go-sqlite3 library?
No, CGO is not enabled by default; you'll need to provide a C compiler for your target architecture. Alternatively, if you don't care that much about performance, there's a pure-Go version: https://gitlab.com/cznic/sqlite More on reddit.com
🌐 r/golang
20
8
June 3, 2023
Go Service Error - CGO_ENABLED Required (IBM DB2)
It seems the service requires CGOENABLED=1 to be enabled because it utilizes the IBM DB2 library, which relies on CGO. You can find the library here: https://github.com/ibmdb/goibm_db. Does anyone have suggestions on how to achieve this CGO_ENABLED setting for this service? More on station.railway.com
🌐 station.railway.com
11
0
July 29, 2024
🌐
Boinkor
boinkor.net › 2023 › 05 › building-a-golang-program-with-cgo
Building a golang program with cgo - Andreas Fuchs’ Journal
May 25, 2023 - As with all semi-taboo knowledge1, ... go programs). Well, we’ll show them! There are two main things you need to do: First, explicitly opt out of disabling the usage of the cgo compiler by setting CGO_ENABLED=1 on the compiler’s process environment....
🌐
Medium
medium.com › @pengcheng1222 › exploring-cgo-enabled-in-go-23cf5cf2fe88
Golang — Exploring CGO_ENABLED in Go | by Allen Ning | Medium
November 20, 2024 - By default, its value varies based ... macOS, and Linux (for amd64, 386, and arm architectures), enabling the integration of C libraries in Go applications....
🌐
Go Packages
pkg.go.dev › cmd › cgo
cgo command - cmd/cgo - Go Packages
April 7, 2026 - It is disabled by default when ... can override the default by setting the CGO_ENABLED environment variable when running the go tool: set it to 1 to enable the use of cgo, and to 0 to disable it....
🌐
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.

🌐
GoLinuxCloud
golinuxcloud.com › home › programming › getting started with cgo using visual studio code
Getting started with CGO using Visual Studio Code | GoLinuxCloud
January 7, 2024 - You can control this by setting the CGO_ENABLED environment variable when running the go tool: set it to 1 to enable the use of cgo, and to 0 to disable it.
Find elsewhere
🌐
Reddit
reddit.com › r/golang › getting cgo_enabled=0 when running a go project that uses github.com/mattn/go-sqlite3 library?
Getting CGO_ENABLED=0 when running a go project that uses github.com/mattn/go-sqlite3 library? : r/golang
June 3, 2023 - No, CGO is not enabled by default; you'll need to provide a C compiler for your target architecture. Alternatively, if you don't care that much about performance, there's a pure-Go version: https://gitlab.com/cznic/sqlite
🌐
Railway
station.railway.com › home › questions › go service error - cgo_enabled required (ibm db2)
Go Service Error - CGO_ENABLED Required (IBM DB2) - Railway Help Station
July 29, 2024 - It seems the service requires CGO_ENABLED=1 to be enabled because it utilizes the IBM DB2 library, which relies on CGO. You can find the library here: https://github.com/ibmdb/go_ibm_db.
🌐
GitHub
github.com › golang › go › issues › 21895
cmd/go: misleading message CGO_ENABLED=0 with -msan says `go run: -race requires cgo` · Issue #21895 · golang/go
September 14, 2017 - What did you do? $ CGO_ENABLED=0 go run -msan main.go What did you expect to see? go run: -msan requires cgo; enable cgo by setting CGO_ENABLED=1 What did you see instead? go run: -race requires cg...
Author   golang
🌐
Bytebase
bytebase.com › blog › engineering › how to cross compile with cgo using goreleaser and github actions
How to cross compile with CGO using GoReleaser and GitHub Actions | Bytebase
August 24, 2022 - To enable CGO in GoReleaser, you only need to add "CGO_ENABLED=1" to the corresponding "env" entry in the GoReleaser configuration file. Let's retry. This error looks strange. We can only know that there is something wrong with CGO.
🌐
Matrix
matrix-org.github.io › go-neb › pkg › C › index.html
cgo - The Go Programming Language
It is disabled by default when cross-compiling. You can control this by setting the CGO_ENABLED environment variable when running the go tool: set it to 1 to enable the use of cgo, and to 0 to disable it.
🌐
CodeGenes
codegenes.net › blog › when-using-cgo-enabled-is-must-and-what-happens
When is CGO_ENABLED a Must? Understanding the Go Compilation Process and Unavoidable Scenarios with C Libraries — codegenes.net
These packages rely on CGO to bridge the gap, so CGO_ENABLED=1 is required to compile them. SQLite: The widely used go-sqlite3 driver (used by ORMs like GORM) wraps the C-based libsqlite3 library.
🌐
GitHub
github.com › golang › go › issues › 32287
go test does not work without CGO_ENABLED=1 · Issue #32287 · golang/go
May 28, 2019 - $ cd ~/go/path/to/program $ go test ./pkg/... # runtime/cgo exec: "gcc": executable file not found in $PATH FAIL path/to/program/pkg/subpkg [build failed] Running CGO_ENABLED=0 go test ./pkg/... works without issues
Author   golang
🌐
JetBrains
youtrack.jetbrains.com › issue › GO-5016 › GoLand-does-not-pass-CGO-variable-during-build
GoLand does not pass CGO variable during build : GO-5016
{{ (>_<) }} This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong
🌐
GitHub
github.com › golang › go › issues › 70868
cmd/go: implicit CGO_ENABLED=1 in go env can be misleading · Issue #70868 · golang/go
December 16, 2024 - go env should give the correct information about CGO_ENABLED and the above program should fail to compile always if CGO_ENABLED=1 is present in the go env. When using go1.23.4, getting similar issue. i.e irrespective of whether the system env ...
Author   golang
🌐
NixOS Discourse
discourse.nixos.org › help
CGO_ENABLED='1' starting go 1.25 - Help - NixOS Discourse
December 2, 2025 - Around the time of go 1.20 I saw an announcement that CGO_ENABLED='0' was no longer necessary, so I’ve migrated my flakes to use mkShellNoCC. I updated today to latest unstable (using default pkgs.go which resolves now t…