ubuntu下安装go环境,包括GOROOT和GOPATH配置
<p>虽然之前安装过,但是还不够熟练,这次做个记录,方便以后翻看。</p><p> </p>
<p>环境:在一台全新的Ubuntu机器上安装go。</p>
<p> </p>
<p>2022.6.14</p>
<p>需求:用root安装go环境,全局配置,所以用户都能用。</p>
<p>wget -c https://go.dev/dl/go1.17.11.linux-amd64.tar.gz # 下载amd64,可通过 $ cat /proc/version 查看</p>
<p>tar xfz go1.17.2.darwin-amd64.tar.gz<span class="hljs-number"> -C /usr/local</span></p>
<p><strong># 配置环境变量生效顺序,查看 https://www.jianshu.com/p/ba2907d8668f</strong></p>
<p>/etc/profile,这也是linux默认的shell主环境变量,每个用户登录都会加载这个文件</p>
<p>/etc/profile.d/ ,自动定义开机启动的脚本可以放在这个目录下</p>
<p>$HOME/.bash_profile,用户个人的环境变量文件</p>
<div>
<div>
<p>在$HOME/.bash_profile文件中,会加载$HOME/.bashrc(也是用户个人的环境变量文件,包含在$HOME/.bash_profile中),如果有环境变量就加载,如果没有就不加载</p>
<p>在$HOME/.bashrc文件中,又会加载/etc/bashrc(这是全局环境变量),同理,有环境变量就加载,没有就不加载</p>
</div>
<br>
<pre class="brush:bash;gutter:true;">#添加Gopath路径
export GOROOT=/usr/local/go
export GOPATH=/data/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH </pre>
source /etc/profile #所有人都可以用,自己修改后要source才生效</div>
<div>其他人登录时已生效。</div>
<div> </div>
<div> </div>
<div> </div>
<p> </p>
<p>$ cat /proc/version<br>Linux version 4.15.0-1065-aws (buildd@lgw01-amd64-035) (gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)) #69-Ubuntu SMP Thu Mar 26 02:17:29 UTC 2020</p>
<p> </p>
<p><strong>升级go版本</strong></p>
<ul>
<li>下载,tar.gz文件。</li>
<li>运行以下解压命令,覆盖旧版本。就成功了。</li>
</ul>
<p><code class="hljs apache"><span class="hljs-attribute"><span class="hljs-number"><span class="hljs-number"><span class="hljs-number"><span class="hljs-number"><span class="hljs-comment"><span class="hljs-attribute">tar xfz go1.17.2.darwin-amd64.tar.gz<span class="hljs-number"><span class="hljs-number"><span class="hljs-number"><span class="hljs-number"> -C /usr/local</span></span></span></span></span></span></span></span></span></span></span></code></p>
<pre></pre>
<p> </p>
<h3 id="1下载go语言安装包">1.下载Go语言安装包</h3>
<p>官网:下载地址</p>
<h3 id="2-获取安装包">2. 获取安装包</h3>
<pre><code class="hljs apache"> <span class="hljs-attribute">wget https://studygolang.com/dl/golang/go1.17.linux-amd64.tar.gz<span class="hljs-number"><span class="hljs-number"><span class="hljs-number"><span class="hljs-number">
<span class="hljs-comment"># 解压文件
<span class="hljs-attribute">tar xfz go1.17.linux-amd64.tar.gz<span class="hljs-number"><span class="hljs-number"><span class="hljs-number"><span class="hljs-number"> -C /usr/local
</span></span></span></span></span></span></span></span></span></span></span></code></pre>
<h3 id="3-配置全局变量">3. 配置环境变量</h3>
<p>go语言的环境变量配置,需要两个值:</p>
<ol>
<li><code>GOROOT</code>是系统上安装Go软件包的位置</li>
<li><code>GOPATH</code>是工作目录的位置</li>
</ol>
<div class="cnblogs_Highlighter">
<pre class="brush:bash;gutter:true;">#修改~/.profile
vim ~/.profile
#添加Gopath路径
export GOROOT=/usr/local/go
export GOPATH=/data/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
# 激活配置
source ~/.profile
</pre>
</div>
<p> </p>
<pre>注意:尝试修改 ~/.bashrc,添加环境变量,并没有生效。</pre>
<h3 id="4验证是否配置成功">4.验证是否配置成功</h3>
<p>$ go version<br>go version go1.17 linux/amd64<br> </p>
<p> </p>
<pre><span style="font-family: "PingFang SC", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px"><br>继续安装make<br><br>apt install make </span></pre>
<p> </p>
<pre><code>sudo apt-get update</code></pre>
<pre><code>sudo apt-get install build-essential</code></pre>
<p> </p>
<p> </p>
<p>安装与删除</p>
<div class="cnblogs_code">
<pre>apt <span style="color: rgba(0, 0, 255, 1)">install</span> golang-<span style="color: rgba(0, 0, 0, 1)">go# 安装之后发现是1.10版本,太老,没法用
apt rmove golang</span>-go# 卸载</pre>
</div>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p><strong>早期版本</strong></p>
<p>$ go version</p>
<p>Command 'go' not found, but can be installed with:</p>
<p>snap install go # version 1.16.2, or<br>aptinstall golang-go<br>aptinstall gccgo-go</p>
<p>See 'snap info go' for additional versions.</p>
<p> </p>
<p>$ snap install go<br>error: This revision of snap "go" was published using classic confinement and thus may perform<br> arbitrary system changes outside of the security sandbox that snaps are usually confined to,<br> which may put your system at risk.</p>
<p> If you understand and want to proceed repeat the command including --classic.</p>
<p> </p>
<p>虽然1.16.2是最新的,但我安装了1.15稳定版</p>
<p>$ snap install go --channel=1.15/stable --classic<br>go (1.15/stable) 1.15.10 from Michael Hudson-Doyle (mwhudson) installed<br>root@ip-172-31-44-132:/data# go version<br>Command 'go' is available in '/snap/bin/go'<br>The command could not be located because '/snap/bin' is not included in the PATH environment variable.<br>go: command not found</p>
<p> </p>
<p>$ cd ~</p>
<p>$ vi .profile</p>
<p>添加</p>
<p>export PATH=$PATH:/snap/bin</p>
<p>$ source .profile</p>
<p>$ go version</p>
<p><em id="__mceDel">go version go1.15.10 linux/amd64</em></p>
<p> </p>
<p> </p>
<p>$ make GenesisHeight=1121818 install</p>
<p>Command 'make' not found, but can be installed with:</p>
<p>apt install make<br>apt install make-guile</p>
<p> </p>
<p>$ apt install make</p>
<p>$ apt install make-guile</p>
<p> </p>
<p>$ make GenesisHeight=1121818 install</p>
<p> </p>
<p>$ apt install gcc</p>
<p>$ gcc --version</p>
<p>$ make GenesisHeight=1121818 install</p>
<p>编译成功。</p>
<p> </p>
<p>$ okexchaind version --long<br>okexchaind: command not found</p>
<p>添加环境变量:</p>
<p>$ vi .profile</p>
<p>添加</p>
<p>export PATH=$PATH:/root/go/bin</p>
<p>$ source .profile</p>
<p> </p>
<p>命令行成功:</p>
<p>name: okexchain<br>server_name: okexchaind<br>client_name: okexchaincli<br>version: v0.16.8.3<br>commit: bd2ab2abdceb46c4cbdfef5c64af92b6b2711fb4<br>build_tags: netgo<br>go: go version go1.15.10 linux/amd64</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/zccst/p/14563885.html
頁:
[1]