Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub: how to fix it
Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub means the program was built without cgo, so mattn/go-sqlite3 compiled its placeholder driver, which fails on every connection. Build with CGO_ENABLED=1 and a C compiler such as gcc, or switch to a pure-Go SQLite driver like modernc.org/sqlite, registered as sqlite rather than sqlite3, which builds with cgo off.
By Livops team, the people who build and run LivopsUpdated
The error, as a Go program usually prints it when it opens the database at start (the log line is an example):
2026/12/01 09:14:03 Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub
The text comes from github.com/mattn/go-sqlite3, word for word[1]. This page explains why the program compiled anyway, why cgo was off, and the two fixes: build with cgo and a C compiler, or move to a SQLite driver written in Go. It ends with how it looks on Livops.
What does the error mean?
go-sqlite3 is a cgo package: it compiles the SQLite C library into your program, and its README says you need CGO_ENABLED=1 and a gcc compiler on the PATH to build it[4].
When cgo is off, the go command does not set the cgo build constraint and skips every file that imports "C"[5]. go-sqlite3’s real driver is in such files, marked //go:build cgo[3]. What gets compiled instead is static_mock.go, marked //go:build !cgo: a stub whose Open returns exactly this error, as do the functions that register custom SQL functions and collations[1].
The stub is why the build gave no warning. The file that registers the driver under the name sqlite3 has no build constraint[2], so import _ "github.com/mattn/go-sqlite3" and sql.Open("sqlite3", ...) both work. sql.Open may just validate its arguments without creating a connection, and Go’s documentation points to db.Ping() to check that a connection works[6]. So the stub’s error comes back at Ping() or at the first query.
Why was cgo off?
The go command decides per build[5]:
| Situation | cgo |
|---|---|
| Native build, and a C compiler is found | On by default |
Cross-compiling (GOOS or GOARCH differs from the machine) |
Off by default |
CC is unset and the default C compiler, typically gcc or clang, is not on the PATH |
Off by default |
CGO_ENABLED=0 or CGO_ENABLED=1 set |
Whatever you set |
The usual ways a project lands in the stub:
- A Dockerfile built for a static binary. Multi-stage Dockerfiles often run
CGO_ENABLED=0 go build, so that the binary can be copied into a minimal image. With go-sqlite3 in the program, that line is the error. - The Alpine golang image.
golang:<version>-alpineleaves out tools such asgccto stay small[12], so cgo has no compiler to use. The Debian-based images installgcc,g++andlibc6-devfor cgo[13]. - Cross-compiling for Linux from macOS or Windows, or for
arm64fromamd64: cgo is off unless you turn it on and give it a cross C compiler[4][5]. - A CI runner or build server without gcc.
To see what your machine would do, and what an existing binary was built with:
go env CGO_ENABLED
go version -m ./app | grep CGO_ENABLED
Go records the build settings in the binary, including the effective CGO_ENABLED[8], and go version -m prints that embedded information[7].
Fix 1: switch to a SQLite driver that needs no cgo
If you do not need anything specific to go-sqlite3, this is the change that makes the problem go away for good: the program builds with cgo on or off, cross-compiles like any other Go code, and keeps the static binary.
modernc.org/sqlite
modernc.org/sqlite is a database/sql driver built on a CGo-free port of the SQLite C library[9]. It registers itself under the name sqlite, not sqlite3[9], so two lines change:
import (
"database/sql"
_ "modernc.org/sqlite" // was: _ "github.com/mattn/go-sqlite3"
)
db, err := sql.Open("sqlite", "file:app.db?_pragma=foreign_keys(1)") // was: "sqlite3"
Connection string options are not the same as go-sqlite3’s. modernc.org/sqlite runs each _pragma value as a PRAGMA statement, for example _pragma=foreign_keys(1), and takes _txlock as deferred (the default), immediate or exclusive[9]. Go through your DSN when you switch. The package lists the platforms it supports, among them linux/amd64, linux/arm64, darwin/arm64 and windows/amd64[9].
github.com/ncruces/go-sqlite3
Another cgo-free option: it wraps a Wasm build of SQLite translated to Go, and its only required dependencies are Go and x/sys[10]. Its database/sql driver is a separate package and keeps the sqlite3 name[10]:
import _ "github.com/ncruces/go-sqlite3/driver"
db, err := sql.Open("sqlite3", "file:app.db")
With GORM
gorm.io/driver/sqlite is, in GORM’s own words, a SQLite driver based on CGO, built on mattn/go-sqlite3, so it fails the same way[11]. GORM’s documentation names github.com/glebarez/sqlite and github.com/libtnb/sqlite as pure-Go SQLite drivers[11]; swapping the import is the GORM version of this fix.
Fix 2: build with cgo and a C compiler
If you want to keep go-sqlite3 — its build tags, its extensions, or code that uses its own API — give the build what it needs: CGO_ENABLED=1 and gcc[4].
CGO_ENABLED=1 go build -o app .
Install the compiler where the build runs. The go-sqlite3 README names the packages per system[4]:
| Build environment | What to install |
|---|---|
| Debian, Ubuntu | sudo apt-get install build-essential |
Alpine (including golang:*-alpine) |
apk add --update gcc musl-dev |
| Windows | a gcc toolchain such as TDM-GCC, with its bin folder on the PATH |
| macOS | Xcode’s developer tools |
The Debian-based official images (golang:1.27, which is golang:1.27-trixie) already include gcc[12][13], so in a Dockerfile it is enough to stop setting CGO_ENABLED=0:
FROM golang:1.27-trixie AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=1 go build -o /app .
FROM debian:trixie-slim
COPY --from=build /app /app
CMD ["/app"]
Run the result on the same Debian release it was built on, as above, rather than copying it into scratch or an Alpine image the way a CGO_ENABLED=0 binary can be. For a fully static binary with cgo, go-sqlite3’s README shows a musl cross-compiler with -ldflags "-linkmode external -extldflags -static"[4].
Cross-compiling needs a C cross-compiler as well: set CC to it, for example CC=arm-linux-gnueabihf-gcc CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=7 go build, as the README shows for ARM[4]. When that gets painful, Fix 1 is usually less work.
On Livops
- The default build already handles it. The build command Livops fills in for a Go repository, shown in Settings → Build, turns CGO on when
go.sumlistsgo-sqlite3. So on Livops this error usually means that command was replaced, or that the project is built from your ownDockerfilewithCGO_ENABLED=0. - Where it shows. A program that opens the database at start and exits on the error stops the deploy as
start_failed, and the log line is the app’s last words on the Logs page. A web app’s previous version keeps running meanwhile. See The app did not start. If the program connects only when a request first needs the database, it can start and answer, and the error shows in Logs when that request comes in. - The fix that works however the project is built: a pure-Go driver (Fix 1). It needs nothing from the build environment.
- Choosing the build yourself: a
Dockerfile, like the one in Fix 2. A Go repository is a recognised framework, and by default theDockerfileis used only when no framework was recognised; put"builder": "dockerfile"inlivops.jsonbefore you create the project, since it is read when the project is created. See Frameworks and livops.json. A web app must listen on the port in thePORTvariable. - Where the file goes. Write the SQLite file under the path in
DATA_DIR, the project’s own disk that is kept across deploys and restarts; for a project built from a Dockerfile it is/data. The disk belongs to one app and is not backed up, so once there are customers or payments, use a PostgreSQL database. See Files and data.
What Livops does with a Go repository that has a go.mod, and running a Go web app in production, is in Go hosting.
Questions
What does go-sqlite3 requires cgo to work. This is a stub mean?
The binary was built with cgo turned off. go-sqlite3 wraps the SQLite C library, so without cgo it compiles a placeholder driver instead, and that placeholder returns this error whenever a connection is opened. Nothing is wrong with the database file; the program has no SQLite in it.
Why did the program compile if the driver cannot work?
Because the placeholder is there on purpose. Without cgo, go-sqlite3 builds a stub file that still registers the sqlite3 driver name, so imports and sql.Open compile and succeed. sql.Open may not connect at all, so the error appears at the first Ping or query, often only when the program starts in production.
Why is CGO_ENABLED 0 when I never set it?
The go command turns cgo off by default when cross-compiling, and when CC is unset and no default C compiler such as gcc or clang is on the PATH. Minimal build images, the Alpine golang image among them, come without gcc. Many Dockerfiles also set CGO_ENABLED=0 on purpose to get a static binary.
Can I switch to modernc.org/sqlite without changing my code?
Almost. Replace the import with modernc.org/sqlite and change the driver name in sql.Open from sqlite3 to sqlite. Connection string options differ too: pragmas are passed as _pragma parameters, for example _pragma=foreign_keys(1). Then run your tests, since the two drivers are different code.
Does GORM have the same problem?
Yes with gorm.io/driver/sqlite, which GORM's documentation describes as a SQLite driver based on CGO, built on mattn/go-sqlite3. The same page names github.com/glebarez/sqlite and github.com/libtnb/sqlite as pure-Go SQLite drivers for GORM.
How do I check whether a binary was built with cgo?
Run go version -m on the binary. Go records the build settings in the binary, CGO_ENABLED among them, and go version -m prints that embedded information. go env CGO_ENABLED shows what the go command on your machine would use.
Sources
- mattn/go-sqlite3 source — static_mock.go (github.com). Checked .
- mattn/go-sqlite3 source — register.go (github.com). Checked .
- mattn/go-sqlite3 source — sqlite3.go (github.com). Checked .
- mattn/go-sqlite3 — README (github.com). Checked .
- Go — cmd/cgo (pkg.go.dev). Checked .
- Go — database/sql (pkg.go.dev). Checked .
- Go — cmd/go (go version) (pkg.go.dev). Checked .
- Go — runtime/debug (BuildSetting) (pkg.go.dev). Checked .
- modernc.org/sqlite — package documentation (pkg.go.dev). Checked .
- ncruces/go-sqlite3 — README (github.com). Checked .
- GORM — Connecting to a Database (gorm.io). Checked .
- Docker Official Image golang — README (github.com). Checked .
- docker-library/golang — 1.27/trixie/Dockerfile (github.com). Checked .
Put your project online today
Connect GitHub or drop a folder. No card is asked for.
Deploy your Go appRelated
- Golang hosting in 2026: how to deploy a Go appGolang hosting means running one compiled binary: build it with CGO off, listen on the PORT the host gives you, and stop cleanly on SIGTERM. Any host that runs a Linux process or a container will do. Koyeb and Fly.io have the lowest always-on dollar prices, Google Cloud Run and Render have free tiers, and a VPS is cheapest when you run many services. On Livops you push a repository with go.mod and it runs as a web app for 39 credits a month, on servers in Uzbekistan. Below: what Go needs, a deploy checklist, and the options with their own prices.
- go: go.mod requires go >= 1.24 (running go 1.22; GOTOOLCHAIN=local): how to fix itgo: go.mod requires go >= 1.24 (running go 1.22; GOTOOLCHAIN=local) means the go line in go.mod asks for a newer Go than the one running, and GOTOOLCHAIN=local forbids fetching another. Since Go 1.21 the go line is a hard minimum. Build with a Go at least that new, let GOTOOLCHAIN=auto download it, or lower the go line if your code and dependencies allow. toolchain not available means that download failed.
- sqlite3.OperationalError: unable to open database file (and attempt to write a readonly database): how to fix itsqlite3.OperationalError: unable to open database file means SQLite could not open or create the file at the path it was given: the folder does not exist, a relative path was resolved from another working directory, or the folder is not writable. Attempt to write a readonly database means the file opened, but only for reading. SQLite also writes -journal and -wal files next to the database, so the folder must be writable, not just the file.
- Docker hosting: how to deploy a Docker container or a DockerfileTo host a container you need a host that builds or pulls the image, starts its CMD, sends traffic to the port it listens on and stops it with SIGTERM. On Livops a Dockerfile at the root of the repository is built on Livops's servers and runs for 39 credits a month (39,000 so'm); a public image from Docker Hub, GHCR or Quay runs without a build. Here is what a container needs, and how a VPS, Render, Railway, Fly.io, DigitalOcean and Heroku compare.
- exec format error in Docker: the image was built for another CPUThe kernel refused to run the container's program because it is not an executable for this machine: almost always a binary or image built for arm64 (an Apple Silicon Mac) running on an amd64 server, or the reverse. Build for the server's platform with --platform linux/amd64, or as a multi-platform image; if the file is a script, give it a #! line.
- App not listening on $PORT: why the health check failed after deployThe platform started your app, tried the port it gave it, and nothing answered in time. Almost always the app listens on its own fixed port instead of the PORT variable, listens on localhost (127.0.0.1) instead of 0.0.0.0, or crashed or was still starting when the check gave up. Listen on 0.0.0.0 and the port in PORT, and it passes.