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.
If you want to develop some amazing projects in Python Programming Language, then you should start with Python Crash Course, 2nd Edition (PAID LINK) written by Eric Matthes.
Or you can buy an online course such as The Complete Python Programming Course: Beginner to Advanced
Environment Specification
We are using a minimal Rocky Linux 8 virtual machine with following specifications.
- 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
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
List down the available Python versions in your standard yum repositories.
# 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 provides alternatives command to configure and use multiple Python versions on a same operating system.
Execute following command to configure your preferred Python version.
# 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 is an essential skill for developers and system administrators who work on diverse projects. By following this guide, you can easily set up and manage different Python environments, ensuring compatibility and flexibility across your applications.
If you need further assistance with installing multiple Python versions on Rocky Linux 8 or have other Linux-related needs, I offer professional services on Fiverr. Visit my Fiverr profile here to explore my offerings, including Python environment setup, system configuration, scripting, and more. Let me help you streamline your development workflow with expert guidance and support.