ubuntu18.04(centos)安装多版本python及管理
ubuntu18.04安装3个python版本,系统自带python3.6.9和2.7.15,增加python3.6.8,默认python3.6.8
一、旧版本信息
root@iZbp13uqehzec6flqx5m23Z:/# python2 --version
Python 2.7.17
root@iZbp13uqehzec6flqx5m23Z:/# python --version
Python 3.6.9
二、注意事项
切记不可随意删除系统自带python版本,因为系统有很多软件依赖
三、安装办法
ubuntu官方apt工具包安装
PPA(Personal Package Archive)的apt工具包安装
手动下载并编译python源代码安装
3.1源码安装教程
3.1.1.下载安装包
下载python3编译的依赖包
yum install -y gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
cd /usr/local/src
#华为镜像 https://mirrors.huaweicloud.com/python
wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
3.1.2.解压安装包
tar -xvf Python-3.6.8.tgz
3.1.3安装指定版本python
3.1.3.1.预配置
mkdir =/usr/local/python3.6.8
./configure --prefix=/usr/local/python3.6.8
3.1.3.1.编译安装
make&&make install
3.1.3.2.预配置
编译执行./configure --prefix=/usr/local/python3.6.8,则可执行文件放在/usr/local/python3.6.8/bin,库文件放在/usr/local/python3.6.8/lib,配置文件放在/usr/local/python3.6.8/include,其它的资源文件放在/usr /local/python3.6.8/share
3.1.4.删除就版本python默认软链指向
1.查看python现有信息
root@iZbp13uqehzec6flqx5m23Z:~# ls /usr/bin/python* -lh
lrwxrwxrwx 1 root root 24 Oct 31 14:42 /usr/bin/python -> /etc/alternatives/python
lrwxrwxrwx 1 root root 9 Apr 16 2018 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 3.5M Jul 1 23:56 /usr/bin/python2.7
lrwxrwxrwx 1 root root 9 Aug 24 15:53 /usr/bin/python3 -> python3.6
-rwxr-xr-x 2 root root 4.4M Jun 29 19:45 /usr/bin/python3.6
lrwxrwxrwx 1 root root 33 Jun 29 19:45 /usr/bin/python3.6-config -> x86_64-linux-gnu-python3.6-config
-rwxr-xr-x 2 root root 4.4M Jun 29 19:45 /usr/bin/python3.6m
lrwxrwxrwx 1 root root 34 Jun 29 19:45 /usr/bin/python3.6m-config -> x86_64-linux-gnu-python3.6m-config
lrwxrwxrwx 1 root root 16 Oct 25 2018 /usr/bin/python3-config -> python3.6-config
lrwxrwxrwx 1 root root 10 Aug 24 15:53 /usr/bin/python3m -> python3.6m
lrwxrwxrwx 1 root root 17 Oct 25 2018 /usr/bin/python3m-config -> python3.6m-config
2.重命名旧的python软链
mv /usr/bin/python /usr/bin/python.bak
mv /usr/bin/pip /usr/bin/pip.bak
3.建立python新软链和pip软链
ln -s /usr/local/python3.6.8/bin/python3 /usr/bin/python
ln -s /usr/local/python3.6.8/bin/pip3 /usr/bin/pip
如果是系统自带的3.6.8
ln -s /usr/bin/python3 /usr/bin/python
4.测试版本
root@iZbp13uqehzec6flqx5m23Z:/# python
Python 3.6.8 (default, Oct 31 2022, 14:34:18)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
3.1.5.指定默认python版本
如上步操作所示,已经制定python3.6.8版本是默认python版本
3.1.6.使用update-alternatives 来为整个系统更改Python版本
我们可以使用 update-alternatives 来为整个系统更改Python 版本。以 root 身份登录,首先罗列出所有可用的python 替代版本信息:
#update-alternatives --list python
update-alternatives: error: no alternatives for python
如果出现以上所示的错误信息,则表示 Python 的替代版本尚未被update-alternatives 命令识别。我们需要更新一下替代列表,将python2.7 和 python3.6放入其中。
root@iZbp13uqehzec6flqx5m23Z:/usr/bin# update-alternatives --install /usr/bin/python python /usr/bin/python2 100
update-alternatives: using /usr/bin/python2 to provide /usr/bin/python (python) in auto mode
root@iZbp13uqehzec6flqx5m23Z:/usr/bin# update-alternatives --install /usr/bin/python python /usr/bin/python3 110
update-alternatives: using /usr/bin/python3 to provide /usr/bin/python (python) in auto mode
root@iZbp13uqehzec6flqx5m23Z:/usr/bin# update-alternatives --install /usr/bin/python python /usr/local/python3.6.8/bin/python3 150
update-alternatives: using /usr/local/python3.6.8/bin/python3 to provide /usr/bin/python (python) in auto mode
-install 选项使用了多个参数用于创建符号链接。最后一个参数指定了此选项的优先级,如果我们没有手动来设置替代选项,那么具有最高优先级的选项就会被选中。
root@iZbp13uqehzec6flqx5m23Z:/usr/bin# sudo update-alternatives --config python
There are 3 choices for the alternative python (providing /usr/bin/python).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/local/python3.6.8/bin/python3 150 auto mode
1 /usr/bin/python2 100 manual mode
2 /usr/bin/python3 110 manual mode
3 /usr/local/python3.6.8/bin/python3 150 manual mode