distroproof

How to fix "exec format error"

The kernel was asked to run a file it does not know how to execute. It is usually a binary for another CPU architecture, such as x86-64 on ARM, or a script without a shebang line.

exec /usr/local/bin/mytool: exec format error
bash: ./mytool: cannot execute binary file: Exec format error

Compare the file with the machine

uname -m
file ./mytool

If uname -m prints aarch64 and file says x86-64, the architectures do not match. x86_64 and aarch64 are the usual pair, for example on AWS Graviton, Raspberry Pi 4 and Linux containers on Apple Silicon.

How to fix it

  1. Use the matching build. Publish one binary per architecture and have your installer choose by uname -m.
  2. Build multi-architecture images. docker buildx build --platform linux/amd64,linux/arm64 -t myimage --push . publishes both under one tag.
  3. Add the shebang. A script run directly needs a first line such as #!/bin/sh, with nothing before it.
  4. Check for a corrupt download. If file reports HTML or text, the download saved an error page instead of the binary.

Emulation hides this until production

Docker with QEMU can run foreign architecture images, so a test may pass on one machine and fail on a server without emulation set up. Emulation is also slower and does not always behave like the real hardware, so test important releases on real hardware of that architecture.