树莓派4/5:设置apt、pip、conda首选清华镜像源

一、教程简介

在中国大陆地区,使用清华镜像源可以显著缩短资源下载时间。

本教程介绍如何将清华镜像源设置为树莓派的apt、pip、conda下载的首选项(默认项)。其中,apt和pip为树莓派系统自带,conda则需要安装miniforge后方可使用。因此,本教程将同步介绍如何在树莓派上安装和使用conda。

本教程使用Windows 11计算机,通过SSH远程控制4B树莓派 (Lite OS, 64Bit),所有操作均在Mobaxterm软件上执行。

如果不知道如何安装无桌面版本的树莓派系统,请先使用以下教程。

http://t.csdnimg.cn/q3tiaicon-default.png?t=O83Ahttp://t.csdnimg.cn/q3tia

二、设置apt首选清华镜像源

1、编辑apt下载源配置文件

# 编辑sources.list
sudo nano /etc/apt/sources.list
# 设置清华镜像源
deb https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ bookworm main

注意,应该保留自动的apt下载源,以下是修改后的sources.list文件。

deb https://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ bookworm main
deb http://deb.debian.org/debian bookworm main contrib non-free non-free-firmware
deb http://deb.debian.org/debian-security/ bookworm-security main contrib non-free non-free-firmware
deb http://deb.debian.org/debian bookworm-updates main contrib non-free non-free-firmware
# Uncomment deb-src lines below then 'apt-get update' to enable 'apt-get source'
#deb-src http://deb.debian.org/debian bookworm main contrib non-free non-free-firmware
#deb-src http://deb.debian.org/debian-security/ bookworm-security main contrib non-free non-free-firmware
#deb-src http://deb.debian.org/debian bookworm-updates main contrib non-free non-free-firmware

编辑完成后,使用CTRL+X关闭文件,并键入Y,最后按Enter。

2、更新apt下载源配置

使用以下指令更新apt下载源,如果看到新增加的清华镜像源,则说明sources.list已成功添加。

sudo apt update

3、更新软件

sudo apt upgrade

三、设置pip首选清华镜像源

1、打开pip下载源配置文件

mkdir -p $HOME/.pip && nano $HOME/.pip/pip.conf

2、编辑pip下载源配置文件

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
extra-index-url = https://pypi.org/simple
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

[install]
extra-index-url =
    https://mirrors.aliyun.com/pypi/simple/
    https://pypi.org/simple

以上二选一,前一个仅添加清华镜像源,后一个多添加阿里云镜像源。

同样地,编辑完成后,使用CTRL+X关闭文件,并键入Y,最后按Enter。

四、安装miniforge

1、下载安装脚本

# 如果第一个无法下载,则使用第二个

# GitHub链接
wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-aarch64.sh
# 加速链接
wget https://www.ghproxy.cn/https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-aarch64.sh

2、设置可执行权限

chmod +x Miniforge3-Linux-aarch64.sh

3、运行安装脚本

./Miniforge3-Linux-aarch64.sh -b

4、添加到PATH环境

echo 'export PATH="$HOME/miniforge3/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

5、初始化conda

输入以下指令。当显示以下界面时,说明conda指令可用。

conda

输入以下指令,初始化conda。

conda init

重启树莓派,输入以下指令。

sudo reboot

然后键入R。

重启后,用户名前方出现了(base)字样,表示conda已经完整初始化。

五、设置conda首选清华镜像源

1、添加清华镜像源并设默认

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge

2、设置通道优先级为严格模式

conda config --set channel_priority strict

3、设置镜像源URLs显示

conda config --set show_channel_urls yes

4、检查通道

conda config --show channels

如果显示以下界面,说明conda已经首选清华镜像源。

END

### 如何在Linux系统中配置清华大学的镜像源 对于Debian及其衍生发行版(如Ubuntu),可以通过编辑`/etc/apt/sources.list`文件来更改APT软件包管理器使用的镜像源为清华大学的镜像源[^1]。 #### 修改 APT 软件库地址 打开终端并使用文本编辑工具修改`sources.list`文件: ```bash sudo nano /etc/apt/sources.list ``` 将原有的官方源链接替换为清华大学提供的对应版本的源链接。例如,如果正在使用的是Debian Buster,则应更改为如下形式的内容之一: ```plaintext deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free ... ``` 完成编辑之后保存退出,并更新本地软件索引以使新的设置生效: ```bash sudo apt-get update ``` #### 更改 PyPI 的默认镜像站至清华 为了加速Python依赖项的获取速度,可以指定pip使用清华大学开源软件镜像服务中的PyPI镜像作为默认下载站点。创建或编辑用户家目录下的`.pip/pip.conf`文件,在其中加入以下内容: ```ini [global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple ``` 这会使得后续通过pip安装任何Python包时自动优先尝试从清华大学的缓存服务器上拉取资源。 #### Anaconda 或 Miniconda 使用国内镜像加快下载速度 当涉及到Anaconda或者Miniconda环境构建的时候,同样推荐切换到中国科学技术大学提供的镜像仓库以便提高效率。具体操作是在初始化完成后立即执行下列指令: ```bash conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/ conda config --set show_channel_urls yes ``` 以上措施能够显著改善在国内网络环境下Linux系统的软件部署体验,减少因国际链路波动带来的不便影响[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值