最近玩linux,不少东西用Python
可系统自带的2.6.6版本太低.所以需要手动升级到Python3并且共存Python2( Python2如果不存在将会导致yum无法使用)

yum安装

  • CentOS 7
# 安装EPEL
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# 安装Python3
yum install python36 -y
# 配置Python3软链接
ln -s /usr/bin/python3.6 /usr/bin/python3
# 安装pip3
wget https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py
  • CentOS 6
# 安装EPEL
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
# 安装Python3和Redis
yum install python34 -y
# 安装pip3
wget https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py
  • Debian/Ubuntu
apt update
apt install python3-pip -y

编译安装

编译在CentOS7上操作了

安装需要的软件

yum groupinstall -y "Development tools"
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel

下载解压Python3

cd /root
wget https://www.python.org/ftp/python/3.5.7/Python-3.5.7.tar.xz
tar xf Python-3.5.7.tar.xz
cd Python-3.5.7
mkdir /usr/local/python3

编译与安装Python3

./configure --prefix=/usr/local/python3
make && make install

创建Python3的软链接

ln -s /usr/local/python3/bin/python3 /usr/bin/python3

创建Pip3的软链接

ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

检查版本

python -V
python3 -V
pip3 -V

可能需要的操作

pip3改为国内源

# 如果没有.pip目录就自己创建一个 mkdir /root/.pip

cat > /root/.pip/pip.conf << EOF
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF

改为国内DNS

rm -rf /etc/resolv.conf && echo -e "nameserver 114.114.114.114\nnameserver 8.8.8.8" >> /etc/resolv.conf