ubuntu18.04安装多版本python及管理

zhglinux 2020-06-09

20200609
部分引用https://www.jb51.net/article/163112.html

ubuntu18.04安装3个python版本,系统已自带python3.6.9和2.7.15,再增加python3.6.8,默认python3.6.8

一、旧版本信息

:/usr/local/bin# python2 --version
Python 2.7.15+
:/usr/local/bin# python --version
Python 3.6.9

二、注意事项

切记不可随意删除系统自带python版本,因为系统有很多软件依赖着

三、安装办法

ubuntu官方apt工具包安装
PPA(Personal Package Archive)的apt工具包安装
手动下载并编译python源代码安装

3.1源码安装教程

3.1.1.下载安装包

cd /usr/local/src
wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz

3.1.2.解压安装包

tax -xvf Python-3.6.8.tgz

3.1.3安装指定版本python

3.1.3.1.预配置

./configure(也可以./configure --prefix=/usr/local/python3.6.8)

3.1.3.1.编译安装

sudo make
sudo make install

3.1.3.2.预配置两种方法的区别

1.若编译执行的是./configure --prefix=/usr/local/python368,则可执行文件放在/usr/local/python368/bin,库文件放在/usr/local/python368/lib,配置文件放在/usr/local/python368/include,其它的资源文件放在/usr /local/python368/share
2.若编译执行的是./configure --prefix=/usr/local/python368,添加环境变量PATH=PATH:HOME/bin:/usr/local/python368/bin
查看环境变量
echo $PATH

3.1.4.删除就版本python默认软链指向

1.查看python现有信息
:/usr/bin# ls /usr/bin/python* -lh
lrwxrwxrwx 1 root root   24 Jun  9 14:45 /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 Apr 16 01:20 /usr/bin/python2.7
lrwxrwxrwx 1 root root    9 Oct 25  2018 /usr/bin/python3 -> python3.6
-rwxr-xr-x 2 root root 4.4M Apr 18 09:56 /usr/bin/python3.6
lrwxrwxrwx 1 root root   34 Jun  9 14:41 /usr/bin/python368 -> /usr/local/python3.6.8/bin/python3
-rwxr-xr-x 2 root root 4.4M Apr 18 09:56 /usr/bin/python3.6m
-rwxr-xr-x 1 root root 1018 Oct 29  2017 /usr/bin/python3-jsondiff
-rwxr-xr-x 1 root root 3.6K Oct 29  2017 /usr/bin/python3-jsonpatch
-rwxr-xr-x 1 root root 1.4K May  2  2016 /usr/bin/python3-jsonpointer
-rwxr-xr-x 1 root root  398 Nov 16  2017 /usr/bin/python3-jsonschema
lrwxrwxrwx 1 root root   10 Oct 25  2018 /usr/bin/python3m -> python3.6m
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/python368/bin/python3 /usr/bin/python
ln -s /usr/local/python368/bin/pip3 /usr/bin/pip
4.测试版本
:/usr/bin# python
Python 3.6.8 (default, Jun  9 2020, 14:37:48) 
[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放入其中。
:/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
:/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
:/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 选项使用了多个参数用于创建符号链接。最后一个参数指定了此选项的优先级,如果我们没有手动来设置替代选项,那么具有最高优先级的选项就会被选中。
:/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

相关推荐