It looks like your GOOS is getting set to "linux " (with an extra space at the end), not "linux".

Answer from hobbs on Stack Overflow
🌐
GitHub
gist.github.com › asukakenji › f15ba7e588ac42795f421b48b8aede63
Go (Golang) GOOS and GOARCH · GitHub
Thank you for reminding and thank you for supporting my gist! ... There is now a new GOAMD64 https://utcc.utoronto.ca/~cks/space/blog/programming/GoAmd64ArchitectureLevels ... Currently, linux/arm and linux/arm64 are supported.
🌐
Opensource.com
opensource.com › article › 21 › 1 › go-cross-compiling
Cross-compiling made easy with Golang | Opensource.com
January 14, 2021 - #!/usr/bin/bash archs=(amd64 arm64 ppc64le ppc64 s390x) for arch in ${archs[@]} do env GOOS=linux GOARCH=${arch} go build -o prepnode_${arch} done
Discussions

go - How to cross compile from Windows to Linux? - Stack Overflow
I've installed Go 1.2 on a Windows machine, wrote up a dummy program and set the environment variables GOARCH and GOOS to "AMD64" and "linux" respectively. When i issue the "go build" command, i r... More on stackoverflow.com
🌐 stackoverflow.com
linux - Cross Compiling Go - Stack Overflow
What it's saying you need to do is rebuild the library and runtime for linux-amd64. You can do that this way: Find the root of your Go installation (if you don't know where this is, running which go may help - the binary is often installed with the rest of the sources). ... Run GOOS=linux GOARCH=a... More on stackoverflow.com
🌐 stackoverflow.com
docker - How to create Golang Linux binaries using a Windows host - DevOps Stack Exchange
When go build is issued on a Windows host a .exe binary is created. How to ensure that a Linux binary is created on a Windows host? The following was tried, but did not solve the issue: GOOS=linu... More on devops.stackexchange.com
🌐 devops.stackexchange.com
GOOS=linux GOARCH=amd64 go build go build github.com/ethereum/go-ethereum/crypto/secp256k1: build constraints exclude allGo files in /Users/mac/Documents/project/src/github.com/ethereum/go-ethereum/crypto/secp256k1
Version: 1.8.8-stable Architecture: amd64 Protocol Versions: [63 62] Network Id: 1 Go Version: go1.10.2 Operating System: darwin GOOS=linux GOARCH=amd64 go build go build github.com/ethereum/go-eth... More on github.com
🌐 github.com
8
May 28, 2018
Top answer
1 of 5
64

It tells you it needs all tools built before you can use them.

If your windows GOARCH is amd64, then you could "build" all required tools by running this small batch programs:

set GOARCH=amd64
set GOOS=linux
go tool dist install -v pkg/runtime
go install -v -a std

If that succeeds then you should be able to do what you've described (just use amd64, not AMD64 - it is case sensitive).

If your windows GOARCH is 386, then you would need to build your 386 tools first. You would need to download mingw gcc for that. Do what user2714852 said.

Here https://golang.org/wiki/WindowsCrossCompiling are similar instructions for linux, perhaps you find them helpful.

Alex

2 of 5
41

I was having some major problems with building for linux from windows, At the end of the day, it was fairly simple. I would comment on Alex's post, but I can not as I am a stackoverflow newb.

As alex said, set the environment variables. This must be done as administrator (eg right click the "command prompt" or "Powershell" shortcut and click "Run as Administrator")

set GOARCH=amd64
set GOOS=linux

If you use Powershell, use

$Env:GOOS = "linux"; $Env:GOARCH = "amd64"

If you dont do it as administrator, the variables wont take effect and you will just be building it for the OS & Architecture you are on at the moment.

I found its always good to check your go environment vars by running go env, which gives you the list of current go environment variables

go env
set GOARCH=amd64
set GOBIN=
set GOEXE=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=linux
set GOPATH=T:\Projects\gopath
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set GOGCCFLAGS=-fPIC -m64 -fmessage-length=0
set CXX=g++
set CGO_ENABLED=0
set PKG_CONFIG=pkg-config
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2

Make sure the GOOS & GOARCH are set to the values you specified earlier.

If thats all good, you should be able to navigate to the directory containing your go code, and from the command line run:

go build

Which will build the package for the system and the architecure that are set in the environment variables.
I encountered some other issues once I finally figured this out, but that is another matter not related to this issue.

Top answer
1 of 3
31

