Skip to content

Cross-compiling Go binaries for Linux, macOS, ARM64

Use GOOS, GOARCH, and CGO_ENABLED to produce reproducible Go binaries for every target you ship to — from amd64 Linux to ARM64 Graviton.

Cross-compilation is the cheapest superpower in Go. The toolchain ships every supported target out of the box — building a Linux ARM64 binary on an Intel Mac is one environment variable away. The hard part is keeping the builds reproducible when you ship to multiple architectures simultaneously. This article covers the build matrix, the gotchas around CGO and net, and how goreleaser automates the whole thing.

How to verify

Confirm the toolchain supports the targets you need:

go version
go tool dist list | head -20
go env GOOS GOARCH CGO_ENABLED
file ./bin/myapp 2>/dev/null
goreleaser --version 2>/dev/null || echo "goreleaser not installed"

go tool dist list enumerates every GOOS/GOARCH combo the local toolchain can target. If a target you expect (linux/arm64, darwin/arm64) is missing, the Go install is incomplete — reinstall from the official tarball.

What’s happening

A Go build is compile + link, and both stages are aware of GOOS (target operating system) and GOARCH (target architecture). With pure Go code and CGO_ENABLED=0, every target is just an environment variable change away — no cross-compiler, no toolchain shenanigans, no QEMU.

CGO is where it stops being free. import "C" in any package — yours or transitively — means the build needs a C compiler that targets the destination. Building Linux ARM64 binaries from Linux amd64 then requires gcc-aarch64-linux-gnu (or use zig cc as the cross-linker, which is cleaner). Most stdlib packages are pure Go, but os/user and net historically used CGO — modern Go gives you -tags=osusergo,netgo flags to force the pure-Go versions and re-enable cross-compilation.

The other half is reproducibility. Two builds of the same commit should produce bit-identical binaries. -trimpath removes local paths. Embedded VCS info (Go 1.18+) bakes the commit hash and modification flag into the binary, which is good for observability but means a dirty working tree produces a different binary than a clean one — keep CI on a clean checkout.

The procedure

  1. Build a single non-native target. Linux ARM64 binary from any amd64 host:

    CGO_ENABLED=0 GOOS=linux GOARCH=arm64 \
      go build -trimpath -ldflags="-s -w" \
      -o bin/myapp-linux-arm64 ./cmd/server
    file bin/myapp-linux-arm64
    # ELF 64-bit LSB executable, ARM aarch64, ... statically linked
  2. Scripted build matrix for the common Linux targets:

    cat > build.sh <<'EOF'
    #!/usr/bin/env bash
    set -euo pipefail
    VERSION=$(git describe --tags --always --dirty)
    mkdir -p dist
    for combo in linux/amd64 linux/arm64 darwin/amd64 darwin/arm64; do
      GOOS=${combo%/*} GOARCH=${combo#*/}
      out="dist/myapp-${VERSION}-${GOOS}-${GOARCH}"
      [[ $GOOS == windows ]] && out="${out}.exe"
      echo "Building $out"
      CGO_ENABLED=0 GOOS=$GOOS GOARCH=$GOARCH \
        go build -trimpath \
        -ldflags="-s -w -X main.version=${VERSION}" \
        -o "$out" ./cmd/server
    done
    EOF
    chmod +x build.sh
    ./build.sh
  3. For CGO-heavy projects (image processing, ML, anything that links against system libs), use zig cc as the cross-compiler:

    sudo snap install zig --classic --beta  # or download from ziglang.org
    CGO_ENABLED=1 GOOS=linux GOARCH=arm64 \
      CC="zig cc -target aarch64-linux-gnu" \
      CXX="zig c++ -target aarch64-linux-gnu" \
      go build -o bin/myapp-linux-arm64 ./cmd/server

    Zig acts as a self-contained cross-compiler — no separate gcc-aarch64 package needed.

  4. Adopt goreleaser for the release pipeline. .goreleaser.yaml:

    project_name: myapp
    builds:
      - id: myapp
        main: ./cmd/server
        env:
          - CGO_ENABLED=0
        flags:
          - -trimpath
        ldflags:
          - -s -w -X main.version={{ .Version }} -X main.commit={{ .Commit }}
        goos: [linux, darwin]
        goarch: [amd64, arm64]
    archives:
      - format: tar.gz
        name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}'
    checksum:
      name_template: 'checksums.txt'

    Then goreleaser release --clean builds, archives, checksums, and (with credentials) publishes to GitHub Releases.

  5. For verification on the destination, re-check file output and a sanity-version flag:

    scp bin/myapp-linux-arm64 graviton-host:/usr/local/bin/myapp
    ssh graviton-host '/usr/local/bin/myapp --version'

Operational notes

  • GOARM matters for 32-bit ARM (Raspberry Pi 3 and older). GOARM=7 (hardware float) is the modern default; GOARM=5 for older boards without FPU. Set explicitly when targeting ARMv6/v7 to avoid the wrong default.
  • A static binary on macOS (darwin/arm64) still uses the macOS libSystem dynamically — Go does not produce truly static darwin binaries. That is fine for distribution but be aware.
  • Embedded go.sum and module hashes mean go build against unverified caches can fail in air-gapped CI. GOFLAGS=-mod=mod and a vendored vendor/ directory side-step the network entirely.
  • For multi-arch Docker images, the canonical pattern is docker buildx build --platform linux/amd64,linux/arm64 with a multi-stage Dockerfile that builds inside the target arch’s base image. Cross-compile in the build stage to dodge QEMU emulation overhead.
  • Reproducibility check: build twice from a clean tree, sha256sum the binaries — they should match. If they do not, something in the build is non-deterministic (timestamps in embedded resources, random map iteration order in generated code).

For the run-side counterpart, see Installing Go and systemd services. Stack Harbor’s managed operations practice runs Go binaries on a mix of amd64 EC2 and ARM64 Graviton — the same release artifact runs on both, the savings on Graviton are real, and the pipeline does not care.