• 欢迎来到小爱博客,一个分享互联网IT技术的网站,喜欢就收藏吧!

非源码安装go(Golang) on Ubuntu 20.04/18.04/ CentOS7

go 小爱 1个月前 (09-20) 35次浏览 已收录 0个评论 扫描二维码

Linux Mint 19: How to Install Go (Golang) on Linux Mint 19

For RHEL 8 / CentOS 8: How to Install Go on RHEL 8

Installing Go (Golang) on CentOS 7

We have two options to install latest Go on CentOS 7

Method 1: Using Go repository ( For CentOS 7 only)

We will use a Go repository to ensure we are getting the latest release without having to build it.

sudo rpm --import https://mirror.go-repo.io/centos/RPM-GPG-KEY-GO-REPO
curl -s https://mirror.go-repo.io/centos/go-repo.repo | sudo tee /etc/yum.repos.d/go-repo.repo

Once the repository has been added, install golang package

sudo yum install golang

Go binary will be placed under /bin/go

$ which go
/bin/go

Install Go on Ubuntu 18.04

For Ubuntu 18.04 server, add Golang backports PPA repository and install Go on Ubuntu 18.04.

sudo add-apt-repository ppa:longsleep/golang-backports 
sudo apt-get update
sudo apt-get install golang-go

Set $GOPATH

mkdir -p ~/go/{bin,pkg,src}
echo 'export GOPATH="$HOME/go"' >> ~/.bashrc
echo 'export PATH="$PATH:${GOPATH//://bin:}/bin"' >> ~/.bashrc

Method 2: Install Go using Golang installer

The second method is the installation of Go from provided Golang installer.

First install wget

sudo apt-get -y install wget
sudo yum -y install wget

Download installer with wget to your local system.

wget https://storage.googleapis.com/golang/getgo/installer_linux

After downloading the file, make it executable:

chmod +x ./installer_linux

And lastly run the installer from your current terminal shell.

$ ./installer_linux 
Welcome to the Go installer!
Downloading Go version go1.11.5 to /home/jmutai/.go
This may take a bit of time…
Downloaded!
Setting up GOPATH
GOPATH has been set up!

One more thing! Run source /home/jmutai/.bash_profile to persist the new environment variables to your current session, or open a
new shell prompt.

Source the ~/.bash_profile to start using Go environment variables in your current session.

$ source ~/.bash_profile
$ go version
go version go1.11.5 linux/amd64

Method 3: Manually Download Go

The second method involves downloading Go archive and installing it to the system. You may need to check Go releases page for the latest version.

Ensure you have wget installed

sudo yum install wget
sudo apt install wget

Then download Go

VER=1.14
wget https://dl.google.com/go/go${VER}.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go${VER}.linux-amd64.tar.gz
rm go${VER}.linux-amd64.tar.gz

Set Go PATH Variable

Run the commands below to set your set $GOPATH

mkdir -p ~/go/{bin,pkg,src}
echo 'export GOPATH="$HOME/go"' >> ~/.bashrc
echo 'export PATH="$PATH:${GOPATH//://bin:}/bin"' >> ~/.bashrc

Testing Go installation on Ubuntu 18.04 / CentOS 7

mkdir -p ~/go/src/test
vim ~/go/src/test/test.go

Add print Hello Gophers message

package main
import "fmt"
func main() {
    fmt.Printf("Hello, Gophers\n")
}

Build test.go file

cd ~/go/src/test
go build

Run binary to test

$ ./test
Hello, Gophers

小爱博客 , 版权所有
转载请注明原文链接:非源码安装go(Golang) on Ubuntu 20.04/18.04/ CentOS7
喜欢 (0)
【你的支持, 我的动力】
分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址