Linux中报错记录以及libRadtran的安装—Ubuntu
1、Linux中使用apt install 报错问题:
**问题**
E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
解决方法
在命令前加 sudo,例如:
sudo apt update
sudo apt install xxx
如果加 sudo 后 仍然报错,按下面顺序排查:
现象 | 处理办法 |
---|---|
1. 提示“sudo: 找不到命令” | 你当前可能是 root 用户,直接运行 apt update 即可。 |
2. 提示“用户不在 sudoers 文件中” | 你的账号没有管理员权限,需要让系统管理员把你加入 sudo 组: usermod -aG sudo 你的用户名 |
3. 提示“另一个进程正在占用 dpkg” | 有别的安装进程没关,等它结束或手动杀进程: sudo lsof /var/lib/dpkg/lock-frontend 找到 PID 后 sudo kill -9 PID |
2、使用aliyun镜像寻找perl软件包报错解决方法:
**问题**
Err:4 http://mirrors.aliyun.com/ubuntu jammy-security/main amd64 perl-modules-5.34 all 5.34.0-3ubuntu1.4404 Not Found [IP: 111.4.227.14 80]
E: Failed to fetch http://mirrors.aliyun.com/ubuntu/pool/main/p/perl/libperl5.34_5.34.0-3ubuntu1.4_amd64.deb 404 Not Found [IP: 111.4.227.14 80]
E: Failed to fetch http://mirrors.aliyun.com/ubuntu/pool/main/p/perl/perl_5.34.0-3ubuntu1.4_amd64.deb 404 Not Found [IP: 111.4.227.14 80]
E: Failed to fetch http://mirrors.aliyun.com/ubuntu/pool/main/p/perl/perl-base_5.34.0-3ubuntu1.4_amd64.deb 404 Not Found [IP: 111.4.227.14 80]
E: Failed to fetch http://mirrors.aliyun.com/ubuntu/pool/main/p/perl/perl-modules-5.34_5.34.0-3ubuntu1.4_all.deb 404 Not Found [IP: 111.4.227.14 80]
E: Internal Error, ordering was unable to handle the media swap
解决方法
阿里云镜像把 旧版 perl 5.34.0-3ubuntu1.4 删了,而本地 apt 缓存还指向这个版本,于是出现 404 Not Found。
思路:刷新软件源索引 → 换镜像 → 再装。
✅ 三步解决
1️⃣ 先更新软件列表(把旧索引清掉)
sudo apt clean # 清本地缓存
sudo apt update # 重新拉索引
如果 update 也 404,继续第 2 步。
2️⃣ 换官方主镜像(或清华/中科大)
sudo sed -i 's|http://mirrors.aliyun.com/ubuntu|http://archive.ubuntu.com/ubuntu|g' /etc/apt/sources.list
sudo apt update
国内网络慢就换成清华源:
sudo sed -i 's|http://mirrors.aliyun.com/ubuntu|https://mirrors.tuna.tsinghua.edu.cn/ubuntu|g' /etc/apt/sources.list
sudo apt update
3️⃣ 再装 perl
sudo apt install perl --fix-missing
✅ 一键脚本(懒人版)
sudo apt clean
sudo sed -i 's|http://mirrors.aliyun.com/ubuntu|https://mirrors.tuna.tsinghua.edu.cn/ubuntu|g' /etc/apt/sources.list
sudo apt update
sudo apt install perl --fix-missing
3、Linux中降级gfortran
下面给出最简、最稳、可回退的 gfortran 降级方案,适用于 Ubuntu 20.04/22.04/24.04。
核心思路:用官方 toolchain PPA 装旧版 → update-alternatives 做软链切换,不动系统原版本
✅ 一键步骤(以降到 gfortran-9 为例)
1. 添加旧版工具链源
sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
sudo apt update
2. 安装目标版本(可同时装多个)
sudo apt install gfortran-9 gfortran-10 gfortran-11 # 想降几就装几
3. 把 gfortran 指向 9(优先级 90,数字越大越优先)
sudo update-alternatives --install /usr/bin/gfortran \gfortran /usr/bin/gfortran-9 90
4. 可选:交互式切换
sudo update-alternatives --config gfortran
看到列表后输入对应编号即可,随时可切回来。
5. 验证
gfortran --version
# 应输出 GNU Fortran (Ubuntu 9.x.x) ...
🔙 随时回退
sudo update-alternatives --config gfortran
# 选回 11/10 即可
📌 常见版本号对照
目标版本 | 安装包名 |
---|---|
7.x | gfortran-7 |
8.x | gfortran-8 |
9.x | gfortran-9 |
10.x | gfortran-10 |
⚠️ 注意
- 不要手动删
/usr/bin/gfortran
,用update-alternatives
管理才安全。 - 若系统原本没有 gfortran,装完第 2 步后
/usr/bin/gfortran
会为空,执行第 3 步即可自动建立软链。
4、编译大型软件(WRF、HDF5)前,**把 CC=gcc-9 CXX=g++-9 FC=gfortran-9
写进环境变量,避免混用版本。
- 编译大型软件(WRF、HDF5)前,把
CC=gcc-9 CXX=g++-9 FC=gfortran-9
写进环境变量,避免混用版本。
编译大型软件(WRF、HDF5)前,把 CC=gcc-9 CXX=g+±9 FC=gfortran-9 写进环境变量,避免混用版本
具体操作
下面给出**“写一次就生效”**的三种常用做法,按场景挑即可——
全部以 gcc-9 / g+±9 / gfortran-9 为例,你可替换成任意版本。
✅ 方案 A:临时生效(只影响当前终端)
export CC=gcc-9
export CXX=g++-9
export FC=gfortran-9
export F77=gfortran-9 # 老代码可能还认 F77
接着直接 configure / make
即可。
关闭终端就失效,适合“我就编这一次”。
✅ 方案 B:一次配置,当前用户永久生效
把上面 4 行写进 ~/.bashrc
(或 ~/.zshrc
如果你用 zsh):
cat >> ~/.bashrc << 'EOF'
# 统一使用 GNU 9 系列编译器
export CC=gcc-9
export CXX=g++-9
export FC=gfortran-9
export F77=gfortran-9
EOF
立即加载:
source ~/.bashrc
以后每开一个新终端自动带这些变量,不影响系统或其他用户。
✅ 方案 C:只给某个工程生效(最干净)
在源码根目录新建 setenv.sh
:
#!/bin/bash
export CC=gcc-9
export CXX=g++-9
export FC=gfortran-9
export F77=gfortran-9
echo "Compiler set to GNU 9.x"
每次编译前 source setenv.sh
即可,离开目录就无效,CI/集群最推荐。
✅ 验证是否生效
echo $CC $CXX $FC
which $CC $CXX $FC
三行都应该指向 /usr/bin/gcc-9
等且版本一致:
gcc-9 --version
⚠️ 常见坑
- CMake 工程:
除了环境变量,显式传参更保险:cmake .. -DCMAKE_C_COMPILER=gcc-9 \-DCMAKE_CXX_COMPILER=g++-9 \-DCMAKE_Fortran_COMPILER=gfortran-9
- autotools 工程:
先export
再./configure
,不要./configure CC=gcc-9
混写,后者会覆盖变量。 - 模块系统(Lmod, EnvironmentModules):
在集群上module load gcc/9.3.0
即可,不要手动 export,防止版本打架。
🎯 一句话总结
临时用就 export;长期用写进
~/.bashrc
;项目级单独source setenv.sh
——编译 WRF/HDF5 前确认echo $CC
已是 gcc-9,就再也不会混版本。
5、libRadtran不同版本的编译信息
**libRadtran-2.0.3**
libRadtran is now configured for x86_64-unknown-linux-gnuSource directory: .Installation prefix: /home/lhp/software/libradC compiler: gcc-9 -g -O2 -WallSanitizer options:Fortran compiler: gfortran-9 -g -O2Fortran linker:Fortran libraries: -L/home/lhp/software/netcdf/lib -L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/9/../../.. -lgfortran -lm -lquadmathAWK: gawkPERL: /usr/bin/perlPYTHON:NCCMP:NetCDF library: -lnetcdf -lnslNetCDFF library: -L/home/lhp/software/netcdf/lib -lnetcdffGSL: -lgsl -lgslcblas -lmGMP:nccmp: noPolRadtran: yesKato et al.: yesFu and Liou: yesKratz (AVHRR): yessos: yesOPAC: noKey et al. (2002): yesYang/Key/Mayer: yesMie (water clouds): noBaum et al. (2005): noBaum et al. (2014): noHEY (ice phamat): noYang et al. (2013): noREPTRAN coarse: yesREPTRAN extended: noSBDART/LOWTRAN: yesFull tests: noBRDF: yesMYSTIC: yesTENSTREAM solver: noPETSC libs: noMPI: yesALIS: yesVROOM: yes
**libRadtran-2.0.4**
libRadtran is now configured for x86_64-unknown-linux-gnuSource directory: .Installation prefix: /usr/localC compiler: gccC++ compiler: g++ -std=c++11 -g -O2Sanitizer options:Fortran compiler: gfortran -g -O2Fortran linker:Fortran libraries: -L/home/lhp/software/netcdf/lib -L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/9/../../.. -lgfortran -lm -lquadmathPETSC includes:PETSC libs:AWK: gawkPERL: /usr/bin/perlPYTHON: /usr/bin/python2NCO:NCCMP:NetCDF found: yes 4.8.0NetCDF CFLAGS: -I/home/lhp/software/netcdf/include -I/home/lhp/software/netcdf/includeNetCDF LDFLAGS: -L/home/lhp/software/netcdf/libNetCDF LIBS: -lnetcdfNetCDF FFLAGS: -I/home/lhp/software/netcdf/include -I/home/lhp/software/netcdf/includeNetCDF FLIBS: -lnetcdffGSL: -lgsl -lgslcblas -lmGMP:nco: nonccmp: noPolRadtran: yesKato et al.: yesFu and Liou: yesKratz (AVHRR): yessos: yesOPAC: noKey et al. (2002): yesYang/Key/Mayer: yesMie (water clouds): noBaum et al. (2005): noBaum et al. (2014): noHEY (ice phamat): noYang et al. (2013): noREPTRAN coarse: yesREPTRAN extended: noSBDART/LOWTRAN: yesFull tests: noBRDF: yesMYSTIC: yesTENSTREAM solver: noPETSC: noSTAR-ENGINE: noTWOMAXRND3C: yesALIS: yesVROOM: yes
**libRadtran-2.0.5**
libRadtran is now configured for x86_64-unknown-linux-gnuSource directory: .Installation prefix: /usr/localC compiler: gcc -g -O2C++ compiler: g++ -std=c++11 -g -O2Sanitizer options:Code coverage options:Fortran compiler: gfortran -g -O2Fortran linker:Fortran libraries: -L/home/lhp/software/netcdf/lib -L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/9/../../.. -lgfortran -lm -lquadmathPETSC includes:PETSC libs:AWK: gawkPERL: /usr/bin/perlPYTHON: /usr/bin/python3NCO:NCCMP:NetCDF found: yes 4.8.0NetCDF CFLAGS: -I/home/lhp/software/netcdf/include -I/home/lhp/software/netcdf/includeNetCDF LDFLAGS: -L/home/lhp/software/netcdf/libNetCDF LIBS: -lnetcdfNetCDF FFLAGS: -I/home/lhp/software/netcdf/include -I/home/lhp/software/netcdf/includeNetCDF FLIBS: -lnetcdffGSL: -lgsl -lgslcblas -lmGMP:nco: nonccmp: noKato et al.: yesKratz (AVHRR): yessos: yesOPAC: noMie (water clouds): noBaum et al. (2005): noBaum et al. (2014): noHEY (ice phamat): noYang et al. (2013): noREPTRAN coarse: yesREPTRAN extended: noREPINT: yesSBDART/LOWTRAN: yesFull tests: yesBRDF: yesMYSTIC: yesTENSTREAM solver: noPETSC: noSTAR-ENGINE: noTWOMAXRND3C: yesALIS: yesVROOM: yes
**libRadtran-2.0.6**
libRadtran is now configured for x86_64-unknown-linux-gnuSource directory: .Installation prefix: /usr/localC compiler: gcc -g -O2C++ compiler: g++ -std=c++11 -g -O2Sanitizer options:Code coverage options:Fortran compiler: gfortran -g -O2Fortran linker:Fortran libraries: -L/home/lhp/software/netcdf/lib -L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/9/../../.. -lgfortran -lm -lquadmathPETSC includes:PETSC libs:AWK: gawkPERL: /usr/bin/perlPYTHON: /usr/bin/python3NCO:NCCMP:NetCDF found: yes 4.8.0NetCDF CFLAGS: -I/home/lhp/software/netcdf/include -I/home/lhp/software/netcdf/includeNetCDF LDFLAGS: -L/home/lhp/software/netcdf/libNetCDF LIBS: -lnetcdfNetCDF FFLAGS: -I/home/lhp/software/netcdf/include -I/home/lhp/software/netcdf/includeNetCDF FLIBS: -lnetcdffGSL: -lgsl -lgslcblas -lmGMP:nco: nonccmp: noKato et al.: yesKratz (AVHRR): yessos: yesOPAC: noMie (water clouds): noBaum et al. (2005): noBaum et al. (2014): noHEY (ice phamat): noYang et al. (2013): noREPTRAN coarse: yesREPTRAN extended: yesREPINT: yesHAMSTER: noSBDART/LOWTRAN: yesFull tests: yesBRDF: yesMYSTIC: yesTENSTREAM solver: noTENSTREAM dynamic: noPETSC: noSTAR-ENGINE: noTWOMAXRND3C: yesALIS: yesVROOM: yes
6、安装libRadtran报错之一
make[1]: Entering directory '/home/lhp/soft/libRadtran-2.0.3/src_py'
writeLex.py
make[1]: writeLex.py: No such file or directory
make[1]: *** [Makefile:35: uvspec_lex.l] Error 127
make[1]: Leaving directory '/home/lhp/soft/libRadtran-2.0.3/src_py'
make: *** [Makefile:39: all] Error 2
出错原因
src_py/Makefile 把 writeLex.py 当成可执行文件调用,但 没加 python3 前缀,导致系统找不到。
✅ 最快修复:进入 src_py 手动生成 lex 文件 —— 实践可行
cd ~/soft/libRadtran-2.0.3/src_py
python3 writeLex.py # 生成 uvspec_lex.l
cd .. # 回到源码根目录
make -j$(nproc) # 继续全局编译
✅ 或者一次性改 Makefile(永久解决)—— 未进行尝试
sed -i 's/^writeLex\.py/python3 writeLex.py/' src_py/Makefile
make -j$(nproc)
✅ 验证
ls src_py/uvspec_lex.l # 应该已生成
make -j$(nproc) # 应继续往下编,不再报 127
🎯 一句话
src_py/Makefile
漏写python3
;手动python3 writeLex.py
一下,或者 sed 替换,再make
就能顺利通过。
7、安装libRadtran-2.0.3/2.0.4的安装包以及命令-Ubuntu22.04
安装包:
通过网盘分享的文件:链接:
https://pan.baidu.com/s/1LWUFtvqpXVvMt0QttgkswA?pwd=8sa5 提取码: 8sa5
1、在进行安装之前提前使用命令进行一下的安装
:
sudo apt install make
sudo apt install build-essential
sudo apt install flex bison
sudo apt install perl --fix-missing
2、首先环境变量
vim ~/.bashrc
----------------------------------------------------------
export NETCDF=/home/lhp/software/netcdf
export PATH=${NETCDF}/bin:$PATH
export LD_LIBRARY_PATH=${NETCDF}/lib:${LD_LIBRARY_PATH}
export LD_RUN_PATH=${NETCDF}/lib:${LD_RUN_PATH}
export CPPFLAGS=-I/home/lhp/software/netcdf/include
export LDFLAGS=-L/home/lhp/software/netcdf/lib
---------------------------------------------------------
source ~/.bashrc
3、安装GSL
tar -xvf gsl-2.6.tar.gz
cd gsl-2.6
./configure --prefix=/home/lhp/software/netcdf
make -j8
make install
4、安装Zlib
tar -xvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure --prefix=/home/lhp/software/netcdf
make -j8
make install
5、安装HDF5
PS:需要更换一下gfortran版本,我使用的是gfortran-9;
在有多个gfortran版本的前提下,设置gfortran-9的优先级;sudo update-alternatives --install /usr/bin/gfortran \gfortran /usr/bin/gfortran-9 90
sudo update-alternatives --config gfortran
在上面的问题中有gfortran更换版本的设置,可以设置只在此命令窗口使用gfortran-9版本;
命令如下:
export CC=gcc-9
export CXX=g++-9
export FC=gfortran-9
export F77=gfortran-9tar -xvf hdf5-1.10.6.tar.gz
cd hdf5-1.10.6./configure --with-zlib=/home/lhp/software/netcdf --prefix=/home/lhp/software/netcdf --enable-fortran --enable-shared
make -j8
make check
make install
6、安装Curl
tar -xvf curl-7.55.1.tar.gz
cd curl-7.55.1
./configure --with-zlib=/home/lhp/software/netcdf --prefix=/home/lhp/software/netcdf
make -j8
make check
make install
7、安装netcdf-C
tar -xvf netcdf-c-4.8.0.tar.gz
cd netcdf-c-4.8.0
./configure --prefix=/home/lhp/software/netcdf
make -j8
make install
8、安装netcdf-Fortran
tar -xvf netcdf-fortran-4.5.3.tar.gz
cd netcdf-fortran-4.5.3
./configure --prefix=/home/lhp/software/netcdf
make -j8
make install
9、安装GMP
tar -xvf gmp-6.2.0.tar
cd gmp-6.2.0
./configure --prefix=/home/lhp/software/netcdf
make -j8
make install
10、安装pnetcdf
sudo apt install openmpi-bin libopenmpi-dev
which mpif90tar -xvf pnetcdf-1.12.3.tar.gz
cd pnetcdf-1.12.3./configure --prefix=/home/lhp/software/netcdf CC=mpicc CXX=mpicxx FC=mpif90 F77=mpif90
make
make install
11、安装nccmp
tar -xvf nccmp-1.8.2.1.tar.gz
cd nccmp-1.8.2.1
./configure --prefix=/home/lhp/software/netcdf
make -j8
make install
12、安装python,libRadtran-2.0.3支持python2
sudo apt install python3 python3-dev
sudo apt install python2 python2-dev
13、安装libRadtran
tar -xvf libradtran-2.0.3.tar.gz
cd libRadtran-2.0.3
./configure --prefix=/home/lhp/software/librad
make
make install
make check
最后安装的报错是问题6:
解决方案是:
✅ 最快修复:进入 src_py 手动生成 lex 文件 —— 实践可行
——后面在检查,发现是路径信息有误,更改路径信息之后,就可以成功运行了
cd ~/soft/libRadtran-2.0.3/src_py
python2 writeLex.py # 生成 uvspec_lex.l
cd .. # 回到源码根目录
make -j$(nproc) # 继续全局编译
最后的环境配置
vim ~/.bashrc
========================================================
export LIBRAD=/home/lhp/software/librad
export DATA=${LIBRAD}/share/libRadtran/data
export INP=${LIBRAD}/share/libRadtran/examples
export PATH=${LIBRAD}/bin:$PATH
export LD_LIBRARY_PATH=${LIBRAD}/lib:${LD_LIBRARY_PATH}
export LD_RUN_PATH=${LIBRAD}/lib:${LD_RUN_PATH}
========================================================
source ~/.bashrc
安装报错问题2
make[1]: Entering directory '/home/lhp1804/soft/libRadtran-2.0.4/libsrc_c'
make[1]: *** No rule to make target '/opt/local/include/netcdf.h', needed by 'triangle_surface.o'. Stop.
make[1]: *** Waiting for unfinished jobs....compiling allocnd.ccompiling ascii.ccompiling bandec.ccompiling cdisort.ccompiling cnv.ccompiling common_math.c
make[1]: Leaving directory '/home/lhp1804/soft/libRadtran-2.0.4/libsrc_c'
Makefile:39: recipe for target 'all' failed
make: *** [all] Error 2
解决方式
sudo apt-get install libnetcdf-dev
可以运行。
安装报错3
rm -f ../lib/libRadtran_f.a
make[1]: Leaving directory '/home/lhp1804/soft/libRadtran-2.0.4/libsrc_f'
make[1]: Entering directory '/home/lhp1804/soft/libRadtran-2.0.4/src'
flex -o uvspec_lex.c ../src_py/uvspec_lex.l
gawk 'NF==2{print "#define "$1" "$2}' sbtaugas.param > sbtaugas.h
make[1]: *** No rule to make target '/opt/local/include/gsl/gsl_math.h', needed by 'angres.o'. Stop.
make[1]: Leaving directory '/home/lhp1804/soft/libRadtran-2.0.4/src'
Makefile:39: recipe for target 'all' failed
make: *** [all] Error 2
解决方法
sudo apt install libgsl-dev已经安装了gsl,配置的时候指定gsl路径./configure --prefix=/home1804/lhp/software/librad --with-gsl=/home/lhp1804/software/netcdf
**出现上述的原因主要是环境与路径的问题**
以上两个错误都是因为配置的
8、运行.sh报错——-bash: ./thread_dis2.sh: /bin/bash^M: bad interpreter: No such file or directory
lhp@DESKTOP-CR5PQUC:~/code$ ./thread_dis2.sh
-bash: ./thread_dis2.sh: /bin/bash^M: bad interpreter: No such file or directory
可能出错的原因
这个错误 bad interpreter: No such file or directory 通常是由于脚本文件包含了 Windows 风格的换行符(CRLF),
而不是 Unix/Linux 风格的换行符(LF)。在 Linux 系统中,bash 解释器期望脚本文件使用 LF 换行符。
方法一:使用 dos2unix 工具
安装 dos2unix:
在基于 Debian 或 Ubuntu 的系统上,运行以下命令安装 dos2unix:
sudo apt install dos2unix转换文件:
运行以下命令将脚本文件的换行符从 CRLF 转换为 LF:
dos2unix thread_dis2.sh再次运行脚本:./thread_dis2.sh
**方法二:使用 sed 命令手动转换**
——**亲测可用**
使用 sed 命令:
sed -i 's/\r$//' thread_dis2.sh
这个命令会使用 sed 编辑器在脚本文件中查找每行末尾的回车符(\r)并删除它。-i 选项表示直接在文件中进行修改。
9、linux 中的Ubuntu换源
在 Ubuntu 里“换源”就是把 /etc/apt/sources.list
里的官方仓库地址替换成国内镜像(清华、阿里、中科大等),加快软件下载速度。下面给出 一行命令无脑脚本 和 手动操作 两种方式,22.04(Jammy)/20.04(Focal)/18.04(Bionic) 都通用。
🔧 一键自动脚本(推荐)
# 先备份
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak# 使用清华源(Jammy 22.04 示例,可自动检测版本)
sudo sed -i 's@http://.*.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list# 更新缓存
sudo apt update
想换阿里源?
把上面 tuna.tsinghua.edu.cn
换成 mirrors.aliyun.com
即可。
📝 手动换源(图形化 or 文本)
- 编辑文件
sudo nano /etc/apt/sources.list
- 把里面所有
http://archive.ubuntu.com
或http://cn.archive.ubuntu.com
替换成
例如:https://mirrors.tuna.tsinghua.edu.cn
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
- 保存后执行
sudo apt update
✅ 检查是否成功
apt policy | head -n 4
输出里出现 mirrors.tuna.tsinghua.edu.cn
或 mirrors.aliyun.com
即换源完成。
⚠️ 注意
- 不同版本代号要对应:
22.04 →jammy
20.04 →focal
18.04 →bionic
一键脚本已自动识别,手动改时别写错。 - 换源后 首次
apt update
会明显变快。
1. 备份 & 清空旧列表
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo truncate -s 0 /etc/apt/sources.list
2. 四选一:把对应块整体复制执行
⭐ 清华大学(TUNA)
sudo tee /etc/apt/sources.list << 'EOF'
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
EOF
⭐ 阿里云
sudo tee /etc/apt/sources.list << 'EOF'
deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
EOF
⭐ 中国科学技术大学(USTC)
sudo tee /etc/apt/sources.list << 'EOF'
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
EOF
⭐ 北京大学(北大镜像)
sudo tee /etc/apt/sources.list << 'EOF'
deb https://mirrors.pku.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.pku.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.pku.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.pku.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
EOF
3. 更新缓存
sudo apt update
4. 一键脚本(以清华为例)
# 保存为 change-mirror.sh && chmod +x change-mirror.sh
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo sed -i 's@http://.*.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g' /etc/apt/sources.list
sudo apt update
版本对照
Ubuntu 版本 | 代号 |
---|---|
22.04 LTS | jammy |
20.04 LTS | focal |
18.04 LTS | bionic |
把上面模板里的 jammy
换成对应代号即可。