CentOS7编译安装Python3.12踩坑记录

  1. 编译安装openssl-3.2.1
  2. 编译安装libmpdec-4.0.0
  3. 编译安装Python-3.12.2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 安装新版gcc
# 使用CentOS7自带的gcc-4.8,如果添加--enable-optimizations则会编译失败
yum install centos-release-scl
yum install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash
# 安装构建工具链
yum install make -y

# 安装新版openssl,要求1.1.1以上。
# 此处安装3.2.1版本,https://www.openssl.org/source/openssl-3.2.1.tar.gz
./config --prefix=/opt/openssl-3.2.1 --openssldir=/opt/openssl-3.2.1
make -j4 # 开启并行编译,4线程,可根据自己的机器配置修改
make install
echo '/opt/openssl-3.2.1/lib64' > /etc/ld.so.conf.d/openssl-3.2.1.conf
ldconfig -v

# 安装新版libmpdec,要求2.5.0以上。
# 此处安装4.0.0版本,https://www.bytereef.org/software/mpdecimal/releases/mpdecimal-4.0.0.tar.gz
./configure --prefix=/opt/libmpdec-4.0.0
make -j4
make install
echo '/opt/libmpdec-4.0.0/lib' > /etc/ld.so.conf.d/libmpdec-4.0.0.conf
ldconfig -v

# 安装其它依赖
yum install -y epel-release
yum install -y gcc make openssl-devel zlib-devel bzip2-devel readline-devel sqlite-devel libffi-devel \
expat-devel libmpdec-devel mpdecimal-devel uuid-devel xz-devel tk-devel editline-devel

# 开始编译Python3.12
# 手动指定openssl和libmpdec的路径
export CFLAGS="-I/opt/libmpdec-4.0.0/include -I/opt/openssl-3.2.1/include"
export LDFLAGS="-L/opt/libmpdec-4.0.0/lib -L/opt/openssl-3.2.1/lib64"
export PKG_CONFIG_PATH="/opt/openssl-3.2.1/lib64/pkgconfig:/opt/libmpdec-4.0.0/lib/pkgconfig:/usr/lib64/pkgconfig:$PKG_CONFIG_PATH"
./configure \
--enable-loadable-sqlite-extensions \
--with-pkg-config=yes \
--prefix=/opt/python-3.12.2 \
--with-ensurepip=upgrade \
--enable-optimizations \
--with-lto=full \
--with-computed-gotos \
--enable-profiling \
--with-strict-overflow \
--enable-shared \
--with-system-expat \
--with-system-libmpdec \
--with-openssl=/opt/openssl-3.2.1 \
--with-openssl-rpath=auto
make -j4
make install
echo '/opt/python-3.12.2/lib' > /etc/ld.so.conf.d/python-3.12.2.conf
ldconfig -v

# 测试Python3.12,输出版本号,至此编译安装完成
/opt/python-3.12.2/bin/python3.12 --version