Difference between revisions of "Golang/Install"
Line 1: | Line 1: | ||
= Ubuntu Install = | = Ubuntu Install = | ||
{{Note|Update to reflect Ubuntu 20.04}} | |||
;Ubuntu 18.04 inc. WSL | ;Ubuntu 18.04 inc. WSL | ||
<source lang="bash"> | <source lang="bash"> | ||
sudo apt install golang-go | # List available versions | ||
apt-cache policy golang-1.1? | |||
sudo apt install golang-go # default version | |||
sudo apt install golang-1.16 # specific | |||
</source> | </source> | ||
# Official way of installing it | |||
Download from https://golang.org/dl/ and extract it into <code>/usr/local</code>, creating a Go tree in <code>/usr/local/go</code> | Download from https://golang.org/dl/ and extract it into <code>/usr/local</code>, creating a Go tree in <code>/usr/local/go</code> | ||
<source lang="bash"> | <source lang="bash"> | ||
sudo tar -C /usr/local -xzvf go$VERSION. | VERSION=1.16.7 | ||
curl -s -O https://storage.googleapis.com/golang/go${VERSION}.linux-amd64.tar.gz | |||
sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzvf go${VERSION}.linux-amd64.tar.gz | |||
</source> | </source> | ||
Line 17: | Line 22: | ||
Add <code>/usr/local/go/bin</code> to the <code>PATH</code> environment variable. You can do this by adding this line to your <tt>/etc/profile</tt> (for a system-wide installation) or <tt>$HOME/.profile</tt>: | Add <code>/usr/local/go/bin</code> to the <code>PATH</code> environment variable. You can do this by adding this line to your <tt>/etc/profile</tt> (for a system-wide installation) or <tt>$HOME/.profile</tt>: | ||
<source lang="bash"> | <source lang="bash"> | ||
# Official | |||
export PATH=$PATH:/usr/local/go/bin | |||
# Optional using alternatives | |||
sudo update-alternatives --install "/usr/bin/go" "go" "/usr/local/go/bin/go" 2 #adds to the $PATH | sudo update-alternatives --install "/usr/bin/go" "go" "/usr/local/go/bin/go" 2 #adds to the $PATH | ||
# Set $GOPATH | # Set $GOPATH |
Revision as of 13:20, 7 August 2021
Ubuntu Install
Note: Update to reflect Ubuntu 20.04
- Ubuntu 18.04 inc. WSL
# List available versions apt-cache policy golang-1.1? sudo apt install golang-go # default version sudo apt install golang-1.16 # specific
- Official way of installing it
Download from https://golang.org/dl/ and extract it into /usr/local
, creating a Go tree in /usr/local/go
VERSION=1.16.7 curl -s -O https://storage.googleapis.com/golang/go${VERSION}.linux-amd64.tar.gz sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzvf go${VERSION}.linux-amd64.tar.gz
Add /usr/local/go/bin
to the PATH
environment variable. You can do this by adding this line to your /etc/profile (for a system-wide installation) or $HOME/.profile:
# Official export PATH=$PATH:/usr/local/go/bin # Optional using alternatives sudo update-alternatives --install "/usr/bin/go" "go" "/usr/local/go/bin/go" 2 #adds to the $PATH # Set $GOPATH export GOPATH=$HOME/go export PATH=$PATH:$GOPATH/bin
Multiple versions
You can have multiple versions fo Golang installed in your system. The way to manage the current version in use is to use update-alternatives
Display best options
sudo update-alternatives --display go go - auto mode link best version is /usr/local/go/bin/go link currently points to /usr/local/go/bin/go link go is /usr/bin/go /usr/local/go/bin/go - priority 2
Before
Selection Path Priority Status ------------------------------------------------------------ 0 /usr/bin/golang-go 10 auto mode 1 /usr/bin/gccgo-go 5 manual mode 2 /usr/bin/golang-go 10 manual mode
Action: Installed manually unzipped GoLang into /usr/local/go
but was not added to alternatives. Use command below to add it on:
sudo update-alternatives --install "/usr/bin/go" "go" "/usr/local/go/bin/go" 2
After
Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/golang-go 10 auto mode 1 /usr/bin/gccgo-go 5 manual mode 2 /usr/bin/golang-go 10 manual mode 3 /usr/local/go/bin/go 2 manual mode
Then run command to change go binary available in your shell
sudo update-alternatives --config go #then select ''3''
When you run
$ go version #the version of ''go'' will be the one from <tt>/usr/local/go</tt> location.
Troubleshooting
What is GOPATH
?
- it's an environment variable with a list paths to look for Go code, on Unix it's colon-separated string
- usually
GOPATH
is$HOME/go
wherebin
andsrc
directories exists. Each time you executego get -u <url>
, the binaries will be placed in$HOME/go/bin
and code in$HOME/go/src
- therefore
$HOME/go/bin
needs to be in the$PATH
- default go directory:
/usr/local/go
is calledGOROOT
- default go bin directory:
/usr/local/go/bin
that needs to be in$PATH
Allow 'sudo go <subcommand>'
sudo visudo # then add to'secure_path' '.../go/bin' directory Defaults secure_path=".../bin:/usr/local/go/bin"
Get go environment variables
$ go env GOARCH="amd64" GOBIN="" GOCACHE="/home/ubuntu/.cache/go-build" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/home/ubuntu/go" GOPROXY="" GORACE="" GOROOT="/usr/local/go" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GCCGO="gccgo" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build629810470=/tmp/go-build -gno-record-gcc-switches"