CentOS 79 源码安装 Python 3.14

完整步骤请看下面 ↓↓↓

安装环境

RedHat 79 + GCC 4.8.5

$ cat /etc/redhat-release
Red Hat Enterprise Linux Workstation release 7.9 (Maipo)

$ gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

直接安装 会报错

# 下载
$ wget https://www.python.org/ftp/python/3.14.3/Python-3.14.3.tar.xz

# 解压缩
$ tar -xf Python-3.14.3.tar.xz

# 进入安装目录
$ cd Python-3.14.3

# 配置安装目录
$ ./configure --prefix=/usr/local/python-3.14.3 --enable-optimizations

# 编译
$ make -j$(nproc)

报错如下:

...
The following modules are *disabled* in configure script:
_sqlite3

The necessary bits to build these optional modules were not found:
_bz2                  _ctypes               _ctypes_test
_curses               _curses_panel         _dbm
_gdbm                 _hashlib              _lzma
_ssl                  _tkinter              _uuid
_zstd                 readline              zlib
To find the necessary bits, look in configure.ac and config.log.

Could not build the ssl module!
Python requires a OpenSSL 1.1.1 or newer

Checked 114 modules (36 built-in, 61 shared, 1 n/a on linux-x86_64, 1 disabled, 15 missing, 0 failed on import)

...

