How to Fix Golang cannot execute binary file: exec format error

Go raises the cannot execute binary file: exec format error “when a binary file for a specific processor architecture is run on a different architecture”, e.g., an x86 executable is run on an ARM CPU.

To fix the “cannot execute binary file: exec format” error, you can follow these steps:

  1. Verify system architecture
  2. Download the correct binary
  3. Build from the source
  4. Install the Go compiler and build the binary
  5. Make the binary executable
  6. Run the binary

Step 1: Verify system architecture

Check your system’s architecture (32-bit or 64-bit) and make sure you have the correct binary for your system. You can run uname -m in the terminal to check your system’s architecture.

Step 2: Download the correct binary

If you installed the wrong binary, download the correct version for your system from the official source or build it yourself.

Step 3: Build from the source

If you have the source code for the Go program, you can try building it yourself. First, ensure you have the Go compiler installed on your system. Then, you can download it from the official Go website: https://go.dev/dl/.

Step 4: Install the Go compiler and build the binary

Once you have the Go compiler installed, navigate to the directory containing the source code and run the following command to build the binary:

go build

This will create an executable binary in the current directory. Make sure the binary is compatible with your system’s architecture.

Step 5: Make the binary executable

Ensure that the binary file has executable permissions. You can use the chmod command to add executable permissions.

chmod +x your_binary_file

Step 6: Run the binary

Now you can run the binary without the “Exec format error”. Execute the binary with ./your_binary_file.

If you still encounter the error after following these steps, the binary file might be corrupt or an issue with your system’s environment.

In this case, you might need assistance from the software developers or your system administrator.

That’s it.

Leave a Comment