[SOLVED]: exec no such file or directory Error with Go Binary

"exec no such file or directory" is a common error faced by golang developers, who are  containerizing the microservice. Different articles in the internet have possible reasons and its mitigation. But none of them fixed the issue faced by me, when i was testing out my code in a scratch container image. Let's explore all the reasons and its possible solutions for fixing the error.

Understanding the Error exec no such file or directory

When facing the error while executing the golang binary, it usually means that the arch of the  binary we created is not compatible with the arch of the system which we are trying to execute. But the same error can also some times (that too in rare of the rare cases) means, that the binary file is missing in the path.

Common causes and fixes for the Error exec no such file or directory

Executing a 32 bit binary on a 64 bit system

We need to ensure that the binary and the system is of same bit system. This can be achieved by executing the two commands

#Verify the binary 
$/] file bin/data-sample-agent 
bin/data-sample-agent: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=Jazxxxxxkc7TIcar9o4/c2haB8rItJrNXwK0iur9/QXWdIxxxxxxWHo2eHNfuReQ5fbskW3a, with debug_info, not stripped

Verify the same for the machine.

getconf LONG_BIT

Output of the above command will be 32 or 64, which shows the system bit

Executing amd64 binary on a arch64 system

Ensure that the binary and the machine which we are trying to run is of same arch either x86_64 (amd64) or  arm64

Verify the arch of the binary using below command

#Verify the binary 
$/] file bin/data-sample-agent 
bin/data-sample-agent: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=Jazxxxxxkc7TIcar9o4/c2haB8rItJrNXwK0iur9/QXWdIxxxxxxWHo2eHNfuReQ5fbskW3a, with debug_info, not stripped

Verify the same for the machine.

$/] uname -m
arm64

Fix for the above issue is to cross compile the go binary by giving required envs. This can be achieved by.

$/] env GOOS=linux GOARCH=amd64 go build -o bin/data-sample-agent .

Replace the above c0mmand's GOARCH env for arm64, when compiling the binary for arm based system.

Compiling Binary with CGO_ENABLED flag

Ideally golang binaries are   statically linked. But enabling CGO_ENABLED flag will allow the golang binary to use other existing C libraries in the system, that are essential for the application to work.

When we are using multi-stage docker build, we usually compile the binary in the build stage, that have all the dependencies and will move the binary to a scratch container image, and if the CGO_ENABLED was set to 1, while compiling the binary will be expecting to have some C libraries in the run environment as well, which will be missing in the scratch image.
Inorder to overcome this issue, set the CGO_ENABLED flag to 0.

#/] env CGO_ENABLED=0 go build -o bin/data-sample-agent .

References

Search on LinuxDataHub

Leave a Comment