./_bootstrap_python ./Programs/_freeze_module.py _collections_abc ./Lib/_collections_abc.py Python/frozen_modules/_collections_abc.h
make[1]: *** [Python/frozen_modules/_collections_abc.h] Segmentation fault (core dumped)
make[1]: *** Waiting for unfinished jobs....
make[1]: *** [Python/frozen_modules/io.h] Segmentation fault (core dumped)
make[1]: *** [Python/frozen_modules/codecs.h] Segmentation fault (core dumped)
make[1]: *** [Python/frozen_modules/abc.h] Segmentation fault (core dumped)
make[1]: Leaving directory `/root/soft/Python-3.14.3'
make: *** [profile-opt] Error 2

解决 Segmentation fault (core dumped) 问题

gcc 版本太低导致,版本推荐安装 devtoolset-9 开发包:
传送门:配置 yum repo 源

$ yum install -y devtoolset-9
启用方式(二选一)
  1. 启用 gcc 9.x 环境
$ scl enable devtoolset-9 bash
  1. 修改 configure 参数
$ ./configure ... \
    CC=/opt/rh/devtoolset-9/root/usr/bin/gcc \
    CXX=/opt/rh/devtoolset-9/root/usr/bin/c++

解决依赖

官方说明:3.1.1. 针对可选模块的要求

缺少库名称安装命令
_bz2yum install bzip2-devel
_ctypes / _ctypes_testyum install libffi-devel
_curses / _curses_panelyum install ncurses-libs
_dbm / _gdbmyum install gdbm-devel
_hashlib / _ssl-
_lzmayum install xz-devel
_tkinter-
_uuidyum install libuuid-devel
_zstdyum install libzstd-devel
readlineyum install readline-devel
_zlibyum install zlib-devel

一键安装命令:

$ yum install -y bzip2-devel libffi-devel ncurses-libs gdbm-devel xz-devel libuuid-devel libzstd-devel readline-devel zlib-devel

安装 sqlite 3.15.2

执行 make 过程中提示 _sqlite3 not found,需要安装 >= 3.15.2 的版本。

# 下载
$ wget https://sqlite.org/2016/sqlite-autoconf-3150200.tar.gz

# 解压缩
$ tar -zxf sqlite-autoconf-3150200.tar.gz

# 进入安装目录
$ cd sqlite-autoconf-3150200

# 配置安装目录
$ ./configure --prefix=/usr/local/sqlite-3.15.2

# 编译 & 安装
$ make && make install

# 配置动态链接库
# $ export LD_LIBRARY_PATH=/usr/local/sqlite-3.15.2/lib:$LD_LIBRARY_PATH

# 全局启用
$ echo "/usr/local/sqlite-3.15.2/lib" > /etc/ld.so.conf.d/sqlite-3.15.2-x86_64.conf

# 刷新动态链接库缓存
$ ldconfig
# 修改 configure 参数
$ ./configure ... \
    LDFLAGS="-L/usr/local/sqlite-3.15.2/lib" \
    CPPFLAGS="-I/usr/local/sqlite-3.15.2/include"

安装 OpenSSL 1.1.1w

解决缺少 _hashlib / _ssl 依赖库问题。
Python3 需要引用 openssl 模块,但是 CentOS 79 默认为 1.0.2k,所以需要手动更新 openssl。

# 下载
$ wget http://www.openssl.org/source/openssl-1.1.1w.tar.gz

# 解压缩
$ tar -zxf openssl-1.1.1w.tar.gz

# 进入安装目录
$ cd openssl-1.1.1w

# 配置安装目录,自定义
$ ./config --prefix=/usr/local/openssl-1.1.1 shared zlib

# 编译 & 安装
$ make && make install

请注意:

  1. openssl 编译(config)的时候必须加上共享参数,否者源码安装Python即使添加了–with-openssl 的自定义路径,仍然会导致Could not build the ssl module! 报错!
  2. 亲测安装最新 3.5 LTS 版依然会报错,建议从 https://www.openssl.org/source/old/ 下载 1.1.1 的源码编译安装。
# 修改 configure 参数
$ ./configure ... \
    --with-openssl="/usr/local/openssl-1.1.1" \
    --with-openssl-rpath="/usr/local/openssl-1.1.1/lib"

_tkinter

未解决
具体请参考:Python 中 tkinter 源码安装使用与中文乱码 - Knowledge-Garden#12

安装完整步骤顺序

  1. yum 安装依赖库
# 安装 devtoolset-9
$ yum install -y devtoolset-9

# scl 启用 devtoolset-9
$ scl enable devtoolset-9 bash

# 安装依赖库
$ yum install -y bzip2-devel libffi-devel ncurses-libs gdbm-devel xz-devel libuuid-devel libzstd-devel readline-devel zlib-devel
  1. 安装 sqlite3
# 下载
$ wget https://sqlite.org/2016/sqlite-autoconf-3150200.tar.gz

# 解压缩
$ tar -zxf sqlite-autoconf-3150200.tar.gz

# 解压缩
$ cd sqlite-autoconf-3150200

# 配置安装目录
$ ./configure --prefix=/usr/local/sqlite-3.15.2

# 编译 & 安装
$ make && make install

# 全局启用
$ echo "/usr/local/sqlite-3.15.2/lib" > /etc/ld.so.conf.d/sqlite-3.15.2-x86_64.conf

# 刷新动态链接库缓存
$ ldconfig

# 退出 sqlite 安装目录
cd ..
  1. 安装 openssl
# 下载
$ wget http://www.openssl.org/source/openssl-1.1.1w.tar.gz

# 解压缩
$ tar -zxf openssl-1.1.1w.tar.gz

# 进入安装目录
$ cd openssl-1.1.1w

# 配置安装目录,自定义
$ ./config --prefix=/usr/local/openssl-1.1.1 shared zlib

# 编译 & 安装
$ make && make install

# 退出 openssl 安装目录
cd ..
  1. 安装 python3
# 下载
$ wget https://www.python.org/ftp/python/3.14.3/Python-3.14.3.tar.xz

# 解压缩
$ tar -xf Python-3.14.3.tar.xz

# 进入安装目录
$ cd Python-3.14.3

# 配置安装目录
$ ./configure \
    --enable-optimizations \
    --prefix=/usr/local/python-3.14.3 \
    --with-openssl=/usr/local/openssl-1.1.1 \
    --with-openssl-rpath=/usr/local/openssl-1.1.1/lib \
    LDFLAGS="-L/usr/local/sqlite-3.15.2/lib" \
    CPPFLAGS="-I/usr/local/sqlite-3.15.2/include"

# 编译
$ make -j$(nproc)

# 安装
$ make install
  1. 测试
# 测试安装成功
$ /usr/local/python-3.14.3/bin/python3 -V
Python 3.14.3

# 测试 openssl
$ /usr/local/python-3.14.3/bin/python3 -c "import ssl; print(ssl.OPENSSL_VERSION)"
OpenSSL 1.1.1w  11 Sep 2023

# 测试 sqlite3
$ /usr/local/python-3.14.3/bin/python3 -c "import sqlite3; print(sqlite3.sqlite_version)"
3.15.2

参考资料

Python3 源码安装:https://github.com/shenweiyan/Digital-Garden/discussions/13

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值