# 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.

Source: https://livops.dev/en/guides/error-go-sqlite3-requires-cgo

By Livops team, the people who build and run Livops. Updated 1 December 2026.

The error, as a Go program usually prints it when it opens the database at start (the log line is an example):

```text
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>-alpine` leaves out tools such as `gcc` to stay small[12], so cgo has no compiler to use. The Debian-based images install `gcc`, `g++` and `libc6-dev` for cgo[13].
- **Cross-compiling** for Linux from macOS or Windows, or for `arm64` from `amd64`: 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:

```sh
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:

```go
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]:

```go
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].

```sh
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`:

```dockerfile
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.sum` lists `go-sqlite3`. So on Livops this error usually means that command was replaced, or that the project is built from your own `Dockerfile` with `CGO_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](https://livops.dev/en/docs/error-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 the `Dockerfile` is used only when no framework was recognised; put `"builder": "dockerfile"` in `livops.json` before you create the project, since it is read when the project is created. See [Frameworks and livops.json](https://livops.dev/en/docs/frameworks). A web app must listen on the port in the `PORT` variable.
- **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](https://livops.dev/en/docs/databases). See [Files and data](https://livops.dev/en/docs/files).

What Livops does with a Go repository that has a `go.mod`, and running a Go web app in production, is in [Go hosting](https://livops.dev/en/hosting/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

1. [mattn/go-sqlite3 source — static_mock.go](https://github.com/mattn/go-sqlite3/blob/master/static_mock.go). Checked 1 December 2026.
2. [mattn/go-sqlite3 source — register.go](https://github.com/mattn/go-sqlite3/blob/master/register.go). Checked 1 December 2026.
3. [mattn/go-sqlite3 source — sqlite3.go](https://github.com/mattn/go-sqlite3/blob/master/sqlite3.go). Checked 1 December 2026.
4. [mattn/go-sqlite3 — README](https://github.com/mattn/go-sqlite3/blob/master/README.md). Checked 1 December 2026.
5. [Go — cmd/cgo](https://pkg.go.dev/cmd/cgo). Checked 1 December 2026.
6. [Go — database/sql](https://pkg.go.dev/database/sql). Checked 1 December 2026.
7. [Go — cmd/go (go version)](https://pkg.go.dev/cmd/go). Checked 1 December 2026.
8. [Go — runtime/debug (BuildSetting)](https://pkg.go.dev/runtime/debug). Checked 1 December 2026.
9. [modernc.org/sqlite — package documentation](https://pkg.go.dev/modernc.org/sqlite). Checked 1 December 2026.
10. [ncruces/go-sqlite3 — README](https://github.com/ncruces/go-sqlite3/blob/main/README.md). Checked 1 December 2026.
11. [GORM — Connecting to a Database](https://gorm.io/docs/connecting_to_the_database.html). Checked 1 December 2026.
12. [Docker Official Image golang — README](https://github.com/docker-library/docs/blob/master/golang/README.md). Checked 1 December 2026.
13. [docker-library/golang — 1.27/trixie/Dockerfile](https://github.com/docker-library/golang/blob/master/1.27/trixie/Dockerfile). Checked 1 December 2026.
