Learn how to effectively manage multiple Python versions on your Linux system using the alternatives
command. This guide provides step-by-step instructions to configure and switch between Python versions effortlessly. #centlinux #linux #python
Table of Contents
Problem Statement
Our Rocky Linux 9 server comes pre-equipped with not just one, but two versions of the Python programming language, offering versatility for various development needs. Among these, Python 3.9 stands out as the default version, thoughtfully included with all Red Hat Enterprise Linux 9-based systems, even in their Minimal installation configurations.
This dual Python setup ensures compatibility with a wide range of applications, whether you’re working on legacy scripts that rely on older Python versions or modern projects optimized for Python 3.9. Having Python pre-installed saves time and effort, allowing developers to dive straight into coding or configuring software without additional setup steps. It reflects the importance of Python as an integral tool for developers and administrators alike on Linux platforms.
Introduction
The alternatives
command in Linux is a powerful tool used to manage different versions of the same software installed on a system. Many Linux distributions, such as Red Hat-based systems (including Rocky Linux and CentOS), use this command to create a flexible and efficient way to handle multiple software versions or implementations.
Key Features of the alternatives
Command:
- Switch Between Versions: If you have multiple versions of a program (e.g., Python, Java, or text editors), the
alternatives
command allows you to choose the default version easily. - Symbolic Links Management: It manages symbolic links for system binaries, libraries, and man pages, ensuring the selected version is used system-wide.
- Priority-Based System: Each installed version of the software can have a priority. The system automatically uses the version with the highest priority unless explicitly overridden.
- User Interaction: The command provides an interactive interface where you can select the default version from the available options.
Basic Usage:
- View Current Alternatives: Use
alternatives --display <name>
to see the current settings and available options for a particular command. - Switch Alternatives Manually: Run
alternatives --config <name>
to interactively choose a different version. - Set Default Version: Use
alternatives --set <name> <path>
to set a specific version as the default manually.
Recommended Training: Complete Linux Training Course to Get Your Dream IT Job 2025 from Imran Afzal
Example Use Case:
If you have Python 2.x and Python 3.x installed, you can use alternatives
to switch between them as the default Python interpreter for the system. This flexibility is especially useful in environments where different applications require different software versions.
By leveraging the alternatives
command, Linux administrators and users can streamline software version management, ensuring compatibility and efficiency across various use cases.
Install Python on Linux
Python 3.10 has already been installed on this machine. If you wish to install it, you can follow the detailed instructions provided in our previous article: How to Install Python 3.10 on Rocky Linux 9. This article walks you through the entire process step-by-step, ensuring a smooth installation experience.
To confirm the availability of Python versions installed on your Rocky Linux 9 system, you can verify them by executing the following commands. These commands will display the installed Python versions, allowing you to confirm the presence of Python 3.10 or any other versions on your machine.
# python3.9 -V Python 3.9.14 # python3.10 -V Python 3.10.4
Alternatives Command Usage
The Linux operating system includes the versatile alternatives
command, which allows you to manage and configure default versions of commands when multiple versions are installed on your system. This utility is particularly helpful in ensuring that the correct version of a program is used by default, without needing to specify the complete path every time.
For example, if you have multiple versions of Python installed, you can use the alternatives
command to easily set an active version as the system default. This ensures that when you type python
in the terminal, the system will invoke the version you have chosen as the default.
To set an active version of Python on your system, use the following command. This will update the symbolic link and align it with your desired Python version, making it the default interpreter for all Python-related tasks.
# alternatives --config python
If the above command returns no output, check whether python is available in commands list known to alternatives.
# alternatives --list libnssckbi.so.x86_64 auto /usr/lib64/pkcs11/p11-kit-trust.so soelim auto /usr/bin/soelim.groff iptables auto /usr/sbin/iptables-nft ebtables auto /usr/sbin/ebtables-nft arptables auto /usr/sbin/arptables-nft cifs-idmap-plugin auto /usr/lib64/cifs-utils/cifs_idmap_sss.so man auto /usr/bin/man.man-db ld auto /usr/bin/ld.bfd
You can see that, there is no python command in list. Therefore, you have to add Python in alternatives info base.
Check the location of Python executable files.
# whereis python3.9 python3.9: /usr/bin/python3.9 /usr/lib/python3.9 /usr/lib64/python3.9 /usr/include/python3.9 /usr/share/man/man1/python3.9.1.gz # whereis python3.10 python3.10: /usr/local/bin/python3.10 /usr/local/lib/python3.10
Note down the location of Python executables from above output and then execute following commands to define Python alternatives.
# alternatives --install /usr/bin/python python /usr/bin/python3.9 1 # alternatives --install /usr/bin/python python /usr/local/bin/python3.10 2
You can now execute following command to set an active version of Python command.
# alternatives --config python There are 2 programs which provide 'python'. Selection Command ----------------------------------------------- 1 /usr/bin/python3.9 *+ 2 /usr/local/bin/python3.10 Enter to keep the current selection[+], or type selection number:
Currently, Python3.10 is set as active Python command.
You can verify it by executing following command at Linux bash prompt.
# python -V Python 3.10.4
Now, change the active version of Python command to 3.9 as follows.
# alternatives --config python There are 2 programs which provide 'python'. Selection Command ----------------------------------------------- 1 /usr/bin/python3.9 *+ 2 /usr/local/bin/python3.10 Enter to keep the current selection[+], or type selection number: 1
Check the version of Python command again.
# python -V Python 3.9.14
Video of Alternatives Command Example
If you prefer a visual demonstration, check out our detailed video tutorial on using the alternatives
command for Python. The tutorial walks you through each step, from configuring multiple Python versions to switching between them effortlessly. This hands-on guide ensures you understand the process clearly and can follow along at your own pace. Whether you’re new to Linux or an experienced user, the video provides practical tips and insights to enhance your learning experience.
Conclusion:
The alternatives
command is a powerful utility for managing multiple versions of Python on your Linux system. By following this guide, you’ve learned how to configure, set priorities, and switch between Python versions seamlessly. This approach ensures flexibility and compatibility for diverse projects, making it an essential tool for developers and system administrators. Whether you’re testing new software or maintaining legacy systems, mastering the alternatives
command will enhance your workflow and productivity.
From setting up scalable AWS solutions to managing complex Linux environments, I’ve got you covered. Visit my Fiverr profile to get started.