Installing Go from Source
Build the Go toolchain from source code step by step covering prerequisites bootstrap toolchain compilation and verification
Building Go from source means compiling the Go compiler and its entire toolchain directly from the source code instead of downloading a prebuilt binary distribution. This is not what most developers need—the official binary installers are faster and simpler—but there are specific cases where compiling from source is the right choice: you plan to contribute to the Go project itself, you need a custom build with local patches, you are bringing Go to a platform that has no official binary release, or you are a package maintainer producing distribution-specific packages.
The process hinges on one non‑obvious detail: the Go compiler is written in Go. To build a version of Go, you must already have a working Go installation. The existing Go toolchain that does the initial build is called the bootstrap toolchain. Understanding the bootstrap requirement and setting it up correctly is the part where most problems occur.
Prefer a binary install?:
If you just want to start writing Go programs, refer to the official download page at go.dev/dl or use the platform‑specific instructions in the previous section of this chapter. Building from source is only recommended when you genuinely need to.
When Building from Source Makes Sense
You should consider a source build when you are:
- Contributing to Go itself. Fixing a compiler bug or working on a standard library improvement requires a development build from the
masterbranch. - Targeting an unsupported platform. If your operating system or CPU architecture combination is not on the official binary download list, a source build is the only way to get a working Go toolchain.
- Running Go inside a special environment. Minimal container images, embedded systems, or environments with unusual C libraries sometimes require building with specific flags or patches.
- Auditing or modifying the toolchain. Security researchers or infrastructure teams may need to compile from audited source with deterministic flags.
For all other development work, the precompiled binaries are recommended. They are tested, signed, and maintained by the Go team.
Prerequisites
Before you run the build, your system must have the following items ready. If any of them are missing, the build will fail with errors that often do not clearly explain what went wrong.
System Requirements
The Go compilers support a long list of operating systems and instruction sets. The exact combinations are documented in the Go Wiki. At minimum, you need a supported OS/arch pair, a few hundred megabytes of free disk space, and a reasonable amount of memory (any modern machine will suffice).
A Bootstrap Go Installation
Because the Go toolchain is written in Go, you need a working go command before you can build a new version from source. The minimum bootstrap version depends on the version you are trying to build:
- Go 1.22 and 1.23 require Go 1.20 or later as the bootstrap compiler.
- Go 1.24 and 1.25 require Go 1.22 or later.
- For earlier versions, the general rule is that Go 1.N requires Go 1.M, where M is N-2 rounded down to an even number.
Version mismatch will stop the build:
If the bootstrap go command in your PATH is older than the required minimum, the build scripts will fail with an error about the bootstrap toolchain. The error message is usually clear, but the fix is to install a newer Go binary first, even if you plan to replace it with the source‑built version later.
There are four ways to obtain a bootstrap toolchain. The first two cover nearly every real‑world scenario.
- Download a recent binary release – The easiest method. Go to go.dev/dl, download the archive for your platform, and extract it to a temporary location. Then point
GOROOT_BOOTSTRAPto that directory. - Cross‑compile a bootstrap toolchain – If you are building on a platform for which no binary release exists, you can run the
bootstrap.bashscript on a different machine that already has Go installed. That script produces a self‑contained bootstrap tree you can transfer to the target machine. - Use gccgo – The GCC‑based Go frontend (gccgo 5 or later) can act as a bootstrap compiler. This is uncommon and mainly used by distribution maintainers.
- Compile from Go 1.4 C sources – Go 1.4 was the last release whose compiler was written in C. You can build a Go 1.4 binary from its C source (provided you have a C compiler) and then use that binary as the bootstrap for newer versions. This method is almost never required today unless you are bootstrapping on a truly exotic architecture with no other options.
For the rest of this guide, we assume you are using a binary release as the bootstrap. If you already have a recent Go installed and on your PATH, that also works—make.bash will look for a go command in PATH first.
Do not reuse the same Git clone for the bootstrap toolchain and the main build:
If you build a Go 1.4 bootstrap compiler from source and then attempt to use the same directory as the source tree for a newer Go version, the build will fail or produce a broken toolchain. The bootstrap toolchain must live in its own directory and remain at the correct version. Even if you obtained the bootstrap from a binary, use a separate directory for the target source tree.
Git
Cloning the Go repository requires Git. Nearly every system already has it, but you can check with:
git version
If Git is missing, install it from your package manager or from git-scm.com. The Go build process does not use Git beyond the initial clone and checkout, so any reasonably recent version is fine.
A C Compiler (Optional)
Go can call C code through a mechanism called cgo. If you want the source‑built Go to support cgo, you must have a C compiler (typically gcc or clang) installed before running the build script. On Linux, install gcc with your package manager. On macOS, clang is included with Xcode Command Line Tools. On Windows, you will need a compiler like TDM‑GCC or MSYS2 MinGW.
If you do not need cgo—for example, you are building a pure Go development environment—you can tell the build to skip C support by setting the environment variable CGO_ENABLED=0 before running make.bash. The resulting Go toolchain will work perfectly for all pure Go programs but will not be able to compile packages that use import "C".
Building without a C compiler when cgo is expected:
If you run make.bash with CGO_ENABLED left at its default (which is 1 on most systems) and no C compiler is installed, the build will fail partway through with linker or compiler errors. Either install a C compiler first or set CGO_ENABLED=0 explicitly.
Step‑by‑Step Source Build
Each step depends on the previous one. Do not skip ahead, and verify that each step completes without errors before moving to the next.
Step 1: Set up a bootstrap Go installation
The build scripts need a working go command. If you already have a recent Go version in your PATH, you can skip to Step 2—the scripts will find it automatically. To be explicit, or to keep your existing installation untouched, set the GOROOT_BOOTSTRAP environment variable instead.
Download the binary release for your platform from go.dev/dl, extract it to $HOME/go-bootstrap (or any location), and point GOROOT_BOOTSTRAP at that directory:
# Download Go 1.22.5 for linux/amd64 (adjust URL for your OS/arch)
wget https://go.dev/dl/go1.22.5.linux-amd64.tar.gz
tar -C $HOME -xzf go1.22.5.linux-amd64.tar.gz
mv $HOME/go $HOME/go-bootstrap
export GOROOT_BOOTSTRAP=$HOME/go-bootstrap
On macOS with an Apple Silicon Mac, use go1.22.5.darwin-arm64.tar.gz. On Windows, you can extract the zip archive to a directory of your choice and set the environment variable in the command prompt.
Test the bootstrap:
Confirm the bootstrap compiler works by running $GOROOT_BOOTSTRAP/bin/go version. You should see the version string of the downloaded Go.
Step 2: Install Git and, if needed, a C compiler
If you do not have Git, install it now. On Debian/Ubuntu:
sudo apt-get install git
If you plan to build with cgo support, install gcc (Linux) or the Xcode Command Line Tools (macOS). On Windows, set up a C compiler that works with Go; MinGW‑w64 from MSYS2 is a common choice.
If you do not need cgo, remember to set CGO_ENABLED=0 in the next steps. You can put this in your environment right now:
export CGO_ENABLED=0
Step 3: Clone the Go source repository
Choose a directory where the Go source tree will live. Do not clone into an existing GOPATH directory, and avoid $HOME/go to prevent conflicts with the default GOPATH location. In this example we use $HOME/goroot.
git clone https://go.googlesource.com/go $HOME/goroot
cd $HOME/goroot
Check out the specific release you want to build. For a stable release, use the corresponding tag:
git checkout go1.22.5
If you intend to contribute to Go, switch to the master branch instead:
git checkout master
Do not point GOROOT_BOOTSTRAP at this clone:
The directory you just cloned is the target source tree that will be built. It must not be used as the bootstrap toolchain. Keep it separate from the bootstrap installation you prepared in Step 1.
Step 4: Build Go
Navigate to the src directory inside the cloned repository and run the build script. The process compiles the compiler, linker, assembler, and all standard tools.
cd $HOME/goroot/src
./make.bash
The build will take a minute or two, depending on your machine. If you set CGO_ENABLED=0, it will be faster but produce a toolchain without cgo.
When the build finishes successfully, you will see output similar to:
---
Installed Go for linux/amd64 in /home/you/goroot
Installed commands in /home/you/goroot/bin
*** You need to add /home/you/goroot/bin to your PATH.
Build completed:
The lines beginning with --- and Installed confirm that the build succeeded. The exact paths will match the directory you chose for the clone.
Errors during make.bash:
If the build fails, check these common problems:
- The bootstrap Go version is too old (upgrade it).
- The
GOROOT_BOOTSTRAPdirectory does not contain a workinggobinary. CGO_ENABLEDis 1 but no C compiler is installed (set it to 0 or install a C compiler).- On macOS, missing command line tools (run
xcode-select --install).
Step 5: Test the installation
Before adjusting your environment, verify that the freshly built go command works correctly. Write a tiny program and run it:
$HOME/goroot/bin/go run - <<EOF
package main
import "fmt"
func main() {
fmt.Println("hello from source-built Go")
}
EOF
You should see hello from source-built Go. This confirms that the compiler, linker, and runtime are all functioning.
You can also run the full test suite (this takes longer but is recommended for production builds):
cd $HOME/goroot/src
./all.bash
A successful test run ends with ALL TESTS PASSED.
Step 6: Configure your environment
For day‑to‑day use, you need the source‑built go command to be on your PATH. The installation directory ($HOME/goroot in our example) contains a bin folder with all the tools.
Add the following line to your ~/.profile or ~/.bashrc:
export PATH=$HOME/goroot/bin:$PATH
Then reload the shell or run source ~/.profile.
Modern Go does not require the GOROOT environment variable—the tools automatically locate the root of the Go tree relative to the go binary. However, if you want to be explicit, you can set GOROOT=$HOME/goroot, but it is optional.
Working with modules:
Since Go 1.16, the default mode for projects is module‑aware. You no longer need to place your code inside a rigid GOPATH workspace. The source‑built go command supports modules just like a binary install.
Common Pitfalls with Source Builds
Even with the correct steps, a few issues tend to trip up newcomers. Being aware of them before you start will save time.
- Reusing the same directory for bootstrap and the main build. The bootstrap Go must stay at the version you downloaded. If you later
git checkouta different tag in that directory, the bootstrap breaks. Always keep them in separate directories. - Forgetting to set
CGO_ENABLED=0when no C compiler exists. This is the single most frequent build failure. The error messages mention missing headers or a broken toolchain, but the root cause is simply the absence ofgcc. Set the variable and runmake.bashagain. - Cloning into the default
$HOME/godirectory. Go uses$HOME/goas the defaultGOPATH. If you place the source tree there, the tools may pick up the wrong packages or configuration. Choose a different name, likegorootorgo-src. - Not adding the new
bindirectory to the PATH, then running an oldergo. After a successful build, if you typego versionand still see the system Go, you are not using the source‑built binary. Check yourPATHandwhich go.
Bootstrap version must match the target Go's requirement:
If you are building a very recent development branch, the required bootstrap version might be higher than the last stable release. The Go contribution guide always lists the current minimum bootstrap version.
Summary
Installing Go from source is a precise but not complex process once you understand the bootstrap requirement: you need an existing Go to build a new Go. The workflow is to obtain a bootstrap toolchain (usually a binary download), clone the source repository, run make.bash (or make.bat on Windows), and then add the resulting bin directory to your PATH.
The single most important insight to carry forward is the separation between the bootstrap toolchain and the target source tree—they must live in different directories, and you must not confuse them. The second is the role of CGO_ENABLED: unless you know you need C interop, building with CGO_ENABLED=0 removes the C compiler dependency and simplifies the build significantly.