Python 3.12 从源代码安装指南
设置系统ubuntu镜像加速
curl -L https://gitee.com/RubyMetric/chsrc/releases/download/pre/chsrc-x64-linux -o /usr/local/bin/chsrc; chmod +x /usr/local/bin/chsrc
chsrc set ubuntu huawei
一、更新系统
首先,确保你的系统是最新的,运行以下命令:
apt update -y
apt upgrade -y
二、安装所需依赖项
安装从源代码构建 Python 所需的必要软件包:
apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev \libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \xz-utils tk-dev libffi-dev liblzma-dev python3-openssl git
三、下载 Python 源代码
访问 Python 下载页面获取源代码。然后,使用 wget 下载源代码:
wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz
#阿里云镜像
wget https://mirrors.aliyun.com/python-release/source/Python-3.12.0.tar.xzwget https://mirrors.aliyun.com/python-release/sourcePython-wget https://mirrors.aliyun.com/python-release/source3.12.4.tar.xz 19.7 MB 2024-06-08 03:00
wget https://mirrors.aliyun.com/python-release/sourcePython-3.12.4.tgz 26.0 MB 2024-06-08 03:00
wget https://mirrors.aliyun.com/python-release/sourcePython-3.12.5.tar.xz 19.5 MB 2024-08-08 03:00
wget https://mirrors.aliyun.com/python-release/sourcePython-3.12.5.tgz 25.7 MB 2024-08-08 03:00
wget https://mirrors.aliyun.com/python-release/sourcePython-3.12.6.tar.xz 19.5 MB 2024-09-08 03:00
wget https://mirrors.aliyun.com/python-release/sourcePython-3.12.6.tgz 25.8 MB 2024-09-08 03:00
wget https://mirrors.aliyun.com/python-release/sourcePython-3.12.7.tar.xz 19.5 MB 2024-10-02 03:00
wget https://mirrors.aliyun.com/python-release/sourcePython-3.12.7.tgz
更多版本
https://mirrors.aliyun.com/python-release/source/查看
四、解压归档文件
tar -xf Python-3.12.0.tgz
五、配置和构建
- 进入解压后的目录:
cd Python-3.12.0
- 运行配置命令:
./configure --enable-optimizations
- 构建 Python:
make -j [你的 CPU 核心数]
六、安装 Python
使用 altinstall
而不是 install
以防止它替换系统默认的 Python 解释器(这可能会导致系统工具出现故障):
make altinstall
七、验证安装
检查 Python 3.12 是否已成功安装:
python3.12 --version
八、设置pip镜像加速
curl -L https://gitee.com/RubyMetric/chsrc/releases/download/pre/chsrc-x64-linux -o /usr/local/bin/chsrc; chmod +x /usr/local/bin/chsrc
chsrc set pip huawei