2023-03-23
python
00

目录

1. 初始化依赖工具
2. 卸载自带python3
3. 安装openssl-v1.1.1
4. 安装python-v3.10.10
5. 修改默认python为3(非必须,不推荐)

一键脚本

bash
yum update -y && yum -y install gcc gcc-c++ bzip2 bzip2-devel xz-devel python-backports-lzma mesa-libGL zlib* libffi-devel openssl-devel && \
rpm -qa|grep python3|xargs rpm -ev --allmatches --nodeps && \
whereis python3 |xargs rm -frv && \
mkdir -p /data/openssl && cd /data/openssl && \
wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz && \
tar -zxvf openssl-1.1.1g.tar.gz && cd openssl-1.1.1g && \
./config --prefix=/usr/local/openssl no-zlib && make -j8 && make install -j8 && \
mv /usr/bin/openssl /usr/bin/openssl.bak && \
mv /usr/include/openssl/ /usr/include/openssl.bak && \
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/bin/openssl /usr/bin/openssl && \
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf && ldconfig -v && \
mkdir -p /data/python && cd /data/python && \
wget https://www.python.org/ftp/python/3.10.10/Python-3.10.10.tgz && \
tar -zxvf Python-3.10.10.tgz && cd Python-3.10.10 && \
export LANGUAGE=en_US.UTF-8 && export LANG=en_US.UTF-8 && export LC_ALL=en_US.UTF-8 && \
./configure --prefix=/usr/local/python3 CFLAGS=-fPIC --enable-shared --with-openssl=/usr/local/openssl && make -j8 && make install -j8 && \
ln -s /usr/local/python3/bin/python3.10 /usr/bin/python3 && \
ln -s /usr/local/python3/bin/pip3.10 /usr/bin/pip3 &&
touch /etc/ld.so.conf.d/python3.conf && \
echo "/usr/local/python3/lib" > /etc/ld.so.conf.d/python3.conf && \
ldconfig -v && python3 -V

1. 初始化依赖工具

bash
yum update -y && yum -y install gcc gcc-c++ bzip2 bzip2-devel xz-devel python-backports-lzma mesa-libGL zlib* libffi-devel openssl-devel

2. 卸载自带python3

bash
rpm -qa|grep python3|xargs rpm -ev --allmatches --nodeps && whereis python3 |xargs rm -frv

查看现有的已安装的python2:whereis python
查看现有的已安装的python3:whereis python3


3. 安装openssl-v1.1.1

bash
mkdir -p /data/openssl && cd /data/openssl && wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz

解压

bash
tar -zxvf openssl-1.1.1g.tar.gz && cd openssl-1.1.1g

编译安装

bash
mkdir /usr/local/openssl && ./config --prefix=/usr/local/openssl no-zlib && make -j8 && make install -j8

备份

bash
mv /usr/bin/openssl /usr/bin/openssl.bak && mv /usr/include/openssl/ /usr/include/openssl.bak

配置软链

bash
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/bin/openssl /usr/bin/openssl

修改配置并验证版本

bash
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf && ldconfig -v && openssl version

环境变量

bash
export LDFLAGS=" -L/usr/local/openssl/lib" && \
export CPPFLAGS=" -I/usr/local/openssl/include" && \
export PKG_CONFIG_PATH="/usr/local/openssl/lib/pkgconfig"

4. 安装python-v3.10.10

下载安装python-v3.10.10

bash
mkdir -p /data/python && cd /data/python && wget https://www.python.org/ftp/python/3.10.10/Python-3.10.10.tgz

解压

bash
tar -zxvf Python-3.10.10.tgz && cd Python-3.10.10

编译安装

bash
export LANGUAGE=en_US.UTF-8 && export LANG=en_US.UTF-8 && export LC_ALL=en_US.UTF-8 && \
./configure --prefix=/usr/local/python3 CFLAGS=-fPIC --enable-shared --with-openssl=/usr/local/openssl && make -j8 && make install -j8

设置软链

bash
ln -s /usr/local/python3/bin/python3.10 /usr/bin/python && ln -s /usr/local/python3/bin/pip3.10 /usr/bin/pip

配置依赖

bash
touch /etc/ld.so.conf.d/python3.conf && echo "/usr/local/python3/lib" > /etc/ld.so.conf.d/python3.conf && ldconfig

验证版本
python2:python -V
python3:python3 -V


5. 修改默认python为3(非必须,不推荐)

修改软链
备份原python

bash
mv /usr/bin/python /usr/bin/python.bak && mv /usr/bin/pip /usr/bin/pip.bak

设置软链

bash
ln -s /usr/local/python3/bin/python3.10 /usr/bin/python && ln -s /usr/local/python3/bin/pip3.10 /usr/bin/pip

配置依赖

bash
touch /etc/ld.so.conf.d/python3.conf && echo "/usr/local/python3/lib" > /etc/ld.so.conf.d/python3.conf && ldconfig

验证版本

bash
python -V

后续:恢复yum使用python2

bash
sed -i '1d' /usr/bin/yum && sed -i "1i#! /usr/bin/python2" /usr/bin/yum
bash
sed -i '1d' /usr/libexec/urlgrabber-ext-down && sed -i "1i#! /usr/bin/python2"  /usr/libexec/urlgrabber-ext-down
bash
sed -i '1d' /usr/bin/yum-config-manager && sed -i "1i#! /usr/bin/python2 -tt" /usr/bin/yum-config-manager

本文作者:954L

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!