What it's saying you need to do is rebuild the library and runtime for linux-amd64. You can do that this way:

  1. Find the root of your Go installation (if you don't know where this is, running which go may help - the binary is often installed with the rest of the sources).
  2. cd into the src directory
  3. Run GOOS=linux GOARCH=amd64 ./make.bash --no-clean (or GOOS=linux GOARCH=amd64 bash make.bash --no-clean if make.bash is not executable). This will rebuild the library and runtime using the specified OS and architecture.

Once you've done this, you can build a go package or binary for this architecture using GOOS=linux GOARCH=amd64 go build. You can follow the same instructions for other architectures and operating systems.

Edit (08/13/15):

As of Go 1.5, cross compiling is much easier. Since the runtime is written in Go, there's no need to set anything up in order to be able to cross-compile. You can now just run GOOS=<os> GOARCH=<arch> go build from a vanilla Go installation and it will work.

However, there's one exception to this. If you're using cgo, you'll still need to set stuff up ahead of time. And you'll need to inform the tooling that you want to enable cgo cross-compiling by setting the CGO_ENABLED environment variable to 1. So, to be precise:

  1. cd into the src directory of your Go installation (see the instructions above).
  2. Run CGO_ENABLED=1 GOOS=<os> GOARCH=<arch> ./make.bash --no-clean
  3. Run CGO_ENABLED=1 go build to build your project. It is important that you specify CGO_ENABLED=1 even when you're compiling.
2 of 3
1

Following the above answer https://stackoverflow.com/a/27413148/3675575, I needed to set GOROOT_BOOTSTRAP to recompile my GO source tree:

GOROOT_BOOTSTRAP=/usr/lib/golang/ CGO_ENABLED=1 GOOS=linux GOARCH=386 ./make.bash --no-clean

(I'm using Fedora 23, so the GOROOT_BOOTSTRAP may vary in your operating system)

🌐
GitHub
github.com › ethereum › go-ethereum › issues › 16818
GOOS=linux GOARCH=amd64 go build go build github.com/ethereum/go-ethereum/crypto/secp256k1: build constraints exclude allGo files in /Users/mac/Documents/project/src/github.com/ethereum/go-ethereum/crypto/secp256k1 · Issue #16818 · ethereum/go-ethereum
May 28, 2018 - Version: 1.8.8-stable Architecture: amd64 Protocol Versions: [63 62] Network Id: 1 Go Version: go1.10.2 Operating System: darwin GOOS=linux GOARCH=amd64 go build go build github.com/ethereum/go-eth...
Author   ethereum
Find elsewhere
🌐
Ecostack
ecostack.dev › posts › go-and-cgo-cross-compilation
Go: Cross-Compilation Including Cgo · Ecostack
May 24, 2024 - The first one for example, aix/ppc64, would be defined as GOOS=aix GOARCH=ppc64 before the actual compile command. > go tool dist list | column -c 35 | column -t aix/ppc64 linux/mips64le android/386 linux/mipsle android/amd64 linux/ppc64 android/arm linux/ppc64le android/arm64 linux/riscv64 darwin/amd64 linux/s390x darwin/arm64 netbsd/386 dragonfly/amd64 netbsd/amd64 freebsd/386 netbsd/arm freebsd/amd64 netbsd/arm64 freebsd/arm openbsd/386 freebsd/arm64 openbsd/amd64 freebsd/riscv64 openbsd/arm illumos/amd64 openbsd/arm64 ios/amd64 openbsd/ppc64 ios/arm64 plan9/386 js/wasm plan9/amd64 linux/386 plan9/arm linux/amd64 solaris/amd64 linux/arm wasip1/wasm linux/arm64 windows/386 linux/loong64 windows/amd64 linux/mips windows/arm linux/mips64 windows/arm64
🌐
Freshman Tech
freshman.tech › snippets › go › cross-compile-go-programs
How to Cross-Compile Go Programs for Windows, macOS, and Linux
February 7, 2024 - Go programs can be easily cross-compiled for various operating systems like Windows, macOS, and Linux using GOARCH and GOOS environment variables, which denote the architecture and target OS respectively.
🌐
Google Groups
groups.google.com › g › golang-nuts › c › dQxQ9O7u11g
Cross-compiling with the go tool
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build $NAME -o ${NAME}_linux and then I copy it over (and the harness notices and restarts). Works quite nicely.
🌐
GitHub
github.com › mattn › go-sqlite3 › issues › 756
osx build fail CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build error reporting · Issue #756 · mattn/go-sqlite3
November 14, 2019 - os version :macos 10.15.1 go version: ... GOARCH="amd64" GOBIN="" GOCACHE="/Users/luojinyi/Library/Caches/go-build" GOENV="/Users/luojinyi/Library/Application Support/go/env" GOEXE="" GOFLAGS=" -mod=vendor" GOHOSTARCH="amd64" GOHOSTOS="darwin" ...
Author   mattn
🌐
Ardan Labs
ardanlabs.com › blog › 2013 › 10 › cross-compile-your-go-programs.html
Cross Compile Your Go Programs
October 2, 2013 - Now build the code for linux/amd64: export GOARCH="amd64" export GOOS="linux" go build file simple Simple: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped You need to change either one or both of the OS/ARCH environment variables to point to the target platform and architecture.
🌐
GitHub
gist.github.com › zfarbp › 121a76d5a3fde562c3955a606a9d6fcc
Golang - Building Executables for Different Architectures · GitHub
# Example env GOOS=darwin GOARCH=amd64 go build env GOOS=darwin GOARCH=amd64 go build main.go env GOOS=darwin GOARCH=amd64 go build github.com/zoo/york/foo/bar # Raspberry pi env GOOS=linux GOARCH=arm GOARM=5 go build · Copy link · Copy Markdown · Thanks · Copy link ·
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-build-go-executables-for-multiple-platforms-on-ubuntu-16-04
How To Build Go Executables for Multiple Platforms on Ubuntu 16.04 | DigitalOcean
March 18, 2022 - Cross-compiling works by setting required environment variables that specify the target operating system and architecture. We use the variable GOOS for the target operating system, and GOARCH for the target architecture.
Top answer
1 of 3
102

Note that those values are defined in:

  • src/internal/syslist/syslist.go, and
  • doc/install/source#environment.

With Go 1.5 (Q3 2015), GOARCH will become much more complete.
See commit 1eebb91 by Minux Ma (minux)

go/build: reserve GOARCH values for all common architectures

Whenever we introduce a new GOARCH, older Go releases won't recognize them and this causes trouble for both our users and us (we need to add unnecessary build tags).

Go 1.5 has introduced three new GOARCHes so far: arm64 ppc64 ppc64le, we can take the time to introduce GOARCHes for all common architectures that Go might support in the future to avoid the problem.

Copyconst goosList = "android darwin dragonfly freebsd linux nacl \ 
  netbsd openbsd plan9 solaris windows "

const goarchList = "386 amd64 amd64p32 arm arm64 ppc64 ppc64le \
   mips mipsle mips64 mips64le mips64p32 mips64p32le \ # (new)
   ppc s390 s390x sparc sparc64 " # (new)

The list is still being review in Change 9644, with comments like:

I wouldn't bother with Itanium. It's basically a dead architecture.
Plus, it's so hard to write a compiler for it that I really can't see it happening except as a labor of love, and nobody loves the Itanium.

The official documentation now (GO 1.5+ Q3 2015) reflects that completed list.


Update 2018: as documented in Giorgos Oikonomou's answer, Go 1.7 (Q1 2016) has introduced the
go tool dist list command.
See commit c3ecded: it fixes issue 12270 opened in Q3 2015:

To easier write tooling for cross compiling it would be good to programmatically get the possible combinations of GOOS and GOARCH.

This was implemented in CL 19837

cmd/dist: introduce list subcommand to list all supported platforms

You can list in plain text, or in json:

Copy> go tool dist list -json
[
        {
                "GOOS": "android",
                "GOARCH": "386",
                "CgoSupported": true
        },
        ...
]

As Mark Bates tweeted:

Bonus: Column output properly formatted for display:

Copygo tool dist list | column -c 75 | column -t
2 of 3
49

I think you're looking for this list of possible GOOS and GOARCH combinations, in this section:

http://golang.org/doc/install/source#environment

$GOOS and $GOARCH The name of the target operating system and compilation architecture. These default to the values of $GOHOSTOS and $GOHOSTARCH respectively (described below).

Choices for $GOOS are darwin (Mac OS X 10.8 and above and iOS), dragonfly, freebsd, linux, netbsd, openbsd, plan9, solaris and windows. Choices for $GOARCH are amd64 (64-bit x86, the most mature port), 386 (32-bit x86), arm (32-bit ARM), arm64 (64-bit ARM), ppc64le (PowerPC 64-bit, little-endian), ppc64 (PowerPC 64-bit, big-endian), mips64le (MIPS 64-bit, little-endian), and mips64 (MIPS 64-bit, big-endian). mipsle (MIPS 32-bit, little-endian), and mips (MIPS 32-bit, big-endian).

The valid combinations of $GOOS and $GOARCH are:

Copy$GOOS $GOARCH
android   arm
darwin    386
darwin    amd64
darwin    arm
darwin    arm64
dragonfly amd64
freebsd   386
freebsd   amd64
freebsd   arm
linux     386
linux     amd64
linux     arm
linux     arm64
linux     ppc64
linux     ppc64le
linux     mips
linux     mipsle
linux     mips64
linux     mips64le
netbsd    386
netbsd    amd64
netbsd    arm
openbsd   386
openbsd   amd64
openbsd   arm
plan9     386
plan9     amd64
solaris   amd64
windows   386
windows   amd64
🌐
OneUptime
oneuptime.com › home › blog › how to cross-compile go applications on ubuntu
How to Cross-Compile Go Applications on Ubuntu
March 2, 2026 - GOOS - Target operating system (linux, windows, darwin, freebsd, etc.) GOARCH - Target architecture (amd64, arm64, arm, 386, etc.)
🌐
Openyurt
openyurt.io › how to build and test
How to Build and Test | OpenYurt
make docker-build TARGET_PLATFORMS=linux/amd64 REGION=cn · After the command is executed, local images of each component of OpenYurt are generated, which can be viewed using docker images. GOOS=${target_os} GOARCH=${target_arch} CGO_ENABLED=0 make build WHAT=yurtadm ·