How to Install Python 2 on Rocky Linux 9
Python 2 was originally introduced in October 2000 and quickly gained traction among developers creating large-scale software systems. While official support ended in January 2020, and it’s no longer actively maintained, numerous older applications still rely on it. Since Python 2 isn’t included by default in Rocky Linux 9, you’ll have to compile and install it manually from the source code.
This guide will walk you through the process of setting up Python 2 on Rocky Linux 9, including side-by-side usage with Python 3, installing Pip, and setting up a virtual environment for legacy development work.
Prerequisites
Before proceeding, make sure you have:
- Access to a Rocky Linux 9 machine
- A non-root account with sudo privileges
Install Required Build Packages
To compile Python 2 from source, you’ll need essential development libraries and tools. These components enable features like SSL encryption, file compression, and external module support. Follow the instructions below to install them on Rocky Linux 9.
Step 1: Refresh the DNF Cache
$ sudo dnf makecache
Step 2: Install the Development Tools Group
This group includes compilers like GCC and Make for building software from source:
$ sudo dnf groupinstall -y "Development Tools"
Step 3: Add Required Development Libraries
These packages support encryption, compression, and code execution from archives:
$ sudo dnf install -y openssl-devel bzip2-devel libffi-devel wget tar
Build and Install Python 2.7
In this section, you’ll download, compile, and install Python 2.7 on Rocky Linux 9. To avoid overwriting Python 3, the altinstall
method is used.
Check the Installed Python Version
$ python --version
Example output:
Python 3.9.21
Download the Python 2.7 Source
$ wget https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tgz
Extract the Archive
$ tar xzf Python-2.7.18.tgz
Change Directory
$ cd Python-2.7.18
Configure the Build
Optimize performance and enable Unicode support:
$ ./configure --with-system-ffi --with-computed-gotos --enable-optimizations --enable-unicode=ucs4
Compile the Source
Use all available cores for faster compilation:
$ make -j$(nproc) build_all
Install Using Altinstall
$ sudo make altinstall
Check Python 2 Version
$ python2.7 --version
Output should be:
Python 2.7.18
Create a Python 2 Shortcut
$ sudo ln -s /usr/local/bin/python2.7 /usr/local/bin/python2
Install Pip for Python 2
To manage Python 2 packages, use Pip via the official get-pip.py
script.
Download the Script
$ wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
Run the Script
$ python2 get-pip.py
Verify Pip Installation
$ python2 -m pip --version
# OR
$ pip2 --version
Expected output:
pip 20.3.4 from /home/linuxuser/.local/lib/python2.7/site-packages/pip (python 2.7)
Create a Python 2 Virtual Environment
Python 2 lacks the built-in venv
module. Use the virtualenv
package instead.
Navigate to a Working Directory
$ cd ~
Install virtualenv
$ pip2 install virtualenv
Create the Environment
$ virtualenv --python=2 my_env
Output sample:
created virtual environment CPython2.7.18.final.0-64 in 168ms...
Check Environment Contents
$ ls my_env
Sample output:
bin include lib pyvenv.cfg
Activate the Environment
$ source my_env/bin/activate
Shell prompt will update:
(my_env) [linuxuser@RockyLinux9 ~]$
Confirm Python Version
$ python --version
Expected output:
Python 2.7.18
Exit the Environment
$ deactivate
Shell prompt reverts to default:
[linuxuser@RockyLinux9 ~]$
Verify and Use Python 2
In this part, you will test whether Python 2 was installed properly by launching the interactive shell and writing a simple web server using Flask. Complete the following steps to verify your setup is functioning as expected.
Open the Python 2 Shell
$ python2
Execute a Test Print Statement
>>> print("Greetings from centron!")
Expected output:
Greetings from centron!
Exit the Interactive Environment
>>> exit()
Install Flask via Pip
$ pip2 install flask
Create a Python Web App File
Use a text editor such as nano to make a new Python script:
$ nano app.py
Add the Flask Application Code
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Greetings from centron!"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Start Flask in Background Using nohup
This launches the app on port 5000, ready to accept external traffic:
$ nohup python2 app.py &
Validate the Web App with curl
Use curl to test if the Flask app is responding correctly:
$ curl http://localhost:5000
Expected response:
Greetings from centron!
Security Notice
Be aware that Python 2.7 is no longer supported with security patches. For better safety and functionality, it’s strongly recommended to switch to a supported Python version. Use Python 2 only for maintaining outdated applications that can’t be upgraded.
Conclusion
You’ve successfully compiled Python 2.7 from source on Rocky Linux 9, added Pip for managing packages, and used virtualenv to isolate environments for legacy apps. You verified the setup by running the Python shell, installing Flask, and deploying a simple web app. This process ensures your older Python 2 projects can function without disrupting the system’s default Python 3 environment. For more advanced features and usage, consult the official Python 2.7 documentation.