Install Go on Linux, macOS and Windows
Go is installed as one directory and one command. There is no virtual machine to install beside it, no package to register and no environment to activate: the go binary in the directory's bin folder compiles, links, tests and formats, and the same directory holds the compiler and the standard library sources it reads.
This lesson installs it on Linux, macOS and Windows, checks the result, and names the three paths the toolchain uses. The install guides for single systems are in this course: Ubuntu, Rocky Linux, macOS, Windows 11, FreeBSD and Docker.
Install it on your system
A package manager puts Go somewhere the system controls and updates with the rest of the system. That is the fastest way on a server, and the version is the one the distribution ships: this container is Alpine Linux 3.24 and its package carries Go 1.26.10.
sudo apk add go
sudo apt install golang-go
sudo dnf install golang
brew install go
The official archive is the other way, and the one to use when a project needs a version newer than the distribution's. Unpack it under /usr/local and put its bin folder first in PATH. On Windows the MSI installer does both steps. The detailed commands per system are on the five install pages linked above.
Go 1.21 and later do not need GOROOT or GOPATH in the environment: the command finds its own directory, and the home directory holds the module cache. Setting them by hand is now the exception, for a second toolchain or a build server with a fixed disk.
Check the install
Three commands answer three different questions. go version prints the toolchain and the platform it was built for. go env prints the settings the build uses. go run proves that the compiler, the linker and the runtime of the installed copy work together.
go version
go env GOROOT GOPATH GOMODCACHE
go run hello.go
apk list -I go
go version go1.26.10 linux/amd64
/usr/lib/go
/home/agent/go
/home/agent/go/pkg/mod
hello from the toolchain you just installed
go-1.26.10-r0 x86_64 {go} (BSD-3-Clause) [installed]
Read the first line as go1.26.10 for linux/amd64. The go env output names the three paths a beginner looks for: GOROOT, the toolchain with its standard library sources; GOPATH, the home of the module cache and installed binaries; GOMODCACHE, the downloaded modules themselves. The last line is the package that installed this copy, so the page and the machine agree on the version.
Which Go am I running?
A machine can hold two toolchains: the distribution's and an unpacked archive. The program below prints what it was compiled by and which source tree the compiler used, and it is the program to keep when a build behaves differently on two machines.
package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Println("version:", runtime.Version())
fmt.Println("compiler:", runtime.Compiler)
fmt.Println("source tree:", runtime.GOROOT())
}
go run which.go
version: go1.26.10
compiler: gc
source tree: /usr/lib/go
runtime.Version is the version of the toolchain that produced the binary, not the version installed now: a binary built last year keeps its own answer. That is why a service prints this line at startup, and why the note at the bottom of every page of this course names the exact version the examples were run with.
The first build outside a module
A single file can be built without a module, which is the quickest way to try an idea. Inside a project the same file is part of a module, and the lesson on modules sets one up. The failure below is the one to recognise: the compiler refuses a package it cannot find, and the message names the import path it tried.
package main
import (
"fmt"
"example.com/greet"
)
func main() {
fmt.Println(greet.Hello("Go"))
}
go build nopkg.go
nopkg.go:6:2: no required module provides package example.com/greet: go.mod file not found in current directory or any parent directory; see 'go help modules'
Exercises
- Install Go with the package manager of your system, then again from the official archive in a different directory. Which one does go version find, and why?
- Run go env without arguments and find GOOS, GOARCH and GOMODCACHE. Which of them would you change to build for another system?
- Remove the Go directory from PATH and run go version. What does the shell say, and what does that tell you about the compiler being one program?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
go versionprint?Answer
The toolchain version and the platform it was built for
The line is go version goX.Y.Z os/arch; the versions of modules come from go list -m.
-
What is GOROOT?
Answer
The folder that holds the toolchain and the standard library sources
GOROOT is the Go installation itself; GOPATH and GOMODCACHE are the ones that hold your files.
-
What does GOMODCACHE hold?
Answer
The downloaded modules
Modules downloaded from a proxy are unpacked in GOMODCACHE and shared by every project of the user.
-
Which command proves that the compiler, the linker and the runtime work together?
Answer
go run hello.go
go run compiles and links the program and then executes it, so it exercises the whole chain.
-
What does this program print?
package main import ( "fmt" "runtime" ) func main() { fmt.Println(runtime.GOOS + "/" + runtime.GOARCH) }Answer
linux/amd64
The constants are fixed at build time and this program was built on linux/amd64.
-
What does this program print?
package main import ( "fmt" "runtime" ) func main() { fmt.Println(runtime.Version()) }Answer
go1.26.10
runtime.Version returns the toolchain version with the go prefix.
-
Why does
go vetanswer with an error for a program that runs correctly?Answer
vet reports code that compiles but looks wrong, such as a format verb of the wrong type
vet runs analyzers for likely mistakes; the program still builds and runs, and vet exits non-zero.
-
What happens when a user installs Go with the distribution's package manager?
Answer
The version is the one the distribution ships
A distribution freezes package versions for the life of the release, so the archive is the way to a newer Go.
-
Which environment variable did Go stop needing in recent versions?
Answer
GOROOT and GOPATH do not have to be set
The go command finds its own directory and uses the user's home for the module cache.
-
What does
go install example.com/tool@latestdo?Answer
It downloads the module and writes its binary to GOPATH/bin
go install with a version builds a program from a module and puts the binary in the bin folder of GOPATH.
The code on this page was run with go 1.26.10 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev