38

my go env

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT=""
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

I compiled and successfully executed the binary on my mac and then copied it to the ubuntu machine whose go env is show above. When I call myprog binary, I get

bash: /usr/local/go/bin/myprog: cannot execute binary file: Exec format error
2
  • 4
    You already compiled the binary for Darwin. That same binary can't run on Linux. You need to compile a Linux version. Commented Mar 24, 2016 at 11:09
  • getting this error while working with docker . i have build a image on docker and trying to run the binary on same mac OS . Commented Aug 15, 2020 at 8:58

5 Answers 5

61

Since go 1.5, cross compiler has gotten pretty easy. Something like

env GOOS=linux GOARCH=amd64 go build -v github.com/constabulary/gb/cmd/gb

Refer to http://dave.cheney.net/2015/08/22/cross-compilation-with-go-1-5.

Sign up to request clarification or add additional context in comments.

1 Comment

Error: /usr/local/go/bin/go: 3: Syntax error: Unterminated quoted string
16

I had the same problem. I installed 64-bit version of go instead of 32-bit version. After installation of 32-bit version it works fine.

2 Comments

Thanks, I did the same mistake.. downloaded the wrong file architecture (Linux instead of ARM)
I downloaded arm version and that was the problem. Solved installing right AMD version after checking my linux architecture using uname -a unix.stackexchange.com/a/12454/429777
5

This happened to me when I was downloading the x86 version of golang, but the VM i provisioned was an ARM cpu.

You can get ARM downloads here: https://go.dev/dl/

1 Comment

Thank you, that is exactly the issue when trying to install Go on Ubuntu 22.04 emulated through UTM on Apple M1 machines. The version of Ubuntu they recommend is actually arm64 architecture, so the appropriate Go distributive really needs to have a combo of "linux" and "arm64" features. It's a pity that the Go's landing page only offers only the amd version to download and tucks away all the other versions: if you aren't sure what version you need, you'll go with the default (probably wrong) one.
1

It may be the case that your binary is dynamically linked to some C-libraries, which are not installed on your OS. This can happen if you e.g. compile in a different environment than where you run your binary.

To get some info about your binary, you can both run file ./your-binary and ld ./your-binary.

I recommend this post for some solutions to that problem: Go-compiled binary won't run in an alpine docker container on Ubuntu host

Basically, your options are:

  • run on the same architecture/OS as you compile
  • install the missing libraries on your target OS
  • try to statically compile

Comments

0

On windows, make sure you(or your IDE) is not running PowerShell - powershell will not take the set GOOS and not throw an error and will still compile the binary and you'll find this error when you deploy the binary to the server.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.