ubuntu配置golang环境

本文提供了解决Ubuntu系统中常见的时钟不同步问题的方法,详细介绍了如何将APT软件源更换为阿里云源以提高下载速度,并给出了针对golang版本过低问题的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备工作

解决默认安装的时钟不同步问题

apt使用阿里云软件源

解决默认apt安装go版本过低的问题

 

一、Ubuntu时钟不同步问题

切换root用户

su root

打开时钟同步配置文件

sudo vim /etc/systemd/timesyncd.conf

修改同步地址NTP=0.cn.pool.ntp.org 1.cn.pool.ntp.org 2.cn.pool.ntp.org 3.cn.pool.ntp.org

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See timesyncd.conf(5) for details.

[Time]
NTP=0.cn.pool.ntp.org 1.cn.pool.ntp.org 2.cn.pool.ntp.org 3.cn.pool.ntp.org
FallbackNTP=ntp.ubuntu.com
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048

重启timesyncd服务

sudo systemctl restart systemd-timesyncd.service

确认状态

sudo systemctl status systemd-timesyncd.service
● systemd-timesyncd.service - Network Time Synchronization
   Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/systemd-timesyncd.service.d
           └─disable-with-time-daemon.conf
   Active: active (running) since Tue 2020-01-07 09:59:10 CST; 34min ago
     Docs: man:systemd-timesyncd.service(8)
 Main PID: 2031 (systemd-timesyn)
   Status: "Synchronized to time server 78.46.102.180:123 (0.cn.pool.ntp.org)."
    Tasks: 2
   Memory: 264.0K
      CPU: 20ms
   CGroup: /system.slice/systemd-timesyncd.service
           └─2031 /lib/systemd/systemd-timesyncd

Jan 07 09:59:10 ubuntu systemd[1]: Starting Network Time Synchronization...
Jan 07 09:59:10 ubuntu systemd[1]: Started Network Time Synchronization.
Jan 07 09:59:21 ubuntu systemd-timesyncd[2031]: Timed out waiting for reply from 124.108.20.1:123 (0.cn.pool.ntp.org).
Jan 07 09:59:24 ubuntu systemd-timesyncd[2031]: Synchronized to time server 78.46.102.180:123 (0.cn.pool.ntp.org).

二、apt切换阿里源

查看自己的系统版本

lsb_release -c
Codename:       xenial

同样的我们也可以得到之前任意版本的系统代号:
Ubuntu 12.04 (LTS)代号为precise。
Ubuntu 14.04 (LTS)代号为trusty。
Ubuntu 15.04 代号为vivid。
Ubuntu 15.10 代号为wily。
Ubuntu 16.04 (LTS)代号为xenial。
Ubuntu 18.04 (LTS)代号为bionic。

备份原来的apt源文件,预防出现问题进行恢复

sudo cp sources.list sources.list.backup

修改源文件内容,vim命令ggdG全选删除

sudo vim /etc/apt/sources.list

16.04的源文件

deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties
#deb http://archive.canonical.com/ubuntu xenial partner
#deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse

18.04的源文件

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

更新源,更新软件

sudo apt-get update
sudo apt-get upgrade  

三、apt安装golang版本过低的问题

卸载旧版

sudo apt-get remove golang-go
sudo apt-get remove --auto-remove golang-go

下载新版本

wget https://studygolang.com/dl/golang/go1.12.5.linux-amd64.tar.gz
tar -zxvf go1.12.5.linux-amd64.tar.gz
sudo mv go /usr/local/

设置环境变量

sudo vim ~/.profile

在文件最后增加


export GOROOT=/usr/local/go           # 安装目录
export GOPATH=/opt/go                 # 工作环境
export GOPROXY=https://goproxy.io     # 解决 golang.org/x/... 系列包无法下载的问题
export GOARCH=amd64                    
export GOOS=linux
export GOTOOLS=$GOROOT/pkg/tool
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

加载配置使环境变量生效

source ~/.profile

测试go

go version
go version go1.12.5 linux/amd64

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值