Tutorials  /  Python

Install Python 2.7 on Rocky Linux 9 with Pip and Flask

Ccentron Redaktion · August 2025 ·7 min read ·Python, Tutorial

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
VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

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

Console
$ sudo dnf makecache

Step 2: Install the Development Tools Group

This group includes compilers like GCC and Make for building software from source:

Console
$ sudo dnf groupinstall -y "Development Tools"

Step 3: Add Required Development Libraries

These packages support encryption, compression, and code execution from archives:

Console
$ 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

Console
$ python --version

Example output:

Python 3.9.21

Download the Python 2.7 Source

Console
$ wget https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tgz

Extract the Archive

Console
$ tar xzf Python-2.7.18.tgz

Change Directory

Console
$ cd Python-2.7.18

Configure the Build

Optimize performance and enable Unicode support:

Console
$ ./configure --with-system-ffi --with-computed-gotos --enable-optimizations --enable-unicode=ucs4

Compile the Source

Use all available cores for faster compilation:

Console
$ make -j$(nproc) build_all

Install Using Altinstall

Console
$ sudo make altinstall

Check Python 2 Version

Console
$ python2.7 --version

Output should be:

Python 2.7.18

Create a Python 2 Shortcut

Console
$ 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

Console
$ wget https://bootstrap.pypa.io/pip/2.7/get-pip.py

Run the Script

Console
$ python2 get-pip.py

Verify Pip Installation

Console
$ 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

Console
$ cd ~

Install virtualenv

Console
$ pip2 install virtualenv

Create the Environment

Console
$ virtualenv --python=2 my_env

Output sample:

created virtual environment CPython2.7.18.final.0-64 in 168ms...

Check Environment Contents

Console
$ ls my_env

Sample output:

bin include lib pyvenv.cfg

Activate the Environment

Console
$ source my_env/bin/activate

Shell prompt will update:

(my_env) [linuxuser@RockyLinux9 ~]$

Confirm Python Version

Console
$ python --version

Expected output:

Python 2.7.18

Exit the Environment

Console
$ 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

Console
$ python2

Execute a Test Print Statement

Code
>>> print("Greetings from centron!")

Expected output:

Greetings from centron!

Exit the Interactive Environment

Code
>>> exit()

Install Flask via Pip

Console
$ pip2 install flask

Create a Python Web App File

Use a text editor such as nano to make a new Python script:

Console
$ nano app.py

Add the Flask Application Code

Python
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:

Console
$ nohup python2 app.py &

Validate the Web App with curl

Use curl to test if the Flask app is responding correctly:

Console
$ 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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren