Explain the alternatives command in detail (this example uses Python version switching as an example)

                                                  Explain the alternatives command in detail (this example uses Python version switching as an example)

Alternatives is a built-in command of the Linux series of operating systems, even with minimal installation. Its main function is version control switching. For example, there are multiple Python versions in your system, such as Python3.8, Python2.7.5, and Python3. 6,.

First of all, it should be clear that multiple versions of Python can coexist in the same system, because the installation directory can be specified when compiling and installing using source code. If it is installed in rpm or yum, then there may be a version conflict problem. In addition, each version of Python has its own characteristics. For example, the major version of Python3 basically comes with a pip package manager, and Python2.7 does not have pip, so you need to install it manually. In order to distinguish, the pip version of python3.8 is 21.0.1, and the pip version of Python2.7.5 is 20.3.4.

Environmental introduction:

Centos7.2 system, use the built-in Python 2.7.5 and the Python 3.8.1 installed from the source code, and install pip20.3.4 manually for Python 2.7.5. Since Python is compiled from source code, it comes with pip. After installing Python 3.8 Upgrade pip to 21.0.1.

Introduction to alternatives command:

[root@centos6 piprpm]# alternatives 
alternatives version 1.7.4 - Copyright (C) 2001 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License.

usage: alternatives --install <link> <name> <path> <priority>
                    [--initscript <service>]
                    [--family <family>]
                    [--slave <link> <name> <path>]*
       alternatives --remove <name> <path>
       alternatives --auto <name>
       alternatives --config <name>
       alternatives --display <name>
       alternatives --set <name> <path>
       alternatives --list

common options: --verbose --test --help --usage --version --keep-missing
                --altdir <directory> --admindir <directory>

The main commonly used parameters are the 5 parameters of install, remove, config, display, and list.

install - Generate soft connection

remove --remove soft connection

config - select soft connection

display --Display soft connection

list - show all soft connections



Assuming that Python 3.8.1 has been installed, the installation directory is specified in /usr/local/python3.8, the pip of Python 2.7.5 is installed with pip 8.1.2, and then upgraded to pip20.3.4, all Python and pip can be used normally,

First, add Python 3.8.1 to alternatives management, the command is:

alternatives --install /usr/bin/python python /usr/local/python3.8/bin/python3.8 3 This command adds Python3.8 to alternatives.

Second, add Python 2.7.5 to alternatives management, the command is:

alternatives --install /usr/bin/python python /usr/bin/python2.7 2

Then, switch the version of Python to Python3.8

[root@centos6 ~]# alternatives --config python

There are 2 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
   1           /usr/bin/python2.7
*+ 2           /usr/local/python3.8/bin/python3.8

Enter to keep the current selection[+], or type selection number: 2

After choosing 2 you can switch to Python 3.8.1 version . 

/usr/bin/python What file is it first?

[root@centos7 ~]# ls -al /usr/bin/python
lrwxrwxrwx. 1 root root 7 Jan 23 22:13 /usr/bin/python -> python2

[root@centos7 bin]# ls -al /usr/bin/python2
lrwxrwxrwx. 1 root root 9 Jan 23 22:13 python2 -> python2.7

We can see that before the alternatives management is added, it is a link file, pointing to /usr/bin/python2 and Python2 points to /usr/bin/python2.7.

After joining the alternatives management, the direction of /usr/bin/python is different again

[root@centos6 ~]# ls -al /usr/bin/python
lrwxrwxrwx 1 root root 24 Mar 27 05:33 /usr/bin/python -> /etc/alternatives/python

[root@centos6 ~]# ls -al /etc/alternatives/python
lrwxrwxrwx 1 root root 34 Mar 27 05:33 /etc/alternatives/python -> /usr/local/python3.8/bin/python3.8

This is the point of /etc/alternatives/python after switching the Python version to 3.8. You can see that it is linked to /usr/local/python3.8/bin/python3.8.

So, what about now after switching to Python 2.7.5?

[root@centos6 ~]# alternatives --config python

There are 2 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
   1           /usr/bin/python2.7
*+ 2           /usr/local/python3.8/bin/python3.8

Enter to keep the current selection[+], or type selection number: 2^H

There are 2 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
   1           /usr/bin/python2.7
*+ 2           /usr/local/python3.8/bin/python3.8

Enter to keep the current selection[+], or type selection number: 1
[root@centos6 ~]# ls -al /usr/bin/python
lrwxrwxrwx 1 root root 24 Mar 27 06:50 /usr/bin/python -> /etc/alternatives/python
[root@centos6 ~]# ls -al /etc/alternatives/python
lrwxrwxrwx 1 root root 18 Mar 27 06:50 /etc/alternatives/python -> /usr/bin/python2.7

Here, after using alternatives --config python, I chose 1, and switched to Python2.7 . After looking at the link, you can see that /etc/alternatives/python points to /usr/bin/python2.7l.

alternatives --install /usr/bin/python python /usr/local/python3.8/bin/python3.8 3 How is this command written? The file followed by install must be a link file, python is the project name, /usr/local/python3.8/bin/python3.8 is the absolute path of the actual file, and 3 is the priority. The priority set by the second command is 2, indicating that the higher priority Python 3.8 is used first, if it is in auto mode.

As shown in the figure below, the second line indicates manual mode. If it is auto, Python3.8 will be used first.

The third line says that it is currently using /usr/bin/python2.7, the fourth and fifth lines are the priority of the two versions, and the sixth line means that the best version is Python3.8.

[root@centos6 ~]# alternatives --display python
python - status is manual.
 link currently points to /usr/bin/python2.7
/usr/bin/python2.7 - priority 2
/usr/local/python3.8/bin/python3.8 - priority 3
Current `best' version is /usr/local/python3.8/bin/python3.8.

It should be noted that after the Python version is switched, the pip manager automatically switches with it, even if pip is two different components. 

Then, after the above experiments, we can conclude that alternatives is similar to a link manager, through alternatives --config project name, and then select the serial number to choose which project to use. Thereby dynamically adjusting the link point.

Alternatives can be used in the case of multiple gcc, tomcat, jdk versions in the same system to avoid version confusion. It is a very useful Linux command. ( Note that even if the system is restarted, as long as it is in manual mode, your Python version selection will still be maintained .)

Guess you like

Origin blog.csdn.net/alwaysbefine/article/details/115261860