[root@izbp1cp2hwkqxg3s2faj1uz ~]# lsb_release -a LSB Version: :core-4.1-amd64:core-4.1-noarch Distributor ID: CentOS Description: CentOS Linux release 7.8.2003 (Core) Release: 7.8.2003 Codename: Core
可以看到我的CentOs核版本是7.8。
节省时间直接跳转方法二
为了保证所有环境是最新的状态,先整体更新一下:
yum update -y
yum install -y python3
yum
:CentOS7的包管理器[root@izbp1cp2hwkqxg3s2faj1uz ~]# python3 Python 3.6.8 (default, Apr 2 2020, 13:34:55) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux Type "help", "copyright", "credits" or "license" for more information.
缺点:yum虽然也能安装Python3,但是,不能安装最新版本的Python3.
好处是这种方法不用安装pip3,因为是自带的。
yum remove python3
检查是否卸载成功:
[root@izbp1cp2hwkqxg3s2faj1uz ~]# python3 -bash: /usr/bin/python3: No such file or directory
如果要用最新的python版本,则使用这种方法。
先安装一些依赖的包:
[root@centos7 ~]$ yum install gcc openssl-devel bzip2-devel libffi-devel -y
[root@centos7 ~]$ curl -O https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz
注:不同的版本修改版本号3.9.0
即可。
[root@centos7 ~]$ tar -zxvf Python-3.9.0.tgz
准备从源代码编译Python:
cd Python-3.9.0 && ./configure prefix=/usr/local/python3
最后使用以下命令完成安装:
make && make install
提示:
WARNING: The script easy_install-3.9 is installed in '/usr/local/python3/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 scripts pip3 and pip3.9 are installed in '/usr/local/python3/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.
提示我们安装的脚本不在路径中,下面创建链接。
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
验证安装是否成功:
[root@izbp1cp2hwkqxg3s2faj1uz ~]$ python3 --version Python 3.9.0 [root@izbp1cp2hwkqxg3s2faj1uz ~]$ pip3 --version pip 20.2.4 from /usr/local/python3/lib/python3.9/site-packages/pip (python 3.9) [root@izbp1cp2hwkqxg3s2faj1uz ~]$