Ubuntu update-alternatives 安装/管理多版本 Python3及PIP3

  • 前言
  • 安装python
  • 安装对应版本pip
  • update-alternatives 切换python3 环境


前言

在ubuntu系统上,使用apt命令安装python只能获取到ubuntu发布的最高版本。例如我使用的ubuntu18.04只能安装python 3.6。在日常使用过程中,经常会遇到需要不同python版本的第三方软件,甚至有些工程强行指定python版本,版本高了也不行。
所以我们需要在一台机器上安装多个python版本来应对,同时也要能方便切换版本。
这也会带出对应python版本切换pip的问题。本文目的在于讲述完整的如何管理多版本python及pip的配置。

安装python

查看当前生效的python3 版本

python3 --version
Python 3.6.9

添加deadsnakes ppa源 来下载各个版本的python。
例如我要安装一个 python 3.7

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.7

安装对应版本pip

查看当前pip3版本

pip3 -V
pip 22.3 from /home/gel-x/.local/lib/python3.7/site-packages/pip (python 3.7)

安装指定pip版本

# 获取安装脚本,若无curl 需要先 sudo apt install curl
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
# 安装对应版本 pip
python3.7 get-pip.py

通过get-pip.py安装的pip默认会放到/home/${USER}/.local/bin 目录下。
查看PATH 是否包含了这个目录,若无则需要添加到系统路径。

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
# 添加~/.local/bin 到系统路径
$ source .profile
# .profile 内容如下:
$ cat .profile 
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
	. "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

update-alternatives 切换python3 环境

通过系统工具update-alternatives 可以配置和切换当前系统中的python3 环境做到多版本管理。

update-alternatives是linux debian 提供的一个多版管理器,其本质是为同名软件创建一个软连接,并可以设置优先级和方便的切换不同的版本。它不仅仅可以用于python,pip3,java等许多需要多版本管理的软件都可以通过update-alternatives来方便的切换版本。

$ update-alternatives --help
用法:update-alternatives [<选项> ...] <命令>
命令:
  --install <链接> <名称> <路径> <优先级>
    [--slave <链接> <名称> <路径>] ...
                           在系统中加入一组候选项。
  --remove <名称> <路径>   从 <名称> 替换组中去除 <路径> 项。
  --remove-all <名称>      从替换系统中删除 <名称> 替换组。
  --auto <名称>            将 <名称> 的主链接切换到自动模式。
  --display <名称>         显示关于 <名称> 替换组的信息。
  --query <名称>           机器可读版的 --display <名称>.
  --list <名称>            列出 <名称> 替换组中所有的可用候选项。
  --get-selections         列出主要候选项名称以及它们的状态。
  --set-selections         从标准输入中读入候选项的状态。
  --config <名称>          列出 <名称> 替换组中的可选项,并就使用其中哪一个,征询用户的意见。
  --set <名称> <路径>      将 <路径> 设置为 <名称> 的候选项。
  --all                    对所有可选项一一调用 --config 命令。

<链接> 是指向 /etc/alternatives/<名称> 的符号链接。(如 /usr/bin/pager)
<名称> 是该链接替换组的主控名。(如 pager)
<路径> 是候选项目标文件的位置。(如 /usr/bin/less)
<优先级> 是一个整数,在自动模式下,这个数字越高的选项,其优先级也就越高。
..........

下面以python3为列,说明配置方法。

# 将本地下载好的python3 版本导入update-alternatives
# 默认优先级高的会被设置为自动模式
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 0
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2

# 查看当前环境中已有的python3 版本
~$ sudo update-alternatives --list python3
/usr/bin/python3.6
/usr/bin/python3.7

# 切换想要使用的python3 版本
~$ sudo update-alternatives --config python3 
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3.6   2         auto mode
* 1            /usr/bin/python3.6   2         manual mode
  2            /usr/bin/python3.7   0         manual mode

Press <enter> to keep the current choice[*], or type selection number: 
# 输入对应python3版本的 Selection num 即可

同理可以将pip3 也通过update-alternatives配置切换,不过实际使用情况看,pip3默认保存最高版本的可以兼容大部分情况,还没有遇到需要切换的情景。