Installing Go from source

This topic describes how to build and run Go from source code. To install with an installer, see Download and install.

Introduction

Go is an open source project, distributed under a BSD-style license. This document explains how to check out the sources, build them on your own machine, and run them.

Most users don't need to do this, and will instead install from precompiled binary packages as described in Download and install, a much simpler process. If you want to help develop what goes into those precompiled packages, though, read on.

There are two official Go compiler toolchains. This document focuses on the gc Go compiler and tools. For information on how to work on gccgo, a more traditional compiler using the GCC back end, see Setting up and using gccgo.

The Go compilers support the following instruction sets:

amd64, 386
The x86 instruction set, 64- and 32-bit.
arm64, arm
The ARM instruction set, 64-bit (AArch64) and 32-bit.
mips64, mips64le, mips, mipsle
The MIPS instruction set, big- and little-endian, 64- and 32-bit.
ppc64, ppc64le
The 64-bit PowerPC instruction set, big- and little-endian.
riscv64
The 64-bit RISC-V instruction set.
s390x
The IBM z/Architecture.
wasm
WebAssembly.

The compilers can target the AIX, Android, DragonFly BSD, FreeBSD, Illumos, Linux, macOS/iOS (Darwin), NetBSD, OpenBSD, Plan 9, Solaris, and Windows operating systems (although not all operating systems support all architectures).

A list of ports which are considered "first class" is available at the first class ports wiki page.

The full set of supported combinations is listed in the discussion of environment variables below.

See the main installation page for the overall system requirements. The following additional constraints apply to systems that can be built only from source:

  • For Linux on PowerPC 64-bit, the minimum supported kernel version is 2.6.37, meaning that Go does not support CentOS 6 on these systems.

Install Go compiler binaries for bootstrap

The Go toolchain is written in Go. To build it, you need a Go compiler installed. The scripts that do the initial build of the tools look for a "go" command in $PATH, so as long as you have Go installed in your system and configured in your $PATH, you are ready to build Go from source. Or if you prefer you can set $GOROOT_BOOTSTRAP to the root of a Go installation to use to build the new Go toolchain; $GOROOT_BOOTSTRAP/bin/go should be the go command to use.

There are four possible ways to obtain a bootstrap toolchain:

These approaches are detailed below.

Bootstrap toolchain from binary release

To use a binary release as a bootstrap toolchain, see the downloads page or use any other packaged Go distribution.

Bootstrap toolchain from cross-compiled source

To cross-compile a bootstrap toolchain from source, which is necessary on systems Go 1.4 did not target (for example, linux/ppc64le), install Go on a different system and run bootstrap.bash.

When run as (for example)

$ GOOS=linux GOARCH=ppc64 ./bootstrap.bash

bootstrap.bash cross-compiles a toolchain for that GOOS/GOARCH combination, leaving the resulting tree in ../../go-${GOOS}-${GOARCH}-bootstrap. That tree can be copied to a machine of the given target type and used as GOROOT_BOOTSTRAP to bootstrap a local build.

Bootstrap toolchain using gccgo

To use gccgo as the bootstrap toolchain, you need to arrange for $GOROOT_BOOTSTRAP/bin/go to be the go tool that comes as part of gccgo 5. For example on Ubuntu Vivid:

$ sudo apt-get install gccgo-5
$ sudo update-alternatives --set go /usr/bin/go-5
$ GOROOT_BOOTSTRAP=/usr ./make.bash

Bootstrap toolchain from C source code

To build a bootstrap toolchain from C source code, use either the git branch release-branch.go1.4 or go1.4-bootstrap-20171003.tar.gz, which contains the Go 1.4 source code plus accumulated fixes to keep the tools running on newer operating systems. (Go 1.4 was the last distribution in which the toolchain was written in C.) After unpacking the Go 1.4 source, cd to the src subdirectory, set CGO_ENABLED=0 in the environment, and run make.bash (or, on Windows, make.bat).

Once the Go 1.4 source has been unpacked into your GOROOT_BOOTSTRAP directory, you must keep this git clone instance checked out to branch release-branch.go1.4. Specifically, do not attempt to reuse this git clone in the later step named "Fetch the repository." The go1.4 bootstrap toolchain must be able to properly traverse the go1.4 sources that it assumes are present under this repository root.

