从源码安装 python 3.12 步骤
1. 获取 Python 源代码
从官方仓库下载稳定版或开发版的源代码:
方式 1:从官网下载
wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgztar -xzf Python-3.12.0.tgzcd Python-3.12.0
(或者将 3.12.0 替换为自己需要的版本号)
方式 2:从 Github 仓库克隆开发版
git clone https://github.com/python/cpython.gitcd cpythongit checkout v3.12.0 # 切换到指定分支(如此处 v3.12.0)
2. 安装依赖
编译 Python 需要一些开发工具和库:
# Debian/Ubuntu
sudo apt updatesudo apt install build-essential zlib1g-dev libncurses5-dev \\libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev \\libreadline-dev libffi-dev libbz2-dev liblzma-dev
# CentOS/RHEL
sudo yum groupinstall \"Development Tools\"sudo yum install zlib-devel bzip2-devel openssl-devel ncurses-devel \\sqlite-devel readline-devel libffi-devel xz-devel
3. 配置编译选项
运行 configure 脚本,指定安装路径(默认是 /usr/local):
./configure \\--enable-shared \\--enable-ipv6 \\--with-ensurepip=install \\--with-system-expat \\--with-system-ffi \\--with-system-libmpdec \\--enable-loadable-sqlite-extensions \\--with-dbmliborder=bdb:gdbm \\--with-computed-gotos \\--without-ensurepip \\--with-fpectl \\--with-address-sanitizer \\--prefix=/usr \\--enable-optimizations
稍微注视:
--enable-optimizations:启用优化(会延长编译时间)。
--enable-shared:生成共享库(.so 文件,供其他程序链接)。
--with-ensurepip=install:包含 pip 工具。
4. 编译和安装
make -j $(nproc) # 使用所有 CPU 核心加速编译sudo make install
安装后会生成文件:
解释器:/usr/local/bin/python3.x
共享库:/usr/local/lib/libpython3.x.so
验证安装
/usr/local/bin/python3 --version/usr/local/bin/pip3 --version
6. 可选配置
(1)设置默认 Python 版本
如果安装了多个 python,可以选择当前版本
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2sudo update-alternatives --config python3 # 选择新版本
(2)找不到共享库问题
如果运行时报错 libpython3.x.so.1.0: cannot open shared object file,执行:
echo \"/usr/local/lib\" | sudo tee /etc/ld.so.conf.d/python3.confsudo ldconfig
6. 卸载(或许用得着)
sudo rm -rf /usr/local/bin/python3.x \\ /usr/local/lib/python3.x \\ /usr/local/include/python3.xsudo ldconfig
7. 常见问题记录
可能会遇到的问题记录:
ModuleNotFoundError: No module named \'_ctypes\'
安装依赖时遗漏 libffi-dev,重新安装后重新编译。
make install 权限不足
确保使用 sudo 或指定用户可写的 --prefix(如 $HOME/.local)。
与系统 Python 冲突
建议通过 update-alternatives 管理多版本,而非直接替换系统 Python。
8. 获取官方预制信息
当你在 Ubuntu/Debian 系统上使用 apt-get install python3 安装 Python 时,安装的是 Debian/Ubuntu 官方维护的预编译版本。如果你想了解这些预编译版本是如何编译的,可以通过以下方法获取编译时的配置信息:
8.1. 查看已安装 Python 的编译参数
python3 -c \"import sysconfig; print(sysconfig.get_config_vars(\'CONFIG_ARGS\'))\"
或者更详细的查看:
python3 -m sysconfig
8.2. 查找 Debian 的编译配置
Debian/Ubuntu 的 Python 软件包在编译时会使用特定的编译选项。你可以:
查看软件包的构建信息
apt-get source python3
这会下载 Python 的源代码和 Debian 的打包文件,其中包含编译配置。或者直接查看构建日志
# 首先找到确切的包名apt-cache show python3 | grep Source# 然后下载构建日志(例如对于 Python 3.8)sudo apt-get install apt-utilsapt-get download python3.8dpkg -c python3.8*.deb # 查看包内容
8.3. 查找 Debian 的编译脚本
Debian 的 Python 软件包维护脚本通常位于:
# 安装 dpkg-dev 工具sudo apt-get install dpkg-dev# 下载 Python 的 debian 打包规则apt-get source python3.8cd python3.8-*ls debian/rules # 这是主要的编译配置脚本
8.4. 典型 Debian/Ubuntu Python 编译选项
Debian/Ubuntu 通常会使用以下主要编译选项(具体版本可能有所不同):
./configure \\--enable-shared \\--enable-ipv6 \\--with-ensurepip=install \\--with-system-expat \\--with-system-ffi \\--with-system-libmpdec \\--enable-loadable-sqlite-extensions \\--with-dbmliborder=bdb:gdbm \\--with-computed-gotos \\--without-ensurepip \\--with-fpectl \\--with-address-sanitizer \\--prefix=/usr \\--enable-optimizations
8.5. 如果想自己编译相同配置的 Python
首先安装构建依赖:
sudo apt-get build-dep python3
然后使用从上述方法获取的配置参数进行编译
注意事项
Debian/Ubuntu 的 Python 编译配置会根据版本和安全需求变化
官方预编译版本通常会启用更多安全相关的编译选项
自己编译时可能需要调整路径以适应系统