How to Fix Cannot Execute Binary File Exec Format Error in Linux

Executing compiled command-line applications, custom utility binaries, or shell automation scripts is the core of working in the Linux terminal. However, running a downloaded binary with ./app only to be halted by the error message bash: ./app: cannot execute binary file: Exec format error (kernel error code ENOEXEC) prevents software execution. This error indicates that the Linux kernel cannot parse the binary file’s ELF header or that the binary was compiled for an incompatible CPU architecture.

Why Linux Returns “Exec format error”

The Linux kernel’s binary loader reads the initial bytes of any executable file before creating a process:

  • CPU Architecture Mismatch: Attempting to run an ARM64 (aarch64) binary on an x86_64 Intel/AMD processor, or running an x86_64 binary on a Raspberry Pi or Apple Silicon Linux VM.
  • 32-Bit vs 64-Bit Incompatibility: Running a 32-bit executable on a 64-bit Linux distribution that lacks 32-bit multiarch runtime libraries.
  • Missing or Malformed Shebang in Shell Scripts: A shell or Python script marked executable (chmod +x) lacking the interpreter line (#!/usr/bin/env bash) at byte 0.
  • Corrupted or Incomplete File Download: A truncated binary file download missing standard ELF (Executable and Linkable Format) magic headers.
  • Windows PE/EXE Binary Executed on Linux: Attempting to run a native Windows .exe file without an emulator like Wine.

1. Identify Architecture of the Binary vs System CPU

Comparing the compiled architecture of the file against your physical processor pinpoints mismatches immediately.

  1. Check your system’s CPU architecture:
    uname -m
    (Common outputs: x86_64 for Intel/AMD 64-bit; aarch64 or armv7l for ARM).
  2. Check the architecture of the problematic binary using the file command:
    file ./app
  3. Examine the output:
    • If uname -m returns x86_64 but file ./app says ELF 64-bit LSB executable, ARM aarch64, you downloaded the wrong CPU build.
    • Download the corresponding x86_64 (amd64) release from the vendor’s repository.

2. Fix Missing or Broken Shebang in Shell Scripts

If the file is a text-based script, the kernel requires a valid shebang line to select the interpreter.

  1. Open the script with a text editor:
    nano ./script.sh
  2. Verify that the very first line contains a valid interpreter path:
    #!/usr/bin/env bash
    (For Python scripts: #!/usr/bin/env python3).
  3. Ensure there are no blank lines or spaces before the shebang.
  4. Check for Windows DOS line endings (CRLF) using dos2unix:
    dos2unix ./script.sh
  5. Save the file, reapply execution permissions (chmod +x ./script.sh), and run it.

3. Enable 32-Bit Multiarch Support (Debian/Ubuntu)

Modern 64-bit Linux systems require 32-bit architecture packages to execute legacy 32-bit binaries.

  1. If file ./app reports ELF 32-bit LSB executable, Intel 80386:
    sudo dpkg --add-architecture i386
    sudo apt update
    sudo apt install libc6:i386 libncurses5:i386 libstdc++6:i386 -y
  2. Once multiarch compatibility libraries are installed, re-run the 32-bit application.

4. Verify File Integrity and Re-download

Incomplete downloads corrupt ELF magic numbers (0x7F 'E' 'L' 'F').

  1. Inspect the top 4 bytes of the binary:
    head -c 4 ./app | xxd
  2. The output should start with 7f45 4c46. If it starts with HTML or error text (e.g. <!DO), your download tool received a web error page rather than the actual binary.
  3. Re-download the file directly using wget -O app <URL> or curl -L -O <URL>.

5. Run Windows Binaries via Wine

If the file is a native Windows PE32 executable, Linux cannot execute it natively.

  1. Install Wine:
    sudo apt install wine -y
  2. Execute the program through the Wine translation layer:
    wine ./app.exe

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.