|
|
更换为国内源可以提高软件包下载速度。常用的国内源有中科大、清华大学、阿里云等。下面是将 Debian Bookworm 的源替换为国内源的步骤:
|
|
|
|
|
|
### 步骤:
|
|
|
|
|
|
1. **备份当前源列表(可选):**
|
|
|
|
|
|
```bash
|
|
|
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
|
|
|
```
|
|
|
|
|
|
2. **编辑 `sources.list` 文件:**
|
|
|
|
|
|
使用你喜欢的文本编辑器(例如 `nano`、`vim` 或 `gedit`)编辑 `/etc/apt/sources.list` 文件:
|
|
|
|
|
|
```bash
|
|
|
sudo nano /etc/apt/sources.list
|
|
|
```
|
|
|
|
|
|
3. **更新源列表内容:**
|
|
|
|
|
|
下面是使用清华大学的镜像源作为示例,可以将其内容粘贴到 `sources.list` 文件中:
|
|
|
|
|
|
```plaintext
|
|
|
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
|
|
|
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware
|
|
|
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
|
|
|
#deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
|
|
|
#deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware
|
|
|
#deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
|
|
|
```
|
|
|
|
|
|
其他可用的国内源示例:
|
|
|
|
|
|
**中科大镜像源:**
|
|
|
|
|
|
```plaintext
|
|
|
deb http://mirrors.ustc.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
|
|
|
deb http://mirrors.ustc.edu.cn/debian-security/ bookworm-security main contrib non-free non-free-firmware
|
|
|
deb http://mirrors.ustc.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
|
|
|
#deb-src http://mirrors.ustc.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
|
|
|
#deb-src http://mirrors.ustc.edu.cn/debian-security/ bookworm-security main contrib non-free non-free-firmware
|
|
|
#deb-src http://mirrors.ustc.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
|
|
|
```
|
|
|
|
|
|
**阿里云镜像源:**
|
|
|
|
|
|
```plaintext
|
|
|
deb http://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware
|
|
|
deb http://mirrors.aliyun.com/debian-security/ bookworm-security main contrib non-free non-free-firmware
|
|
|
deb http://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware
|
|
|
#deb-src http://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware
|
|
|
#deb-src http://mirrors.aliyun.com/debian-security/ bookworm-security main contrib non-free non-free-firmware
|
|
|
#deb-src http://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware
|
|
|
```
|
|
|
|
|
|
4. **保存并退出编辑器:**
|
|
|
|
|
|
- 如果使用 `nano`,按 `Ctrl+O` 保存文件,然后按 `Ctrl+X` 退出编辑器。
|
|
|
|
|
|
5. **更新软件包列表:**
|
|
|
|
|
|
运行以下命令来更新软件包列表:
|
|
|
|
|
|
```bash
|
|
|
sudo apt-get update
|
|
|
```
|
|
|
|
|
|
### 验证更新
|
|
|
|
|
|
在运行 `sudo apt-get update` 后,你应该看到类似如下的输出,表示成功从新的软件源获取了软件包列表:
|
|
|
|
|
|
```plaintext
|
|
|
Hit:1 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm InRelease
|
|
|
Hit:2 https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security InRelease
|
|
|
Hit:3 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm-updates InRelease
|
|
|
Reading package lists... Done
|
|
|
```
|
|
|
|
|
|
### 安装和升级软件包
|
|
|
|
|
|
如果需要升级所有已安装的软件包,可以运行:
|
|
|
|
|
|
```bash
|
|
|
sudo apt-get upgrade
|
|
|
```
|
|
|
|
|
|
或者安装特定软件包:
|
|
|
|
|
|
```bash
|
|
|
sudo apt-get install <package-name>
|
|
|
```
|
|
|
|
|
|
通过这些步骤,你可以使用国内镜像源加快软件包的下载速度。
|
|
|
|
|
|
|
|
|
### PYTHON 虚拟环境
|
|
|
|
|
|
|
|
|
您遇到了一个与环境管理相关的错误信息,这是因为您的系统被配置为限制在系统范围内安装 Python 包。为了避免破坏系统配置,建议使用虚拟环境来安装和管理 Python 包。以下是具体步骤:
|
|
|
|
|
|
1. **创建虚拟环境**:
|
|
|
|
|
|
在您的项目目录下创建一个虚拟环境:
|
|
|
|
|
|
```shell
|
|
|
python3 -m venv venv
|
|
|
```
|
|
|
|
|
|
2. **激活虚拟环境**:
|
|
|
|
|
|
激活刚刚创建的虚拟环境:
|
|
|
|
|
|
```shell
|
|
|
source venv/bin/activate
|
|
|
```
|
|
|
|
|
|
激活后,您会看到命令提示符前面多了 `(venv)`,表示您已经在虚拟环境中。
|
|
|
|
|
|
3. **安装 paho-mqtt**:
|
|
|
|
|
|
在激活的虚拟环境中,使用 `pip` 安装 `paho-mqtt` 库:
|
|
|
|
|
|
```shell
|
|
|
pip install paho-mqtt
|
|
|
```
|
|
|
|
|
|
4. **验证安装**:
|
|
|
|
|
|
您可以通过运行一个简单的 Python 脚本来验证是否成功安装了 `paho-mqtt`:
|
|
|
|
|
|
```python
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
|
|
def on_connect(client, userdata, flags, rc):
|
|
|
print("Connected with result code " + str(rc))
|
|
|
|
|
|
client = mqtt.Client()
|
|
|
client.on_connect = on_connect
|
|
|
|
|
|
client.connect("mqtt.eclipse.org", 1883, 60)
|
|
|
|
|
|
client.loop_forever()
|
|
|
```
|
|
|
|
|
|
将以上代码保存为一个 Python 文件(例如 `mqtt_test.py`),然后运行它:
|
|
|
|
|
|
```shell
|
|
|
python mqtt_test.py
|
|
|
```
|
|
|
|
|
|
如果成功连接到 MQTT 服务器,会看到输出类似于:
|
|
|
|
|
|
```shell
|
|
|
Connected with result code 0
|
|
|
```
|
|
|
|
|
|
这样,您就可以在虚拟环境中安装和使用 `paho-mqtt` 库,而不会影响系统的其他部分。
|
|
|
|
|
|
|
|
|
|
|
|
### redis 安装
|
|
|
|
|
|
在您的 Raspberry Pi 上安装 Redis,可以使用以下步骤:
|
|
|
|
|
|
1. **更新系统包列表**:
|
|
|
|
|
|
首先,更新您的系统包列表:
|
|
|
|
|
|
```shell
|
|
|
sudo apt-get update
|
|
|
```
|
|
|
|
|
|
2. **安装 Redis**:
|
|
|
|
|
|
使用 `apt-get` 安装 Redis:
|
|
|
|
|
|
```shell
|
|
|
sudo apt-get install redis-server
|
|
|
```
|
|
|
|
|
|
3. **验证安装**:
|
|
|
|
|
|
安装完成后,您可以通过以下命令来检查 Redis 服务是否正在运行:
|
|
|
|
|
|
```shell
|
|
|
sudo systemctl status redis
|
|
|
```
|
|
|
|
|
|
如果 Redis 正在运行,您会看到类似以下的输出:
|
|
|
|
|
|
```shell
|
|
|
● redis-server.service - Advanced key-value store
|
|
|
Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
|
|
|
Active: active (running) since [date and time]
|
|
|
Docs: http://redis.io/documentation,
|
|
|
man:redis-server(1)
|
|
|
```
|
|
|
|
|
|
4. **测试 Redis**:
|
|
|
|
|
|
您可以通过 `redis-cli` 命令行工具来测试 Redis:
|
|
|
|
|
|
```shell
|
|
|
redis-cli
|
|
|
```
|
|
|
|
|
|
在 Redis 命令行中,输入以下命令进行测试:
|
|
|
|
|
|
```shell
|
|
|
set test "Hello, Redis!"
|
|
|
get test
|
|
|
```
|
|
|
|
|
|
如果安装成功,您会看到:
|
|
|
|
|
|
```shell
|
|
|
"Hello, Redis!"
|
|
|
```
|
|
|
|
|
|
5. **设置 Redis 开机自启动**:
|
|
|
|
|
|
Redis 通常默认设置为开机自启动,如果没有,可以使用以下命令设置:
|
|
|
|
|
|
```shell
|
|
|
sudo systemctl enable redis
|
|
|
```
|
|
|
|
|
|
这将确保 Redis 在系统启动时自动运行。
|
|
|
|
|
|
通过这些步骤,您应该能够在您的 Raspberry Pi 上成功安装并运行 Redis。 |