在AWS EC2執行個體Linux 2 AMI安裝Go。
本範例在EC2 Linux 2 AMI instance安裝目前最新的Go 1.17版本。
SSH連線到EC2 intance,預設使用者為ec2-user
;登入後的預設目錄為ec2-user
的home目錄/home/ec2-user
。
下載Go for Linux installer
執行wget https://go.dev/dl/go1.17.3.linux-amd64.tar.gz
下載Go 1.17 for Linux installer壓縮檔go1.17.3.linux-amd64.tar.gz
。
[ec2-user@ip-172-31-21-26 ~]$ wget https://go.dev/dl/go1.17.3.linux-amd64.tar.gz
--2021-12-02 10:10:00-- https://go.dev/dl/go1.17.3.linux-amd64.tar.gz
Resolving go.dev (go.dev)... 216.239.36.21, 216.239.38.21, 216.239.32.21, ...
Connecting to go.dev (go.dev)|216.239.36.21|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://dl.google.com/go/go1.17.3.linux-amd64.tar.gz [following]
--2021-12-02 10:10:01-- https://dl.google.com/go/go1.17.3.linux-amd64.tar.gz
Resolving dl.google.com (dl.google.com)... 172.217.31.174, 2404:6800:4004:823::200e
Connecting to dl.google.com (dl.google.com)|172.217.31.174|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 134804820 (129M) [application/x-gzip]
Saving to: 'go1.17.3.linux-amd64.tar.gz'
100%[=========================================>] 134,804,820 106MB/s in 1.2s
2021-12-02 10:10:02 (106 MB/s) - 'go1.17.3.linux-amd64.tar.gz' saved [134804820/134804820]
執行ls
檢視所在目錄中的檔案,可看到剛下載的go1.17.3.linux-amd64.tar.gz
。
[ec2-user@ip-172-31-21-26 ~]$ ls
go1.17.3.linux-amd64.tar.gz
解壓縮Go for Linux installer
執行sudo su
切換為root。
[ec2-user@ip-172-31-21-26 ~]$ sudo su
[root@ip-172-31-21-26 ec2-user]#
以root執行rm -rf /usr/local/go && tar -C /usr/local -xzf go1.17.3.linux-amd64.tar.gz
。注意此命令會先把/usr/local/go
目錄刪除(若存在的話)並把go1.17.3.linux-amd64.tar.gz
解壓縮至/usr/local
目錄。解壓縮出的檔案是一個go
目錄。
[root@ip-172-31-21-26 ec2-user]# rm -rf /usr/local/go && tar -C /usr/local -xzf go1.17.3.linux-amd64.tar.gz
輸入ls /usr/local
查看解壓縮在/usr/local
的go
目錄。
[root@ip-172-31-21-26 ec2-user]# ls /usr/local
bin etc games go include lib lib64 libexec sbin share src
設定PATH
輸入su - ec2-user
切換回ec2-user
。
[root@ip-172-31-21-26 ec2-user]# su - ec2-user
[ec2-user@ip-172-31-21-26 ~]$
使用vim編輯home目錄的.bashrc
檔加入以下內容。
export PATH=$PATH:/usr/local/go/bin
本範例.bashrc
修改後的完整內容如下,最下面一行即為新增的PATH內容。
.bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
# Go
export PATH=$PATH:/usr/local/go/bin
確認安裝
重新登入EC2 instance來刷新PATH
。輸入go version
檢視版本確認安裝成功。
[ec2-user@ip-172-31-21-26 ~]$ go version
go version go1.17.3 linux/amd64
輸入go env
檢視Go環境變數。
[ec2-user@ip-172-31-21-26 ~]$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/ec2-user/.cache/go-build"
GOENV="/home/ec2-user/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/ec2-user/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/ec2-user/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.3"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
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-build2188668062=/tmp/go-build -gno-record-gcc-switches"
到此便成功在EC2 Linux 2 instance安裝GO程式了。
沒有留言:
張貼留言