前言
在基于 LINUX 操作系统之上安装所需开发环境组件时,可能会遇到无可避免的场景是:同一个组件,我们需要同时使用到两个或者更多的版本,比如 Java 有 1.6、1.7、1.8 等多版本,又比如 Python 有 2、3 等等,这里以 Python 组件为例,以搭建一套 Android 的 AOSP 编译环境为目标,对使用 update-alternatives 命令管理多个组件版本
进一步详解。
update-alternatives
-
操作系统信息
itaso@ubuntu:~$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 16.04.7 LTS Release: 16.04 Codename: xenial itaso@ubuntu:~$
-
使用查看
update-alternatives --help
itaso@ubuntu:~$ update-alternatives --help Usage: update-alternatives [<option> ...] <command>Commands:--install <link> <name> <path> <priority>[--slave <link> <name> <path>] ...add a group of alternatives to the system.--remove <name> <path> remove <path> from the <name> group alternative.--remove-all <name> remove <name> group from the alternatives system.--auto <name> switch the master link <name> to automatic mode.--display <name> display information about the <name> group.--query <name> machine parseable version of --display <name>.--list <name> display all targets of the <name> group.--get-selections list master alternative names and their status.--set-selections read alternative status from standard input.--config <name> show alternatives for the <name> group and ask theuser to select which one to use.--set <name> <path> set <path> as alternative for <name>.--all call --config on all alternatives.<link> is the symlink pointing to /etc/alternatives/<name>.(e.g. /usr/bin/pager) <name> is the master name for this link group.(e.g. pager) <path> is the location of one of the alternative target files.(e.g. /usr/bin/less) <priority> is an integer; options with higher numbers have higher priority inautomatic mode.Options:--altdir <directory> change the alternatives directory.--admindir <directory> change the administrative directory.--log <file> change the log file.--force allow replacing files with alternative links.--skip-auto skip prompt for alternatives correctly configuredin automatic mode (relevant for --config only)--verbose verbose operation, more output.--quiet quiet operation, minimal output.--help show this help message.--version show the version. itaso@ubuntu:~$
itaso@ubuntu:~$ 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) <优先级> 是一个整数,在自动模式下,这个数字越高的选项,其优先级也就越高。...... itaso@ubuntu:~$
-
查看替换组的信息
update-alternatives --display python
itaso@ubuntu:~$ update-alternatives --display python python - manual modelink best version is /usr/bin/python3.5link currently points to /usr/bin/python3.5link python is /usr/bin/python /usr/bin/python2.7 - priority 2 /usr/bin/python3.5 - priority 3 /usr/local/bin/python3.7 - priority 4 itaso@ubuntu:~$
itaso@ubuntu:~$ update-alternatives --display python python - 手动模式link best version is /usr/bin/python3.5链接目前指向 /usr/bin/python2.7link python is /usr/bin/python /usr/bin/python2.7 - 优先级 2 /usr/bin/python3.5 - 优先级 3 /usr/local/bin/python3.7 - 优先级 4 itaso@ubuntu:~$
或者 假装我们知道系统自带的组件都是安装在
/usr/bin
目录下,那么我们找一下它/它们itaso@ubuntu:/usr/bin$ cd ~ itaso@ubuntu:~$ pwd /home/itaso itaso@ubuntu:~$ find /usr/bin/ -name "python*" /usr/bin/python3 /usr/bin/python2 /usr/bin/python3m /usr/bin/python3.5 /usr/bin/python3.5m /usr/bin/python2.7 /usr/bin/python itaso@ubuntu:~$
通过使用
find
进行条件筛选,发现系统已经带有了python2
和python3
备注:python2 -> python2.7
这样的->
表示软连接
,这里可以把python2
看成 Windon系统 中的快捷方式
,所以这句话的意思是python2
真正执行的目标是python2.7
既然系统已经有两个 Python 的版本 ,那么已经足够用来展示多版本切换,就无需在自己手动安装其他版本的 Python 了(当然,并不阻止手动自己安装一个,一般手动安装的路径是/usr/local/bin
) -
版本管理配置
update-alternatives --install /usr/bin/python python /usr/local/bin/python3.7 4
为了能比较有说服力的进行版本管理,这边的验证使用了自己手动安装的 Python 3.7 版本 用于区分系统自带的 2.7 和 3.5 版本
--install
表示向 update-alternatives 注册服务名
/usr/bin/python
表示注册最终地址,成功后将会把命令在这个固定的目的地址做真实命令的软链,以后管理就是管理这个软链
python
表示服务名,以后管理时以它为关联依据
/usr/local/bin/python3.7
表示被管理的命令绝对路径
4
表示优先级,数字越大优先级越高 -
版本切换
update-alternatives --config python
itaso@ubuntu:~$ sudo update-alternatives --config python There are 3 choices for the alternative python (providing /usr/bin/python).Selection Path Priority Status ------------------------------------------------------------0 /usr/local/bin/python3.7 4 auto mode1 /usr/bin/python2.7 2 manual mode2 /usr/bin/python3.5 3 manual mode * 3 /usr/local/bin/python3.7 4 manual modePress <enter> to keep the current choice[*], or type selection number: 1 update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in manual mode itaso@ubuntu:~$ sudo update-alternatives --config python There are 3 choices for the alternative python (providing /usr/bin/python).Selection Path Priority Status ------------------------------------------------------------0 /usr/local/bin/python3.7 4 auto mode * 1 /usr/bin/python2.7 2 manual mode2 /usr/bin/python3.5 3 manual mode3 /usr/local/bin/python3.7 4 manual modePress <enter> to keep the current choice[*], or type selection number: 1 itaso@ubuntu:~$
备注:注意
*
号的位置itaso@ubuntu:~$ sudo update-alternatives --config python 有 3 个候选项可用于替换 python (提供 /usr/bin/python).选择 路径 优先级 状态 ------------------------------------------------------------0 /usr/local/bin/python3.7 4 自动模式1 /usr/bin/python2.7 2 手动模式2 /usr/bin/python3.5 3 手动模式 * 3 /usr/local/bin/python3.7 4 手动模式要维持当前值[*]请按<回车键>,或者键入选择的编号: 1 update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in manual mode itaso@ubuntu:~$
至此,以上是在自己搭建的虚拟机系统的真实输入日志,有效的提供了从 查看操作系统
、查看 update-alternatives 帮助手册
、添加多版本管理配置
以及最后的 版本切换
操作等常用需求