centos7 编译python3.11

youncyb 发布于 22 天前 90 次阅读 Python


本文档详细介绍了在CentOS 7环境下编译安装Python 3.11的完整流程,涵盖了从系统依赖环境的准备到编译、安装和配置的步骤

1. 安装了 SCL 仓库

cat <<EOF > /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo 
# CentOS-SCLo-rh.repo
#
# Please see http://wiki.centos.org/SpecialInterestGroup/SCLo for more
# information

[centos-sclo-rh]
name=CentOS-7 - SCLo rh
baseurl=https://vault.centos.org/7.6.1810/sclo/x86_64/rh/
# mirrorlist=http://mirrorlist.centos.org?arch=$basearch&release=7&repo=sclo-rh
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo

[centos-sclo-rh-testing]
name=CentOS-7 - SCLo rh Testing
baseurl=http://buildlogs.centos.org/centos/7/sclo/$basearch/rh/
gpgcheck=0
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo

[centos-sclo-rh-source]
name=CentOS-7 - SCLo rh Sources
baseurl=http://vault.centos.org/centos/7/sclo/Source/rh/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo

[centos-sclo-rh-debuginfo]
name=CentOS-7 - SCLo rh Debuginfo
baseurl=http://debuginfo.centos.org/centos/7/sclo/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
EOF
cat <<EOF > /etc/yum.repos.d/CentOS-SCLo-scl.repo
# CentOS-SCLo-sclo.repo
#
# Please see http://wiki.centos.org/SpecialInterestGroup/SCLo for more
# information

[centos-sclo-sclo]
name=CentOS-7 - SCLo sclo
baseurl=https://vault.centos.org/7.6.1810/sclo/x86_64/sclo/
# mirrorlist=http://mirrorlist.centos.org?arch=$basearch&release=7&repo=sclo-sclo
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo

[centos-sclo-sclo-testing]
name=CentOS-7 - SCLo sclo Testing
baseurl=http://buildlogs.centos.org/centos/7/sclo/$basearch/sclo/
gpgcheck=0
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo

[centos-sclo-sclo-source]
name=CentOS-7 - SCLo sclo Sources
baseurl=http://vault.centos.org/centos/7/sclo/Source/sclo/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo

[centos-sclo-sclo-debuginfo]
name=CentOS-7 - SCLo sclo Debuginfo
baseurl=http://debuginfo.centos.org/centos/7/sclo/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
EOF

2. 安装依赖和编译 python

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.back && \
curl -o /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-7-reg.repo && \
yum makecache && \
yum -y install epel-release

yum install devtoolset-8-gcc* -y && \
source /opt/rh/devtoolset-8/enable && \
mv /usr/bin/gcc /usr/bin/gcc-4.8.5 && \
ln -s /opt/rh/devtoolset-8/root/bin/gcc /usr/bin/gcc && \
mv /usr/bin/g++ /usr/bin/g++-4.8.5 && \
ln -s /opt/rh/devtoolset-8/root/bin/g++ /usr/bin/g++



yum -y install wget xz tar gcc make tk-devel libffi-devel   sqlite-devel zlib-devel readline-devel openssl-devel curl-devel tk-devel gdbm-devel  xz-devel  bzip2-devel && \
version=3.11.5 && \
name="Python-${version}.tgz" && \
path="Python-${version}" && \
url="https://registry.npmmirror.com/-/binary/python/${version}/Python-${version}.tgz" && \
curl -o $name $url -L && \
tar -xzvf $name && \
cd $path && \
yum install -y epel-release && \
yum install -y openssl11 openssl11-devel && \
sed -i 's/PKG_CONFIG openssl /PKG_CONFIG openssl11 /g' configure && \
./configure --prefix=/usr/local/python"${version}"  --enable-optimizations --with-ensurepip=install && \
make -j "$(nproc)" && make install && \
ln -sf /usr/local/python"${version}"/bin/python3 /usr/local/bin/python311 && \
cd ../ && rm -rf Python3.11.5* && \
echo 'PATH=/usr/local/python3.11.5/bin:$PATH' >> ~/.bashrc && \
source ~/.bashrc

问题

如果在安装了 scl 包后,依然无法安装 devtoolset-8,那么查看/etc/yum.repo.d/目录后,发现没有 Centos-SCLo-scl-rh 包,可以手动添加:

cat <<EOF > /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo 
# CentOS-SCLo-rh.repo
#
# Please see http://wiki.centos.org/SpecialInterestGroup/SCLo for more
# information

[centos-sclo-rh]
name=CentOS-7 - SCLo rh
#baseurl=http://mirror.centos.org/centos/7/sclo/$basearch/rh/
mirrorlist=http://mirrorlist.centos.org?arch=$basearch&release=7&repo=sclo-rh
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo

[centos-sclo-rh-testing]
name=CentOS-7 - SCLo rh Testing
baseurl=http://buildlogs.centos.org/centos/7/sclo/$basearch/rh/
gpgcheck=0
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo

[centos-sclo-rh-source]
name=CentOS-7 - SCLo rh Sources
baseurl=http://vault.centos.org/centos/7/sclo/Source/rh/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo

[centos-sclo-rh-debuginfo]
name=CentOS-7 - SCLo rh Debuginfo
baseurl=http://debuginfo.centos.org/centos/7/sclo/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo
EOF

然后下载 yum 的缓存:

yum makecache