
Python 2 was first released back in October 2000 and became a popular choice for developers building large-scale applications. Although it reached end-of-life (EOL) in January 2020 and is no longer maintained. However, many legacy systems and applications still depend on it. Since Rocky Linux 9 no longer includes Python 2 by default, you’ll need to install it manually using the source file.
This article explains how to install Python 2 on Rocky Linux 9, run it alongside Python 3, install Pip, and set up a virtual environment for your legacy Python 2 applications.
Before you begin, you need to:
To install Python 2 from source, your system must have essential development tools and libraries. These packages support compiling source code and enable Python features such as encryption (SSL), file compression, and external library integration. Follow the steps below to install the required dependencies on Rocky Linux 9.
Update the DNF package index.
Install the Development Tools group package.
This package group includes essential build utilities such as gcc, make, and other compilation tools necessary to compile software from source.
Install additional development libraries and tools.
The above command installs the libraries required for SSL, compression, C extensions, and tools to download and extract source files.
In this section, you will download, compile, and install Python 2.7 from source on Rocky Linux 9. The altinstall method is used to prevent interference with the system’s default Python interpreter, ensuring stability for system tools and scripts. Follow the steps below to compile and install Python 2.7.
Check the default version of Python to confirm Python 3 is already installed on the server.
Your output should be similar to the one below:
Download the official Python 2.7 source archive from the Python releases page.
Extract the downloaded archive.
Navigate into the extracted directory.
Configure the build with common options for compatibility and performance.
The above command prepares the source to build with system-wide FFI support, computed gotos for faster loops, general performance optimizations, and extended Unicode (UCS-4) support.
Run the make command to build the source using all available CPU cores.
This compiles the Python source code efficiently by parallelizing the build process across all CPU cores.
Install Python 2.7 using altinstall to avoid overwriting the system Python package.
View the installed version of Python 2.7.
Output:
Create a symbolic link to use python2 as a shortcut for python2.7.
Pip is the package manager used to install and manage Python libraries. In this section, you'll use the official get-pip.py bootstrap script to install Pip for Python 2.7. Follow the steps below to install pip using the bootstrap script.
Download the Pip installation script get-pip.py for Python 2.7.
This script is maintained by the Python Packaging Authority and installs a compatible version of Pip for Python 2.7.
Install Pip using Python 2.
Run the following command to view the installed version of Pip.
OR
Your output should be similar to the one below:
Python 2 does not include the venv module, which was introduced in Python 3.3. Instead, use the virtualenv package to create isolated Python 2 environments for your projects. Follow the steps below to install and configure a Python 2 virtual environment.
Navigate to the directory where you want to create the virtual environment.
Install the virtualenv package using Pip.
Create a virtual environment using Python 2.
Your output should be similar to the one below:
Verify that the environment directory contains the required files.
Your output should be similar to the one below:
From the above output, you can conclude that all the requisite files and directories are available in the my_env environment for use.
Activate the virtual environment.
After activation, your shell prompt will change to indicate that the environment is active, like below:
View the virtual environment Python version.
Your output should be similar to the one below:
To deactivate the virtual environment and return to the global Python context, run the following command.
You will notice that your shell prompt changes back to the normal state, without the virtual environment prefix, like below:
In this section, you will test the functionality of your Python 2 installation by running a simple Python code in the interactive shell and creating a basic Flask web application. Follow the steps below to ensure everything is set up correctly and working as expected.
Launch the Python 2 interactive shell.
Print a test message to confirm the shell is functioning.
Output:
Exit the Python interactive shell.
Install the Flask framework using Pip.
Create a file named app.py using a text editor such as nano.
Add the following Python code into the file.
Save and close the file.
Start the Flask application in the background using nohup.
This launches the app on port 5000, accessible to incoming requests.
Test the application using curl to ensure it's serving content as expected.
Output:
Python 2.7 is no longer supported with security updates. You should upgrade to a newer Python version for better security and performance. Use Python 2 only if necessary for legacy applications.
You have compiled and installed Python 2.7 from source on Rocky Linux 9, configured Pip for package management, and set up isolated environments using virtualenv to manage legacy applications. You have verified the setup by launching the interactive Python shell, installing Flask, and deploying a minimal web app accessible on your server. This installation allows you to keep older Python 2 projects without disrupting system-level Python 3 components. For deeper usage and configuration, refer to the official Python 2.7 documentation.
0 Comments
Be the first to comment and share your perspective with the community.