我写了一份关于如何在 CentOS 6 和 CentOS 7 上安装最新版本的 Python 2 和 Python 3的快速指南。它目前涵盖 Python 2.7.13 和 Python 3.6.0:
# Start by making sure your system is up-to-date:
yum update
# Compilers and related tools:
yum groupinstall -y "development tools"
# Libraries needed during compilation to enable all features of Python:
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel expat-devel
# If you are on a clean "minimal" install of CentOS you also need the wget tool:
yum install -y wget
后续步骤取决于您安装的 Python 版本。
对于 Python 2.7.14:
wget http://python.org/ftp/python/2.7.14/Python-2.7.14.tar.xz
tar xf Python-2.7.14.tar.xz
cd Python-2.7.14
./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall
# Strip the Python 2.7 binary:
strip /usr/local/lib/libpython2.7.so.1.0
对于 Python 3.6.3:
wget http://python.org/ftp/python/3.6.3/Python-3.6.3.tar.xz
tar xf Python-3.6.3.tar.xz
cd Python-3.6.3
./configure --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall
# Strip the Python 3.6 binary:
strip /usr/local/lib/libpython3.6m.so.1.0
要安装 Pip:
# First get the script:
wget https://bootstrap.pypa.io/get-pip.py
# Then execute it using Python 2.7 and/or Python 3.6:
python2.7 get-pip.py
python3.6 get-pip.py
# With pip installed you can now do things like this:
pip2.7 install [packagename]
pip2.7 install --upgrade [packagename]
pip2.7 uninstall [packagename]
您不应该更改 Python 的系统版本,因为它会破坏系统(如您所见)。只要您不理会原始系统版本,安装其他版本就可以正常工作。这可以通过/usr/local
在运行 configure 时使用自定义前缀(例如 )来完成,并在安装 Python 构建时使用make altinstall
(而不是正常的)来完成。make install
只要您记得输入包含版本号的全名(例如“python2.7”或“pip2.7”),拥有多个可用的 Python 版本通常不是什么大问题。如果您从 virtualenv 完成所有 Python 工作,则版本控制会为您处理,因此请确保您安装并使用 virtualenv!