保留服务器原Python安装版本,安装指定需求版本Python
1.查看当前系统版本
[root@iZbp1ac4pv22mg092qi2zfZ ~]# cat /etc/system-release
CentOS Linux release 7.9.2009 (Core)
2.查看已安装Python路径及版本
[root@iZbp1ac4pv22mg092qi2zfZ ~]# which python
/usr/bin/python
[root@iZbp1ac4pv22mg092qi2zfZ ~]# which python3
/usr/bin/python3
[root@iZbp1ac4pv22mg092qi2zfZ ~]# python -V
Python 2.7.5
[root@iZbp1ac4pv22mg092qi2zfZ ~]# python3 -V
Python 3.6.8
3.下载需要想Python源码包
[root@iZbp1ac4pv22mg092qi2zfZ workspace_python]# wget https://www.python.org/ftp/python/3.8.6/Python-3.8.6.tgz
4.解压
[root@iZbp1ac4pv22mg092qi2zfZ workspace_python]# tar -xzvf Python-3.8.6.tgz
5.安装
5.1安装编译可能需要的库或者依赖
[root@iZbp1ac4pv22mg092qi2zfZ workspace_python]# cd Python-3.8.6/
[root@iZbp1ac4pv22mg092qi2zfZ workspace_python]# sudo install zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel
5.2创建安装路径,编译安装
[root@iZbp1ac4pv22mg092qi2zfZ Python-3.8.6]# mkdir -p /opt/python3.8
[root@iZbp1ac4pv22mg092qi2zfZ Python-3.8.6]# ./configure --prefix=/opt/python3.8
[root@iZbp1ac4pv22mg092qi2zfZ Python-3.8.6]# make && make install
5.3安装完成提示
Installing collected packages: setuptools, pip
WARNING: The script easy_install-3.8 is installed in '/opt/python3.8/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.8 are installed in '/opt/python3.8/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.1 setuptools-49.2.1
6.创建软连接
当前Python3是链接Python3.6的,改成新编译的Python3.8.6
[root@iZbp1ac4pv22mg092qi2zfZ Python-3.8.6]# which python3
/usr/bin/python3
[root@iZbp1ac4pv22mg092qi2zfZ Python-3.8.6]# ls -l /usr/bin/python3
lrwxrwxrwx 1 root root 9 Feb 8 2023 /usr/bin/python3 -> python3.6
[root@iZbp1ac4pv22mg092qi2zfZ Python-3.8.6]# ls -l /usr/bin/python3.6
-rwxr-xr-x 2 root root 11328 Nov 17 2020 /usr/bin/python3.6
[root@iZbp1ac4pv22mg092qi2zfZ Python-3.8.6]# rm /usr/bin/python3
rm: remove symbolic link ‘/usr/bin/python3’? yes
[root@iZbp1ac4pv22mg092qi2zfZ Python-3.8.6]# ln -s /opt/python3.8/bin/python3 /usr/bin/python3
同样设置链接一下pip
[root@iZbp1ac4pv22mg092qi2zfZ Python-3.8.6]# rm -rf /usr/bin/pip3
[root@iZbp1ac4pv22mg092qi2zfZ Python-3.8.6]# ln -s /opt/python3.8/bin/pip3 /usr/bin/pip3
7.测试
[root@iZbp1ac4pv22mg092qi2zfZ Python-3.8.6]# python3 -V
Python 3.8.6
[root@iZbp1ac4pv22mg092qi2zfZ Python-3.8.6]# pip3 -V
pip 20.2.1 from /opt/python3.8/lib/python3.8/site-packages/pip (python 3.8)
[root@iZbp1ac4pv22mg092qi2zfZ Python-3.8.6]# pip3 list
Package Version
---------- -------
pip 20.2.1
setuptools 49.2.1
WARNING: The repository located at mirrors.cloud.aliyuncs.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host mirrors.cloud.aliyuncs.com'.
有个WARNING,解决方法
pip.conf 加上
trusted-host=mirrors.cloud.aliyuncs.com
[root@iZbp1ac4pv22mg092qi2zfZ Python-3.8.6]# vim ~/.pip/pip.conf
[global]
index-url=http://mirrors.cloud.aliyuncs.com/pypi/simple/
trusted-host=mirrors.cloud.aliyuncs.com
[install]
再测试一下
[root@iZbp1ac4pv22mg092qi2zfZ Python-3.8.6]# pip3 list
Package Version
---------- -------
pip 20.2.1
setuptools 49.2.1
WARNING: You are using pip version 20.2.1; however, version 24.1.1 is available.
You should consider upgrading via the '/opt/python3.8/bin/python3.8 -m pip install --upgrade pip' command.
更新最新的pip版本
[root@iZbp1ac4pv22mg092qi2zfZ Python-3.8.6]# /opt/python3.8/bin/python3.8 -m pip install --upgrade pip
将pip使用清华源
[root@iZbp1ac4pv22mg092qi2zfZ Python-3.8.6]# pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
标签:bin,Python,iZbp1ac4pv22mg092qi2zfZ,源码,Linux,pip,root,3.8
From: https://www.cnblogs.com/xp1763/p/18284102