环境初始化

基于CentOS7.9联网环境下编译安装python3.12稳定版本

配置本地离线源:虚拟机环境,且挂载了镜像

mount /dev/sr0 /mnt/
mv /etc/yum.repos.d/* /home/
cat > /etc/yum.repos.d/c.repo <<eof
[c]
name=c
baseurl=file:///mnt
gpgcheck=0
enabled=1
eof
yum clean all;yum repolist
  • 如果没有挂载,可以使用阿里源
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

安装编译所需依赖

yum groupinstall "Development Tools" -y
yum install -y \
    gcc \
    zlib-devel \
    ncurses-devel \
    gdbm-devel \
    openssl-devel \
    sqlite-devel \
    readline-devel \
    tk-devel \
    libffi-devel \
    bzip2-devel \
    xz-devel \
    libcurl-devel \
    db4-devel \
    perl-IPC-Cmd \
    wget 

编译安装

在CentOS 7上编译安装 Python 3.12 时,由于系统自带的 OpenSSL 版本较低,可能会导致 Python 无法使用最新的 SSL/TLS 功能。为了解决这个问题,需要先安装一个较新版本的 OpenSSL
官网

wget https://github.com/openssl/openssl/releases/download/openssl-3.4.1/openssl-3.4.1.tar.gz
tar -xf openssl-3.4.1.tar.gz
cd openssl-3.4.1
  • 开始编译安装
./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl shared
[root@localhost openssl-3.4.1]# ./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl shared
Configuring OpenSSL version 3.4.1 for target linux-x86_64
Using os-specific seed configuration
Created configdata.pm
Running configdata.pm
Created Makefile.in
Created Makefile
Created include/openssl/configuration.h

**********************************************************************
***                                                                ***
***   OpenSSL has been successfully configured                     ***
***                                                                ***
***   If you encounter a problem while building, please open an    ***
***   issue on GitHub <https://github.com/openssl/openssl/issues>  ***
***   and include the output from the following command:           ***
***                                                                ***
***       perl configdata.pm --dump                                ***
***                                                                ***
***   (If you are new to OpenSSL, you might want to consult the    ***
***   'Troubleshooting' section in the INSTALL.md file first)      ***
***                                                                ***
**********************************************************************
make ;make install
  • 设置环境变量
vi ~/.bashrc
export LDFLAGS="-L/usr/local/openssl/lib64 -Wl,-rpath,/usr/local/openssl/lib64"
export CPPFLAGS="-I/usr/local/openssl/include"
export LD_LIBRARY_PATH="/usr/local/openssl/lib64:$LD_LIBRARY_PATH"
export PKG_CONFIG_PATH="/usr/local/openssl/lib64/pkgconfig:$PKG_CONFIG_PATH"
source ~/.bashrc
  • 查看版本:如果需要重新编译,删除/usr/local/openssl文件,执行make clean即可
[root@localhost openssl-3.4.1]# /usr/local/openssl/bin/openssl version
OpenSSL 3.4.1 11 Feb 2025 (Library: OpenSSL 3.4.1 11 Feb 2025)

官网下载文档版本Python3.12:如果命令下载失败,去网页下载,然后上传

wget  https://www.python.org/ftp/python/3.12.9/Python-3.12.9.tar.xz
tar -xf Python-3.12.9.tar.xz
cd Python-3.12.9
  • 配置 Python 使用自定义 OpenSSL
./configure --prefix=/usr/local/python3.12 \
            --with-openssl=/usr/local/openssl \
            --with-openssl-rpath=auto
make;make install 
  • 创建软链接
ln -sf /usr/local/python3.12/bin/python3.12 /usr/bin/python3
ln -sf /usr/local/python3.12/bin/pip3.12 /usr/bin/pip3
  • 验证查看
[root@localhost Python-3.12.9]# python3 -c "import ssl; print(ssl.OPENSSL_VERSION)"
OpenSSL 3.4.1 11 Feb 2025

./configure 命令的常见选项说明:

  • –prefix=
    指定 Python 安装的目录。

  • –enable-optimizations
    启用优化,通常会增加编译时间,但能生成更高效的二进制文件。

  • –enable-shared
    构建共享库版本的 Python,适用于需要 Python 动态链接的环境。

  • –with-ensurepip=install
    安装 pip,Python 包管理工具。install 表示安装 pip,upgrade 表示升级已安装的 pip,none 表示不安装。

  • –with-ssl
    启用 OpenSSL 支持,允许 Python 使用 SSL 加密。

  • –with-dbmliborder=
    指定用于数据库支持的库,如 gdbm 或 dbm。

  • –with-readline
    启用 Readline 支持,提供命令行输入的历史记录和编辑功能。

  • –with-zlib
    启用对 zlib 压缩库的支持,通常用于对大型文件进行压缩和解压。

  • –with-lto
    启用链接时优化(LTO),提升代码性能,但增加编译时间。

  • –enable-unicode=ucs4 / --enable-unicode=ucs2
    设置 Python 中的 Unicode 字符串实现为 UCS4 或 UCS2,适用于不同的字符集大小。

  • –disable-ipv6
    禁用 IPv6 支持,减少不必要的网络功能。

  • –enable-loadable-sqlite-extensions
    启用 SQLite 可加载扩展支持。

  • –with-pymalloc
    启用 pymalloc 分配器,优化内存管理,通常可以提高性能。

  • –without-ensurepip
    禁用 pip 安装。

  • –without-threads
    禁用 Python 的线程支持。

  • –with-bz2
    启用对 bzip2 的支持。

  • –with-uuid
    启用 UUID 支持。

  • –with-xxhash
    启用 xxhash 支持,这是一种快速的哈希算法。

  • –disable-test-modules
    禁用 Python 的测试模块。

  • –enable-pydebug
    启用调试支持,增加调试信息,适用于开发和调试阶段。

  • –disable-shared
    禁用构建共享库版本的 Python。

  • –with-system-expat
    使用系统安装的 expat 库,而不是 Python 内部的 expat 实现。

  • –without-gcc
    禁用使用 GCC 编译 Python(如果使用其他编译器时可能会用到)。

配置pip国内源仓库

如果没有安装pip

  • 手动安装 pip
    下载 get-pip.py:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
  • 或使用浏览器下载:https://bootstrap.pypa.io/get-pip.py
    然后运行:
python get-pip.py
  1. 更新 pip
python -m pip install --upgrade pip

解决安装依赖失败问题,使用国内源阿里源

mkdir ~/.pip/
cat > ~/.pip/pip.conf <<EOF
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=mirrors.aliyun.com
EOF

配置清华源

  • pip临时使用
    注意,simple 不能少。
    pip 要求使用 https ,因此需要 https 而不是 http
pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple some-package
  • 设为默认
    升级 pip 到最新的版本后进行配置:
python -m pip install --upgrade pip
pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