Skip to content

CentOS编译安装Python3

下载Python源码包

shell
curl -O https://oss.odboy.cn/blog/files/Python-3.9.9.tar.xz
#curl -O https://oss.odboy.cn/blog/files/Python-3.11.10.tar.xz

更换阿里源、安装常用依赖

避免关键时刻命令不存在, 最好按顺序执行~

shell
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo_bak
curl http://mirrors.aliyun.com/repo/Centos-7.repo > /etc/yum.repos.d/CentOS-Base.repo
curl http://mirrors.aliyun.com/repo/epel-7.repo > /etc/yum.repos.d/epel.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/epel.repo
yum makecache fast
yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel netstat lrzsz unzip zip sysstat ntpdate net-tools -y
yum install epel-release -y

顺序执行命令

shell
# 切换到目录'/usr/local'
cd /usr/local
 
# 上传源码包'Python-3.9.9.tar.xz'到目录'/usr/local'下
rz
 
# 解压'Python-3.9.9.tar.xz'
tar -xvf Python-3.9.9.tar.xz
 
# 切换到目录'Python-3.9.9'
cd Python-3.9.9
 
# 配置、编译、安装(有点漫长, 稍等片刻~)
# ./configure --prefix=/usr/local/python3
# 如果pip3 install出现ssl异常, 那么就换成以下语句重新编译
./configure --with-ssl --prefix=/usr/local/python3
make && make install
 
# 退回上一层, 删除解压后的源码目录(防止误覆盖)
cd ..
rm -rf Python-3.9.9
 
# 配置环境变量
echo 'export PYTHON_HOME=/usr/local/python3' > /etc/profile.d/python3.sh
echo 'PATH=$PATH:$PYTHON_HOME/bin' >> /etc/profile.d/python3.sh

# 配置生效
source /etc/profile
 
# 检查python3安装情况
[root@localhost local]# python3 -V
Python 3.9.9

# 配置阿里源
mkdir ~/.pip
cat > ~/.pip/pip.conf <<EOF
[global]
index-url = https://mirrors.aliyun.com/pypi/simple
 
[install]
trusted-host = mirrors.aliyun.com
EOF