29

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
tod
  • 1,539
  • 4
  • 17
  • 43
mg03
  • 667
  • 2
  • 9
  • 14
  • 4
    You already compiled the binary for Darwin. That same binary can't run on Linux. You need to compile a Linux version. – JimB Mar 24 '16 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 . – Sharath BJ Aug 15 '20 at 08:58

5 Answers5

54

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.

WeakPointer
  • 3,087
  • 27
  • 22
12

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.

Alex Dvoretskiy
  • 317
  • 2
  • 9
  • Thanks, I did the same mistake.. downloaded the wrong file architecture (Linux instead of ARM) – Bilal Jul 26 '20 at 21:36
  • 2
    I downloaded arm version and that was the problem. Solved installing right AMD version after checking my linux architecture using `uname -a` https://unix.stackexchange.com/a/12454/429777 – Gudari Oct 06 '20 at 15:31
2

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/

fafrd
  • 1,017
  • 13
  • 17
  • 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. – Emile Zäkiev Jan 11 '23 at 09:27
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
Beolap
  • 768
  • 9
  • 15
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.

DMin
  • 10,049
  • 10
  • 45
  • 65