完整步骤请看下面 ↓↓↓
安装环境
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
启用方式(二选一)
- 启用 gcc 9.x 环境
$ scl enable devtoolset-9 bash
- 修改 configure 参数
$ ./configure ... \
CC=/opt/rh/devtoolset-9/root/usr/bin/gcc \
CXX=/opt/rh/devtoolset-9/root/usr/bin/c++
解决依赖
官方说明:3.1.1. 针对可选模块的要求
| 缺少库名称 | 安装命令 |
|---|---|
| _bz2 | yum install bzip2-devel |
| _ctypes / _ctypes_test | yum install libffi-devel |
| _curses / _curses_panel | yum install ncurses-libs |
| _dbm / _gdbm | yum install gdbm-devel |
| _hashlib / _ssl | - |
| _lzma | yum install xz-devel |
| _tkinter | - |
| _uuid | yum install libuuid-devel |
| _zstd | yum install libzstd-devel |
| readline | yum install readline-devel |
| _zlib | yum 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
请注意:
- openssl 编译(config)的时候必须加上共享参数,否者源码安装Python即使添加了–with-openssl 的自定义路径,仍然会导致Could not build the ssl module! 报错!
- 亲测安装最新 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
安装完整步骤顺序
- 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
- 安装 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 ..
- 安装 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 ..
- 安装 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
- 测试
# 测试安装成功
$ /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


640

被折叠的 条评论
为什么被折叠?



