How to Install Go on Linux and Configure Environment Variables in Minutes
This guide walks you through downloading the Go binary, extracting it to /usr/local, verifying the installation, and setting up essential environment variables like GOROOT and GOPATH so you can run Go commands directly from the terminal.
Install Go
Go installation is very simple compared to other language environments: just download the archive and extract it.
1. Download the archive (official site https://golang.org/dl/ is blocked in China, use the Chinese mirror https://studygolang.com/dl). Example:
wget https://studygolang.com/dl/golang/go1.17.1.linux-amd64.tar.gz
# If wget is not installed:
# yum install wget OR download via browser and upload via FTP2. Extract to the application directory
$ tar -zxvf go1.17.1.linux-amd64.tar.gz -C /usr/local/"-C" specifies the target directory; typically programs are installed under /usr/local/.
3. Verify the installation
$ /usr/local/go/bin/go version
go version go1.17.1 linux/amd64Environment Variable Configuration
After installation you still need to set environment variables, mainly GOROOT and GOPATH, to simplify command usage.
Check current Go environment:
$ /usr/local/go/bin/go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOPATH="/root/go"
GOROOT="/usr/local/go"
... (other variables omitted for brevity)To understand each field, refer to the official documentation; for most projects you only need to know GOROOT and GOPATH.
Set the variables system‑wide so you can run go directly:
echo 'export GOROOT=/usr/local/go' >> /etc/profile
echo 'export GOPATH=$HOME/go' >> /etc/profile
echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin' >> /etc/profile
source /etc/profileNow the go command works without the full path.
Modify Go environment variables using the go env -w flag, e.g.:
go env -w GOPATH=/path/to/your/go
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,directThese settings complete the Go environment configuration.
Article source: SegmentFault (copyright belongs to the original author).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
