Python3.11:源代码编译安装

警告
本文最后更新于 2022-11-22,文中内容可能已过时。

从源代码开始编译、安装 Python3.11,由于从这个版本之后,Python 采用了 SSL 的加密方式,需要依赖 openssl-1.1.1。同时,如果我们开启了 --enable-optimization 优化选项,还需要依赖 gcc9 以上版本才能支持,否则会一直出现报错。

安装依赖包

1
2
3
4
5
6
7
8
9
yum update -y && yum install -y \
        sudo vim git make cmake htop\
        gcc gcc-c++ kernel-devel \
        bzip2 mlocate sqlite-devel \
        zlib zlib-devel libffi-devel \
        openssl-devel libcurl-devel chrony \
        wget dmidecode net-tools openssh-server openssh-client perl-CPAN perl-IPC-Cmd && \
    yum clean all && \
    rm -rf /var/cache/yum/*

安装 glibc

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
cd /tmp && wget --no-check-certificate http://mirrors.ustc.edu.cn/gnu/libc/glibc-2.18.tar.gz && \
    tar -xvf glibc-2.18.tar.gz && \
    cd glibc-2.18 && \
    mkdir build && cd build && \
    ../configure --prefix=/usr \
        --disable-profile \
        --enable-add-ons \
        --with-headers=/usr/include \
        --with-binutils=/usr/bin && \
    make && make install && \
    rm -rf /tmp/glibc*

安装 gcc9

 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
cd /tmp && mkdir gcc9 && cd gcc9 && \
    wget --no-check-certificate https://ftp.gnu.org/gnu/gcc/gcc-9.2.0/gcc-9.2.0.tar.gz && \
    tar zxvf gcc-9.2.0.tar.gz && \
    cd gcc-9.2.0 && \
    wget --no-check-certificate ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.1.0.tar.bz2 && \
    wget --no-check-certificate ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-3.1.4.tar.bz2 && \
    wget --no-check-certificate ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.0.3.tar.gz && \
    wget --no-check-certificate ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-0.18.tar.bz2 && \
    tar jxvf gmp-6.1.0.tar.bz2 && \
    tar zxvf mpc-1.0.3.tar.gz && \
    tar jxvf mpfr-3.1.4.tar.bz2 && \
    tar jxvf isl-0.18.tar.bz2 && \
    ln -s gmp-6.1.0 gmp  && \
    ln -s mpfr-3.1.4 mpfr  && \
    ln -s mpc-1.0.3 mpc && \
    ln -s isl-0.18 isl && \
    sudo ./configure --prefix=/usr/local/gcc9 \
        --enable-bootstrap \
        --enable-checking=release \
        --enable-languages=c,c++ \
        --disable-multilib && \
    make && make install && \
    echo  "export PATH=/usr/local/gcc9/bin:$PATH" >> /etc/profile.d/gcc.sh && \
    source /etc/profile.d/gcc.sh && \
    ln -sv /usr/local/gcc9/include/ /usr/include/gcc && \
    echo "/usr/local/gcc9/lib64" >> /etc/ld.so.conf.d/gcc.conf && \
    ldconfig -v && \
    ldconfig -p |grep gcc && \
    ln -sf /usr/local/gcc9/bin/g++ /usr/bin/g++ && \
    ln -sf /usr/local/gcc9/bin/gcc /usr/bin/gcc && \
    ln -sf /usr/local/gcc9/bin/c++ /usr/bin/c++ && \
    ln -sf /usr/local/gcc9/bin/cc /usr/bin/cc && \
    rm -rf /tmp/gcc*

安装 openssl

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
cd /tmp && wget --no-check-certificate https://www.openssl.org/source/openssl-3.0.7.tar.gz && \
    tar -xvf openssl-3.0.7.tar.gz && \
    cd openssl-3.0.7 && \
    ./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl no-shared zlib-dynamic && \
    make && make install && \
    ln -s /usr/local/openssl/include/openssl /usr/include/openssl && \
    ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/local/lib64/libssl.so && \
    ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1 && \
    ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1 && \
    ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl && \
    echo "/usr/local/openssl/lib64" >> /etc/ld.so.conf && \
    ldconfig -v && \
    rm -rf /tmp/openssl*

这里需要注意:

  • 指定了安装路径:``–prefix=/usr/local/openssl,这个在安装 Python` 的时候使用

安装 Python 3.11

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
export PYTHON_VERSION=3.11.1
cd /tmp && \
    # wget --no-check-certificate https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz && \
    wget --no-check-certificate https://registry.npmmirror.com/-/binary/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz && \
    tar -xvf Python-${PYTHON_VERSION}.tar.xz && \
    cd Python-${PYTHON_VERSION} && \
    export LDFLAGS=-rdynamic && \
    CFLAGS="-I/usr/local/openssl/include" LDFLAGS="-L/usr/local/openssl/lib64" \
    ./configure \
        --enable-shared \
        --enable-optimizations \
        --enable-loadable-sqlite-extensions \
        --prefix=/usr/local/python3 \
        --with-openssl=/usr/local/openssl && \
    make -j && make install && \
    ln -s /usr/local/python3/bin/python3 /usr/bin/python3 &&\
    ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 && \
    ln -s /usr/local/python3/bin/pip3 /usr/bin/pip && \
    echo "export PATH=/usr/local/python3/bin:$PATH" >> /etc/profile.d/python3.sh && \
    echo "/usr/local/python3/lib" >> /etc/ld.so.conf && \
    ldconfig -v && \
    python3 -m ssl && \
    rm -rf /tmp/Python*

相关内容

william 支付宝支付宝
william 微信微信
0%