CentOS 7 一键安装 Python 3.10.10

By Admin

Python

```bash #!/bin/bash PYTHON_VERSION=3.10.10 MIN_GCC_VER=8 if [[ $EUID -ne 0 ]]; then echo "This script must be run as root or with sudo." exit 1 fi # 检查GCC版本 GCC_VER=`gcc -dumpversion` if [ ${GCC_VER%%...

#!/bin/bash

PYTHON_VERSION=3.10.10
MIN_GCC_VER=8

if [[ $EUID -ne 0 ]]; then
  echo "This script must be run as root or with sudo." 
  exit 1
fi


# 检查GCC版本  
GCC_VER=`gcc -dumpversion`
if [ ${GCC_VER%%.*} -lt ${MIN_GCC_VER} ]; then
    OPTIMIZE_ARG=
else
    OPTIMIZE_ARG="--enable-optimizations"
fi

# 安装依赖包
yum groupinstall -y "Development tools"

yum install -y sqlite-devel ncurses-devel ncurses-libs bzip2-devel libffi-devel zlib-devel sqlite-devel mysql-devel openssl11-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel || { echo "Failed to install dependencies."; exit 1; }


# 下载Python源码
wget -O Python-${PYTHON_VERSION}.tgz https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz || { echo "Failed to download Python source code."; exit 3; }

# 解压缩
tar -zxvf Python-${PYTHON_VERSION}.tgz || { echo "Failed to decompress Python source code."; exit 4; }   

# 进入Python源码目录
cd Python-${PYTHON_VERSION} || { echo "Failed to enter Python source directory."; exit 5; }   

#This works for me on CentOS 7.9 with Python 3.10.9 based on https://bugs.python.org/issue47201
sed -i 's/PKG_CONFIG openssl /PKG_CONFIG openssl11 /g' configure

# 配置编译参数
./configure --prefix=/usr/local/python${PYTHON_VERSION}  ${OPTIMIZE_ARG} || { echo "Failed to configure Python."; exit 6; }

# 编译安装  
make && make altinstall || { echo "Failed to compile and install Python."; exit 7; }
ln -sf /usr/local/python${PYTHON_VERSION}/bin/python3.10 /usr/bin/python3.10  

# 安装pip${PYTHON_VERSION} 
#curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
#python${PYTHON_VERSION} get-pip.py || { echo "Failed to install pip${PYTHON_VERSION}."; exit 8; }

# 建立pip软链接
ln -s /usr/local/python${PYTHON_VERSION}/bin/pip3.10 /usr/bin/pip3.10 || { echo "Failed to create pip${PYTHON_VERSION} soft link."; exit 9; }

# 升级pip  
pip3.10 install --upgrade pip || { echo "Failed to upgrade pip."; exit 11; }

# 测试安装是否成功
python3.10 -V
pip3.10 -V  

# 完成安装  
echo "Python ${PYTHON_VERSION} installed successfully!"