关键字:ubuntu、镜像源、apt-get、速度慢、无法更新源
1 前言
现在微软正以前所未有的积极性主动拥抱多元化的开源世界,从参与各种开源社区到用巨大财力收购Github,都可以体现其开源的决心。基于Windows推出的WSL(Windows Subsystem for Linux)适用于Linux的Windows子系统,可以说Windows是最强的Linux发行版。现在完全可以使用Visual Studio Code在Windows下编辑,然后在WSL下进行编译,浑然一体。在做好WSL的相关准备以及配置后,Ubuntu也成功运行在Windows下运行起来,然而在国内特殊的环境下去更新Ubuntu的相关软件速度实在是慢得可怜,这时候就必须更换一下Ubuntu的镜像源。
2 环境
操作系统:Microsoft Windows 10
WSL:Ubuntu 20.04.1 LTS
系统工具:Windows Terminal
3 具体方法
3.1 确认版本
首先需要确认Ubuntu的发行版本,这一步非常重要,如果只是搜索一下到哪个文章复制粘贴镜像源文本去单纯地替换原镜像源就会产生奇怪的问题,使用如下命令:
lsb_release -a
可以得到当前系统发行版本的相关信息:
Distributor ID: Ubuntu
Description: Ubuntu 20.04.1 LTS
Release: 20.04
Codename: focal
值得一提的是不同的Ubuntu发行版本要不同的代号,以下列举部分LTS版本代号:
Ubuntu 16.04:xenial
Ubuntu 17.04:zesty
Ubuntu 18.04:bionic
Ubuntu 19.04:disco
Ubuntu 20.04:focal
3.2 修改镜像源
镜像源地址:/etc/apt/sources.list
修改镜像源前先备份一下原来的镜像源,防止以后出现什么问题不好回退,使用命令:
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
查看原来的镜像源文本cat /etc/apt/sources.list:
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://archive.ubuntu.com/ubuntu/ focal main restricted
# deb-src http://archive.ubuntu.com/ubuntu/ focal main restricted
## Major bug fix updates produced after the final release of the
## distribution.
deb http://archive.ubuntu.com/ubuntu/ focal-updates main restricted
# deb-src http://archive.ubuntu.com/ubuntu/ focal-updates main restricted
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu/ focal universe
# deb-src http://archive.ubuntu.com/ubuntu/ focal universe
deb http://archive.ubuntu.com/ubuntu/ focal-updates universe
# deb-src http://archive.ubuntu.com/ubuntu/ focal-updates universe
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://archive.ubuntu.com/ubuntu/ focal multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ focal multiverse
deb http://archive.ubuntu.com/ubuntu/ focal-updates multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ focal-updates multiverse
## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse
## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu focal partner
# deb-src http://archive.canonical.com/ubuntu focal partner
deb http://security.ubuntu.com/ubuntu/ focal-security main restricted
# deb-src http://security.ubuntu.com/ubuntu/ focal-security main restricted
deb http://security.ubuntu.com/ubuntu/ focal-security universe
# deb-src http://security.ubuntu.com/ubuntu/ focal-security universe
deb http://security.ubuntu.com/ubuntu/ focal-security multiverse
# deb-src http://security.ubuntu.com/ubuntu/ focal-security multiverse
使用sudo vim /etc/apt/sources.list将其中的网址替换为国内稳定的镜像源即可,国内较为稳定的镜像源网址如下:
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
3.3 更新镜像源
设置好Ubuntu镜像源之后需要更新:
sudo apt-get update
否则在使用sudo apt-get install upgrade命令时会提示错误Unable to locate package upgrade。
4 遇到的问题
目前网上大多数的文章都是Ubuntu 18.04 LTS发行版本,这次安装的Ubuntu是20.04 LTS版本,然后直接去复制网上的国内镜像源文本,导致软件更新出现了问题。错误的镜像源:
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
并不是说这个镜像源不对,只是这个不适用于Ubuntu 20.04 LTS,一开始更新与安装还挺正常的没有在意,但在后面安装GNU GCC去编译的时候就出现了问题,直接提示找不到头文件stdio.h,差一点去重装WSL。后面在网上找了半天才找到了解决方案,为了避免其他人踩坑,把这个问题记录下来,以供参考。如果出现了描述的问题,可使用如下方法解决:
# 1.删除 /var/lib/apt/lists/所有文件夹
sudo rm -rf /var/lib/apt/lists/*
# 2.删除 /var/lib/apt/lists/所有文件
sudo rm /var/lib/apt/lists/* -vf
# 3.修改镜像源为当前Ubuntu版本适用的国内镜像源
# 4.更新镜像源
sudo apt-get update
sudo apt-get install upgrade
5 参考文章