从源码构建、编译和安装Python 3.13
📅 2024-12-17 | 🖱️
🔖 python
构建需求 #
构建CPython所需的功能和最低版本要求如下:
- C11编译器。不需要支持C11的可选功能。
- OpenSSL 的最低版本为1.1.1,推荐最低版本为3.0.9,用于
ssl
和hashlib
扩展模块。 - SQLite的最低版本为3.15.2,用于
sqlite3
扩展模块。 - Tcl/Tk的最低版本为8.5.12,用于
tkinter
模块。 - 重新生成
configure
脚本需要Autoconf 2.71和aclocal 1.16.5。
准备工作 #
安装依赖和构建工具 #
1sudo apt-get update
2sudo apt-get upgrade
3sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev liblzma-dev tk-dev
1sudo dnf -y update
2sudo dnf -y groupinstall "Development Tools"
3sudo dnf -y install wget gcc openssl-devel bzip2-devel libffi-devel xz-devel tk-devel
1curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
2sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
3yum clean all
4yum makecache
5
6yum install centos-release-scl-rh
7sed -i 's#mirrorlist=http://mirrorlist.centos.org?arch=$basearch&release=7&repo=sclo-rh#\#mirrorlist=mirrorlist=http://mirrorlist.centos.org?arch=$basearch&release=7&repo=sclo-rh#g' /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo
8sed -i 's#\#baseurl=http://mirror.centos.org/centos/7/sclo/$basearch/rh/#baseurl=https://mirrors.aliyun.com/centos/7/sclo/$basearch/rh/#g' /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo
9yum clean all
10yum makecache
11yum install devtoolset-11-gcc devtoolset-11-gcc-c++ devtoolset-11-binutils
12scl enable devtoolset-11 bash
编译安装新版openssl,要求3.0.9版本以上。
1wget https://www.openssl.org/source/openssl-3.4.0.tar.gz
2
3yum install -y perl perl-core perl-CPAN perl-IPC-Cmd
4tar -zxvf openssl-3.4.0.tar.gz
5cd openssl-3.4.
6./config --prefix=/usr/local/openssl-3.4.0
7make -j4
8make install
9echo '/usr/local/openssl-3.4.0/lib64' > /etc/ld.so.conf.d/openssl-3.4.0.conf
10# 更新动态链接库缓存
11ldconfig -v
编译安装Python 3.13 #
1cd ~
2wget https://www.python.org/ftp/python/3.13.1/Python-3.13.1.tgz
3tar xzf Python-3.13.1.tgz
4cd Python-3.13.1
1sudo ./configure --prefix=/usr/local/python-3.13 --enable-optimizations --with-lto --with-computed-gotos --with-system-ffi --enable-shared
2sudo make -j "$(nproc)"
3sudo ./python3.13 -m test -j "$(nproc)"
4sudo make altinstall
1cd ~
2wget https://www.python.org/ftp/python/3.13.1/Python-3.13.1.tgz
3tar xzf Python-3.13.1.tgz
4cd Python-3.13.1
1sudo ./configure --prefix=/usr/local/python-3.13 --enable-optimizations --with-lto --with-computed-gotos --with-system-ffi --enable-shared
2sudo make -j "$(nproc)"
3sudo ./python3.13 -m test -j "$(nproc)"
4sudo make altinstall
1yum install -y \
2 openssl-devel \
3 bzip2-devel \
4 libffi-devel \
5 zlib-devel \
6 sqlite-devel \
7 readline-devel \
8 tk-devel \
9 gdbm-devel \
10 libpcap-devel \
11 xz-devel
12
13cd ~
14wget https://www.python.org/ftp/python/3.13.1/Python-3.13.1.tgz
15tar -xvzf Python-3.13.1.tgz
16cd Python-3.13.1
1export CFLAGS="-I/usr/local/openssl-3.4.0/include"
2export LDFLAGS="-L/usr/local/openssl-3.4.0/lib64"
3export PKG_CONFIG_PATH="/usr/local/openssl-3.4.0/lib64/pkgconfig:/usr/lib64/pkgconfig:$PKG_CONFIG_PATH"
4export LD_LIBRARY_PATH="/usr/local/openssl-3.4.0/lib64:$LD_LIBRARY_PATH"
5
6./configure --prefix=/usr/local/python-3.13 \
7 --enable-optimizations \
8 --enable-shared \
9 --with-ensurepip=upgrade \
10 --with-lto \
11 --with-computed-gotos \
12 --enable-loadable-sqlite-extensions \
13 --with-pkg-config=yes \
14 --with-openssl=/usr/local/openssl-3.4.0
15
16scl enable devtoolset-11 bash
17make -j4
18make altinstall
19
20echo '/usr/local/python-3.13/lib' > /etc/ld.so.conf.d/python3.13.conf
21# 更新动态链接库缓存
22ldconfig -v
验证安装 #
1/usr/local/python-3.13/bin/python3.13 --version
2Python 3.13.1
3
4/usr/local/python-3.13/bin/python3.13
5>>> import sys
6>>> print(sys.platform)
7linux
8>>> print(sys.implementation.name)
9cpython
10>>> print(sys.version_info)
11sys.version_info(major=3, minor=13, micro=1, releaselevel='final', serial=0)
12>>> print(sys.version)
133.13.1 (main) [GCC 11.2.1 20220127 (Red Hat 11.2.1-9)]