Go "cannot find GOROOT directory" in CI - Fix the build
By Kaveh Alemi·Latchkey
GOROOT is where the Go toolchain lives. If it points at a directory that does not exist, the go command cannot find its own standard library and refuses to run.
What this error means
A go command fails immediately with go: cannot find GOROOT directory: /usr/local/go. It usually means a hand-set GOROOT env var is stale, or a cached path no longer matches the installed toolchain.
go
go: cannot find GOROOT directory: /usr/local/go
Diagnose it: toolchain, tags, or platform?
Go build failures that only appear in CI usually come from a different toolchain version, different build tags, or cross-compilation defaults that differ from your machine.
Terminal
go version
go env GOOS GOARCH CGO_ENABLED GOFLAGS GOTOOLCHAIN
# build exactly what CI builds, verbosely
go build -v ./... 2>&1 | tail -40
# CGO is the usual difference: on by default locally, often off in a slim CI image
CGO_ENABLED=0 go build ./...
Common causes
Stale GOROOT environment variable
A pinned GOROOT points at an install path that the current runner image does not have.
Cached path from a different setup
A cache or earlier step set GOROOT to a location the actual Go install no longer uses.
How to fix it
Stop hard-coding GOROOT
Remove any manual GOROOT export and let setup-go configure it.
.github/workflows/ci.yml
- uses:actions/setup-go@v5with:go-version:'1.22'# do not set GOROOT manually
Point GOROOT at the real install
If you must set it, derive it from the installed go binary.
Terminal
export GOROOT="$(go env GOROOT)"
How to prevent it
Let setup-go own GOROOT instead of hard-coding it.
Avoid caching absolute toolchain paths across images.
Use go env GOROOT if a value is genuinely needed.
Frequently asked questions
What causes Go "cannot find GOROOT directory" in CI?
There are 2 common causes: stale goroot environment variable and cached path from a different setup. A pinned GOROOT points at an install path that the current runner image does not have.
How do I fix Go "cannot find GOROOT directory" in CI?
There are 2 fixes depending on which cause you have: stop hard-coding goroot and point goroot at the real install. Work through them in order, since the first is the most common.
What does Go "cannot find GOROOT directory" in CI actually mean?
A go command fails immediately with go: cannot find GOROOT directory: /usr/local/go.
How do I stop Go "cannot find GOROOT directory" in CI happening again?
Let setup-go own GOROOT instead of hard-coding it. The prevention section lists 3 changes that keep it from recurring.