Learn how to install multiple versions of Python on Rocky Linux 8 with this detailed guide. Follow step-by-step instructions to set up and manage different Python environments easily. #centlinux #linux #python
Table of Contents
Can you have Multiple Python versions installed?
Yes, you can have multiple versions of Python installed on a single system. This is particularly useful for developers who need to work on projects that require different Python versions. Here’s how you can manage multiple Python versions:
- Using Package Managers: You can use package managers like
yum
ordnf
on Linux to install different Python versions. For example, you can install Python 2.7 and Python 3.8 side by side. - Python Version Management Tools: Tools like
pyenv
allow you to easily install and switch between multiple Python versions.pyenv
is particularly popular for managing Python versions in development environments. - Virtual Environments: Python’s built-in
venv
module and third-party tools likevirtualenv
let you create isolated environments with specific Python versions and dependencies. This way, each project can have its own Python version and set of packages. - Custom Installation Paths: You can manually install different Python versions in separate directories and use environment variables or aliases to switch between them.
Advantages of using Multiple Versions of Python
Using multiple versions of Python on the same system offers several advantages, especially for developers and system administrators who manage diverse projects and environments. Here are some key benefits:
- Compatibility: Different projects may require different Python versions. Having multiple versions installed ensures that you can work on various projects without compatibility issues.
- Testing and Development: Developers can test their code across different Python versions to ensure compatibility and identify potential issues early in the development process.
- Access to Legacy Code: Some older projects might rely on deprecated features or libraries that only work with specific Python versions. Having multiple versions installed allows you to maintain and run legacy code alongside newer projects.
- Using Latest Features: While maintaining compatibility with older versions, you can also experiment with and utilize the latest features available in the newest Python releases for new projects.
- Development Environment Isolation: Tools like virtual environments (
venv
,virtualenv
) allow you to create isolated environments with specific Python versions and dependencies, preventing conflicts between projects. - Continuous Integration and Deployment: In CI/CD pipelines, testing code against multiple Python versions ensures that your software works consistently across different environments, enhancing reliability and robustness.
- Educational Purposes: For learning and teaching Python, having multiple versions installed allows students and educators to understand the differences and evolution of the language over time.
- Flexibility in Package Management: Some Python packages might have different versions compatible with different Python versions. Managing multiple Python versions allows you to install and use the appropriate package versions for each project.
Recommended Training: 100 Days of Code: The Complete Python Pro Bootcamp from Dr. Angela Yu
Environment Specification
We are working with a minimal installation of Rocky Linux 8 on a virtual machine configured with the following specifications. This minimal setup is purposefully designed to provide a clean, lightweight operating environment, free from unnecessary services or software, ensuring optimal performance and resource utilization. The minimal installation allows us to fully customize the server by adding only the required packages and configurations, making it an excellent choice for specific tasks such as web hosting, software development, or application deployment.
- CPU – 3.4 Ghz (2 cores)
- Memory – 2 GB
- Storage – 20 GB
- Operating System – Rocky Linux 8.5 (Green Obsidian)
- Hostname – python-01.centlinux.com
- IP Address – 192.168.116.128 /24
By starting with this streamlined base, we can focus on installing and configuring only what is needed, which improves security by reducing potential vulnerabilities associated with unused software. The virtual machine’s hardware specifications, such as CPU, RAM, and storage, have been carefully selected to meet the requirements of the task at hand, with room for scalability if needed. This setup provides a robust and flexible foundation for building and managing a reliable Linux environment.
Update your Rocky Linux Server
By using a SSH client software, login to python-01.centlinux.com as root user.
Rebuild cache of installed yum repositories.
# dnf makecache MariaDB 1.5 kB/s | 3.4 kB 00:02 Rocky Linux 8 - AppStream 434 kB/s | 9.7 MB 00:22 Rocky Linux 8 - BaseOS 647 kB/s | 6.8 MB 00:10 Rocky Linux 8 - Extras 3.6 kB/s | 12 kB 00:03 Metadata cache created.
Execute following command to update your Rocky Linux server.
# dnf update -y Last metadata expiration check: 0:00:59 ago on Thu 30 Apr 2022 11:40:04 PM PKT. Dependencies resolved. Nothing to do. Complete!
Check the Linux Operating System and Linux Kernel versions as follows.
# uname -r 4.18.0-348.20.1.el8_5.x86_64 # cat /etc/os-release NAME="Rocky Linux" VERSION="8.5 (Green Obsidian)" ID="rocky" ID_LIKE="rhel centos fedora" VERSION_ID="8.5" PLATFORM_ID="platform:el8" PRETTY_NAME="Rocky Linux 8.5 (Green Obsidian)" ANSI_COLOR="0;32" CPE_NAME="cpe:/o:rocky:rocky:8:GA" HOME_URL="https://rockylinux.org/" BUG_REPORT_URL="https://bugs.rockylinux.org/" ROCKY_SUPPORT_PRODUCT="Rocky Linux" ROCKY_SUPPORT_PRODUCT_VERSION="8"
Install Python3 on Rocky Linux 8
To determine the Python versions available for installation through the standard YUM repositories on Rocky Linux 8, you need to list the packages provided by the system’s default package manager. This step is essential for identifying the versions of Python that are readily available and supported by your system without requiring manual compilation or third-party repositories.
By querying the YUM repositories, you can view all the available Python packages, including the default Python version for the operating system and any additional versions offered as modules or alternatives. This approach ensures that you install a compatible version of Python tailored to your specific project or application requirements.
To list the available Python versions, use the appropriate dnf
or yum
command to search for Python-related packages. The output will provide a clear overview of the Python versions you can install directly from the system’s repositories, simplifying the setup process and ensuring compatibility with the Rocky Linux 8 environment. This method is particularly useful for maintaining a stable and efficient development or production system.
# dnf search python | grep ^python | grep nterpre Last metadata expiration check: 2:12:22 ago on Sun 01 May 2022 09:51:56 PM PKT. python36.x86_64 : Interpreter of the Python programming language python38.x86_64 : Interpreter of the Python programming language python39.x86_64 : Version 3.9 of the Python interpreter python2.x86_64 : An interpreted, interactive, object-oriented programming language
Here, the latest available version of Python programming language is 3.9.
Let’s install Python 3.9 by executing dnf command.
# dnf install -y python39
After successful installation, verify the version of Python by using following command.
# python3 -V Python 3.9.6
Install Python2 on Rocky Linux 8
Similarly, install Python2 on your Linux server.
# dnf install -y python2
After installation, verify the version of Python as follows.
# python2 -V Python 2.7.18
Install Multiple Versions of Python on Linux
Linux offers the powerful alternatives
command, which is specifically designed to manage and switch between multiple software versions installed on the same operating system. This tool is particularly useful for configuring and using multiple Python versions, enabling you to set a preferred version as the system default while still allowing access to other installed versions when needed.
By leveraging the alternatives
command, you can easily configure your desired Python version without the risk of overwriting or conflicting with other versions. This approach ensures flexibility, especially when working on diverse projects that rely on different Python versions or dependencies.
To configure your preferred Python version, execute the following command, which updates the alternatives system to point to the desired Python binary. This process ensures that all subsequent calls to python
or python3
on the command line will use the version you have set as the default. This method is straightforward, reliable, and ideal for managing multiple Python environments on Rocky Linux 8.
# alternatives --config python There are 4 programs which provide 'python'. Selection Command ----------------------------------------------- *+ 1 /usr/libexec/no-python 2 /usr/bin/python3 3 /usr/bin/python3.9 4 /usr/bin/python2 Enter to keep the current selection[+], or type selection number: 2
Now, execute the python command to check active Python version.
# python -V Python 3.9.6
Again use the alternatives command to switch to another version of Python programming language.
# alternatives --config python There are 4 programs which provide 'python'. Selection Command ----------------------------------------------- * 1 /usr/libexec/no-python + 2 /usr/bin/python3 3 /usr/bin/python3.9 4 /usr/bin/python2 Enter to keep the current selection[+], or type selection number: 4
Again check the active Python version.
# python -V Python 2.7.18
You can see that, now Python 2.7 is active version on your Linux server.
Final Thoughts
Installing multiple versions of Python on Rocky Linux 8 allows you to maintain a flexible and robust development environment tailored to various project requirements. By leveraging tools like dnf
, source compilation, and version managers such as pyenv
, you can seamlessly switch between Python versions without conflicts.
This approach is especially useful for developers working on diverse projects that rely on different Python versions or dependencies. Rocky Linux 8’s stability and modular design make it an ideal platform for managing multiple Python environments. Whether you’re testing legacy applications or exploring the latest Python features, this setup ensures compatibility, efficiency, and ease of use in your development workflows.
Need a dependable Linux system administrator? I specialize in managing, optimizing, and securing Linux servers to keep your operations running flawlessly. Check out my services on Fiverr!