Note that Go 1.4 does not run on all systems that later versions of Go do. In particular, Go 1.4 does not support current versions of macOS. On such systems, the bootstrap toolchain must be obtained using one of the other methods.

Install Git, if needed

To perform the next step you must have Git installed. (Check that you have a git command before proceeding.)

If you do not have a working Git installation, follow the instructions on the Git downloads page.

(Optional) Install a C compiler

To build a Go installation with cgo support, which permits Go programs to import C libraries, a C compiler such as gcc or clang must be installed first. Do this using whatever installation method is standard on the system.

To build without cgo, set the environment variable CGO_ENABLED=0 before running all.bash or make.bash.

Fetch the repository

Change to the directory where you intend to install Go, and make sure the goroot directory does not exist. Then clone the repository and check out the latest release tag (go1.12, for example):

$ git clone https://go.googlesource.com/go goroot
$ cd goroot
$ git checkout <tag>

Where <tag> is the version string of the release.

Go will be installed in the directory where it is checked out. For example, if Go is checked out in $HOME/goroot, executables will be installed in $HOME/goroot/bin. The directory may have any name, but note that if Go is checked out in $HOME/go, it will conflict with the default location of $GOPATH. See GOPATH below.

Reminder: If you opted to also compile the bootstrap binaries from source (in an earlier section), you still need to git clone again at this point (to checkout the latest <tag>), because you must keep your go1.4 repository distinct.

If you intend to modify the go source code, and contribute your changes to the project, then move your repository off the release tag, and onto the master (development) branch. Otherwise, skip this step.

$ git checkout master

Install Go

To build the Go distribution, run

$ cd src
$ ./all.bash

(To build under Windows use all.bat.)

If all goes well, it will finish by printing output like:

ALL TESTS PASSED

---
Installed Go for linux/amd64 in /home/you/go.
Installed commands in /home/you/go/bin.
*** You need to add /home/you/go/bin to your $PATH. ***

where the details on the last few lines reflect the operating system, architecture, and root directory used during the install.

For more information about ways to control the build, see the discussion of environment variables below. all.bash (or all.bat) runs important tests for Go, which can take more time than simply building Go. If you do not want to run the test suite use make.bash (or make.bat) instead.

Testing your installation

Check that Go is installed correctly by building a simple program.

Create a file named hello.go and put the following program in it:

package main

import "fmt"

func main() {
	fmt.Printf("hello, world\n")
}

Then run it with the go tool:

$ go run hello.go
hello, world

If you see the "hello, world" message then Go is installed correctly.

Set up your work environment

You're almost done. You just need to do a little more setup.

How to Write Go Code Learn how to set up and use the Go tools

The How to Write Go Code document provides essential setup instructions for using the Go tools.

Install additional tools

The source code for several Go tools (including gopls) is kept in the golang.org/x/tools repository. To install one of the tools (gopls in this case):

$ go install golang.org/x/tools/gopls@latest

Community resources

The usual community resources listed on the help page have active developers that can help you with problems with your installation or your development work. For those who wish to keep up to date, there is another mailing list, golang-checkins, that receives a message summarizing each checkin to the Go repository.

Bugs can be reported using the Go issue tracker.

Keeping up with releases

New releases are announced on the golang-announce mailing list. Each announcement mentions the latest release tag, for instance, go1.9.

To update an existing tree to the latest release, you can run:

$ cd go/src
$ git fetch
$ git checkout <tag>
$ ./all.bash

Where <tag> is the version string of the release.

Optional environment variables

The Go compilation environment can be customized by environment variables. None is required by the build, but you may wish to set some to override the defaults.

Note that $GOARCH and $GOOS identify the target environment, not the environment you are running on. In effect, you are always cross-compiling. By architecture, we mean the kind of binaries that the target environment can run: an x86-64 system running a 32-bit-only operating system must set GOARCH to 386, not amd64.

If you choose to override the defaults, set these variables in your shell profile ($HOME/.bashrc, $HOME/.profile, or equivalent). The settings might look something like this:

export GOARCH=amd64
export GOOS=linux

although, to reiterate, none of these variables needs to be set to build, install, and develop the Go tree.