Install Multiple Versions of Python on Rocky Linux 8

Share on Social Media

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

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:

  1. Using Package Managers: You can use package managers like yum or dnf on Linux to install different Python versions. For example, you can install Python 2.7 and Python 3.8 side by side.
  2. 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.
  3. Virtual Environments: Python’s built-in venv module and third-party tools like virtualenv 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.
  4. Custom Installation Paths: You can manually install different Python versions in separate directories and use environment variables or aliases to switch between them.
Install Multiple Versions of Python on Rocky Linux 8
Install Multiple Versions of Python on Rocky Linux 8

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:

  1. Compatibility: Different projects may require different Python versions. Having multiple versions installed ensures that you can work on various projects without compatibility issues.
  2. Testing and Development: Developers can test their code across different Python versions to ensure compatibility and identify potential issues early in the development process.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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

2776760 f176 10
show?id=oLRJ54lcVEg&bids=1597309

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

Execute following command to update your Rocky Linux server.

dnf update -y

Check the Linux Operating System and Linux Kernel versions as follows.

uname -r
cat /etc/os-release

Output:

4.18.0-348.20.1.el8_5.x86_64
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

Output:

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

Output:

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

Output:

Python 2.7.18

Install Multiple Versions of Python

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

Output:

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

Output:

Python 3.9.6

Again use the alternatives command to switch to another version of Python programming language.

alternatives --config python

Output:

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

Output:

Python 2.7.18

You can see that, now Python 2.7 is active version on your Linux server.

Frequently Asked questions (FAQs)

1. Why would I need multiple Python versions on Rocky Linux 8?

Different projects may require specific Python versions due to compatibility or dependency issues. Rocky Linux 8 ships with Python 3.6 by default, but you might need newer versions (e.g., 3.9 or 3.11) for modern frameworks like Django or Flask.

Solution: Use dnf for official RPM packages or pyenv for fine-grained version control.


2. What’s the safest way to install additional Python versions without conflicts?

Following are the recommended methods for installing additional python versions without conflicts.

Official RPMs (Best for stability):

sudo dnf module install python39 # For Python 3.9

pyenv (Best for flexibility):

https://pyenv.run | bash # Installs pyenv pyenv install 3.11.4 # Downloads and compiles Python 3.11.4

Manual compilation (Advanced users):
Download source from python.org and install to /usr/local/.

Avoid replacing /usr/bin/python3—system tools depend on it!


3. How do I make a specific Python version the default for my user?

Using alternatives (System-wide, requires sudo):

sudo alternatives --config python3 # Select from installed versions

Using pyenv (Per-user, recommended):

pyenv global 3.11.4 # Sets default Python for your user exec $SHELL # Reloads shell to apply changes

Shell alias (Temporary):

alias python=python3.9 # Adds to ~/.bashrc for persistence

Verify: Run python --version to confirm the change.


4. How can I create a virtual environment with a specific Python version?

Install python3.x-venv (replace x with your version), then:

python3.9 -m venv myenv
source myenv/bin/activate

5. Will installing multiple Python versions break system tools?

No, if done correctly. Rocky Linux 8 relies on /usr/bin/python3 for system tools. Avoid replacing it; instead, use alternatives or pyenv for custom projects.


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!

Looking for something?

Leave a Reply