Step 1: Install Python Dependencies
登陆账户(root账户或者拥有 sudo 权限的账户)
$ ssh username@serveripaddress
更新系统
sudo yum -y install epel-release
sudo yum -y update
重启系统
sudo reboot
安装开发者工具
sudo yum groupinstall "Development Tools" -y
sudo yum install openssl-devel libffi-devel bzip2-devel -y
确认 gcc 可用
$ gcc --version
gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-4)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Step 2: Download latest Python 3.9 Archive
安装 wget
sudo yum install wget -y
使用 wget 下载 python3.9
wget https://www.python.org/ftp/python/3.9.10/Python-3.9.10.tgz
使用 tar 解压压缩包
tar xvf Python-3.9.10.tgz
进入到解压的文件夹内:
cd Python-3.9*/
Step 3: Install Python 3.9 on CentOS 8 / CentOS 7
配置 python 安装
./configure --enable-optimizations
上面命令可能产生的输出如下:
....
checking for the Linux getrandom() syscall... yes
checking for the getrandom() function... yes
checking for library containing shm_open... -lrt
checking for sys/mman.h... (cached) yes
checking for shm_open... yes
checking for shm_unlink... yes
checking for pkg-config... /usr/bin/pkg-config
checking whether compiling and linking against OpenSSL works... yes
checking for X509_VERIFY_PARAM_set1_host in libssl... yes
checking for --with-ssl-default-suites... python
checking for --with-builtin-hashlib-hashes... md5,sha1,sha256,sha512,sha3,blake2
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Misc/python.pc
config.status: creating Misc/python-embed.pc
config.status: creating Misc/python-config.sh
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup.local
creating Makefile
Build Python 3.9 on CentOS 8 / CentOS 7:
sudo make altinstall
耐心等待,如果成功的话,可能会产生如下信息:
....
running install_scripts
copying build/scripts-3.9/pydoc3.9 -> /usr/local/bin
copying build/scripts-3.9/idle3.9 -> /usr/local/bin
copying build/scripts-3.9/2to3-3.9 -> /usr/local/bin
changing mode of /usr/local/bin/pydoc3.9 to 755
changing mode of /usr/local/bin/idle3.9 to 755
changing mode of /usr/local/bin/2to3-3.9 to 755
rm /usr/local/lib/python3.9/lib-dynload/_sysconfigdata__linux_x86_64-linux-gnu.py
rm -r /usr/local/lib/python3.9/lib-dynload/__pycache__
/usr/bin/install -c -m 644 ./Misc/python.man \
/usr/local/share/man/man1/python3.9.10
if test "xupgrade" != "xno" ; then \
case upgrade in \
upgrade) ensurepip="--altinstall --upgrade" ;; \
install|*) ensurepip="--altinstall" ;; \
esac; \
./python -E -m ensurepip \
$ensurepip --root=/ ; \
fi
Looking in links: /tmp/tmpxqejw3c3
Processing /tmp/tmpxqejw3c3/setuptools-49.2.1-py3-none-any.whl
Processing /tmp/tmpxqejw3c3/pip-20.2.3-py2.py3-none-any.whl
Installing collected packages: setuptools, pip
WARNING: The script easy_install-3.9 is installed in '/usr/local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The script pip3.9 is installed in '/usr/local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-20.2.3 setuptools-49.2.1
Step 4: Check Python 3.9 installation on CentOS 8 / CentOS 7
检查是否成功安装
$ python3.9 --version
Python 3.9.10
Pip3.9 也会被安装:
$ pip3.9 --version
pip 21.2.4 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
升级 pip
$ /usr/local/bin/python3.9 -m pip install --upgrade pip
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pip in /usr/local/lib/python3.9/site-packages (21.2.4)
Collecting pip
Downloading pip-21.3.1-py3-none-any.whl (1.7 MB)
|████████████████████████████████| 1.7 MB 11.1 MB/s
Installing collected packages: pip
Successfully installed pip-21.3.1
这样安装第三方库
$ pip3.9 install virtualenv
查看安装的包的信息:
$ pip3.9 show virtualenv
Name: virtualenv
Version: 20.14.1
Summary: Virtual Python Environment builder
Home-page: https://virtualenv.pypa.io/
Author: Bernat Gabor
Author-email: gaborjbernat@gmail.com
License: MIT
Location: /usr/local/lib/python3.9/site-packages
Requires: distlib, filelock, platformdirs, six
Required-by: