没志青年
发布于 2025-08-17 / 20 阅读
0

Ubuntu 升级 Python

24.04.3 自带的 为3.12.3 所以不用升级

ubuntu自带的python是系统要用的,千万不要删除,应该安装一个新的,并将它设置为默认的。

编译安装:

sudo apt-get install liblzma-dev

# 下载源码
wget https://www.python.org/ftp/python/3.11.9/Python-3.11.9.tgz
tar -xvf Python-3.11.9.tgz
cd Python-3.11.9

# 编译安装
./configure --enable-optimizations --with-lzma
make -j$(nproc)
sudo make altinstall   # ⚠️ 用 altinstall 避免覆盖系统 python3

设置默认:

# 把系统的 python3.6 和新编译的 python3.11 都加入 alternatives
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.11 2

# 选择默认版本
sudo update-alternatives --config python3

There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                       Priority   Status
------------------------------------------------------------
* 0            /usr/local/bin/python3.11   2         auto mode
  1            /usr/bin/python3.6          1         manual mode
  2            /usr/local/bin/python3.11   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 2

查询:

ls -l /usr/bin/python*
ls -l /usr/local/bin/python*

验证:

python3 --version

pip 下载超时问题,换个镜像源:

pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple