Go "exec format error" running a cross-compiled binary in CI
The kernel refused to run the binary because its machine format does not match the host. A Go binary cross-compiled for, say, linux/arm64 cannot run on a linux/amd64 runner.
What this error means
A step that runs a freshly built Go binary fails with "exec format error" or "cannot execute binary file: Exec format error", usually after a cross-compile build.
./bin/app: cannot execute binary file: Exec format errorCommon causes
GOARCH or GOOS does not match the runner
The build set GOARCH=arm64 (or another target) but the job runs the binary on an amd64 Linux runner, which cannot execute it.
A multi-arch image mismatch
The binary was built for one architecture and copied into an image or runner of another, so exec fails at runtime.
How to fix it
Build for the platform that runs it
- Decide whether you want to run the binary in CI or only release it.
- For a binary you run, build with the runner GOOS/GOARCH (the defaults).
- For a release artifact, build the cross target but do not execute it on the runner.
# build for the runner so it can run here
go build -o bin/app ./cmd/app
./bin/app --versionRun foreign binaries under emulation
To run a cross-built binary on the runner, register QEMU emulation (for example via docker/setup-qemu-action) before executing it.
- uses: docker/setup-qemu-action@v3How to prevent it
- Separate "build and run here" jobs from "cross-compile for release" jobs.
- Only set GOOS/GOARCH when producing artifacts you will not execute locally.
- Use QEMU when a foreign-arch binary genuinely must run in CI.