Introduction
Python is the most widely used programming language for web development, data science, automation, and machine learning. Ubuntu 24.04 ships with Python 3.12 pre-installed as part of the base system — many system tools depend on it — but the default installation does not include pip (the Python package manager) or the venv module needed to create isolated project environments.
Starting with Ubuntu 23.04, running pip install outside a virtual environment is blocked by default. This is a deliberate change to prevent pip from overwriting system-managed Python packages, which can break critical system tools like apt itself. The correct workflow on Ubuntu 24.04 is to install the venv and pip packages through APT, create a virtual environment for each project, and install dependencies inside that environment.
In this tutorial, you will verify the pre-installed Python version, install pip and the venv module, create and activate a virtual environment, install packages with pip, and run a test script to confirm everything works. On a Raff VM, Python 3.12 is ready to use within minutes — the AMD EPYC processors deliver strong single-thread performance for CPU-bound Python workloads like data processing and API servers.
Step 1 — Verify the Pre-Installed Python Version
Ubuntu 24.04 includes Python 3.12 out of the box. Verify it is installed and check the version:
bashpython3 --version
Expected output:
Python 3.12.3
Note
The exact patch version may differ. The important thing is that it shows Python 3.12.x. Do not install Python 2 — it has been end-of-life since January 2020 and is not available in Ubuntu 24.04's default repositories.
Check where the Python binary is located:
bashwhich python3
Expected output:
/usr/bin/python3
On Ubuntu 24.04, the python command (without the 3) is not available by default. If you need it as an alias, you can install the python-is-python3 package later in this tutorial.
Step 2 — Install pip and the venv Module
pip is the standard package manager for Python. It downloads and installs packages from the Python Package Index (PyPI), which hosts over 500,000 packages. The venv module creates isolated environments for project dependencies.
Install both packages:
bashsudo apt update
sudo apt install -y python3-pip python3-venv
Verify pip is installed:
bashpip3 --version
Expected output:
pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12)
Warning
Do not run pip3 install <package> directly on Ubuntu 24.04. The system will block it with an externally-managed-environment error. This is by design — installing packages system-wide with pip can overwrite APT-managed files and break system tools. Always use a virtual environment instead (Step 3).
If you also want the python command to point to python3, install the compatibility package:
bashsudo apt install -y python-is-python3
After installing, python --version and python3 --version will return the same result.
Step 3 — Create a Virtual Environment
A virtual environment is an isolated directory that contains its own Python binary and pip installation. Packages installed inside a virtual environment do not affect the system Python or other projects. This is the required workflow on Ubuntu 24.04.
Create a project directory and a virtual environment inside it:
bashmkdir ~/my-python-project
cd ~/my-python-project
python3 -m venv venv
The python3 -m venv venv command creates a directory called venv containing a copy of the Python binary, pip, and setuptools. The environment is completely isolated from the system.
Activate the virtual environment:
bashsource venv/bin/activate
After activation, your shell prompt changes to include (venv) at the beginning:
(venv) user@server:~/my-python-project$
This indicates that any python or pip commands will use the virtual environment's binaries and packages, not the system ones.
Verify that the virtual environment is active:
bashwhich python
Expected output:
/home/your_user/my-python-project/venv/bin/python
The path points to the virtual environment, not /usr/bin/python3. Now pip is unblocked — you can install packages freely inside this environment.
Check the pip version inside the environment:
bashpip --version
Tip
Always activate the virtual environment before working on a project. If you open a new terminal session, navigate to your project directory and run source venv/bin/activate again. A common mistake is forgetting to activate and wondering why installed packages are not found.
Step 4 — Install Packages with pip
With the virtual environment active, install packages from PyPI. Start with a common package — requests, the most popular HTTP library for Python:
bashpip install requests
pip downloads the package and its dependencies into the virtual environment. Verify the installation:
bashpip show requests
You should see the package version, location (inside your venv directory), and its dependencies.
Install multiple packages at once:
bashpip install flask gunicorn
Flask is a lightweight web framework and Gunicorn is a production-grade WSGI server. Together, they form a common stack for Python web applications.
List all installed packages in the current environment:
bashpip list
You should see requests, flask, gunicorn, and their dependencies listed.
To save your project's dependencies for reproducibility, generate a requirements file:
bashpip freeze > requirements.txt
This creates a requirements.txt file that records every package and its exact version. Anyone cloning your project can recreate the exact same environment with:
bashpip install -r requirements.txt
Note
Always commit requirements.txt to version control. Never commit the venv directory — it is large, platform-specific, and easily recreated from the requirements file. Add venv/ to your .gitignore.
Step 5 — Run a Test Script
Create a simple Python script to confirm everything works. Make sure your virtual environment is active:
bashnano app.py
Add the following code:
pythonfrom flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello from Python on Raff!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Save and close the file. Run the application:
bashpython app.py
You should see:
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:5000
* Running on http://your_server_ip:5000
Open a second terminal and test the server:
bashcurl http://localhost:5000
Expected output:
Hello from Python on Raff!
If UFW is enabled and you want to access the application from your browser, allow port 5000:
bashsudo ufw allow 5000/tcp
Visit http://your_server_ip:5000 in your browser. You should see the "Hello from Python on Raff!" message. Press Ctrl+C in the first terminal to stop the server. Remove the firewall rule when done:
bashsudo ufw delete allow 5000/tcp
Step 6 — Deactivate and Manage Environments
When you are finished working on a project, deactivate the virtual environment:
bashdeactivate
The (venv) prefix disappears from your prompt, and python and pip commands revert to the system versions.
You can create separate virtual environments for different projects. Each one is independent:
bashmkdir ~/project-a && cd ~/project-a && python3 -m venv venv
mkdir ~/project-b && cd ~/project-b && python3 -m venv venv
Project A might use Flask 3.0 while Project B uses Django 5.0 — they will not conflict because each has its own isolated environment.
To delete a virtual environment, simply remove the directory:
bashrm -rf ~/my-python-project/venv
This removes all installed packages and the virtual environment itself. Your source code is unaffected because it lives outside the venv directory.
To upgrade pip inside a virtual environment to the latest version:
bashsource venv/bin/activate
pip install --upgrade pip
Keeping pip updated ensures you have the latest security fixes and compatibility improvements for package installation.
Step 7 — Install Development Tools (Optional)
For Python development, you may need additional system libraries that some pip packages require for compilation. Install the common development headers:
bashsudo apt install -y python3-dev build-essential libssl-dev libffi-dev
These packages are needed by pip packages that include C extensions, such as cryptography, psycopg2 (PostgreSQL adapter), mysqlclient (MySQL adapter), and Pillow (image processing). Without them, pip shows compilation errors during installation.
If you plan to work with databases on this server, the relevant client libraries are also required:
bash# For PostgreSQL projects
sudo apt install -y libpq-dev
# For MySQL/MariaDB projects
sudo apt install -y libmysqlclient-dev
These allow pip packages like psycopg2 and mysqlclient to compile their native extensions against the correct database client libraries. If you are using PostgreSQL or MySQL on the same server, these headers are already installed as dependencies.
Conclusion
You have installed pip and the venv module on your Raff Ubuntu 24.04 VM, created an isolated virtual environment, installed packages with pip, run a Flask test application, and learned how to manage multiple environments for different projects. Your server is ready for Python development and deployment.
From here, you can:
- Deploy a production Flask or Django application behind Nginx as a reverse proxy with Gunicorn as the WSGI server
- Containerize your Python application using Docker for consistent deployments across environments
- Connect to PostgreSQL or MySQL from your Python application using
psycopg2ormysqlclient - Set up automation scripts with Python for server management, monitoring, and data processing
- Use Portainer to manage Docker-based Python deployments through a web interface
Raff VMs with AMD EPYC processors deliver strong single-thread performance that directly benefits CPU-bound Python workloads. For I/O-bound applications like web APIs, the NVMe SSD storage ensures fast database queries and file operations. Python development fits comfortably on a Tier 1 VM ($3.99/month), while data-intensive workloads benefit from the larger RAM on Tier 3 or Tier 4 instances.
This tutorial was tested by our systems engineering team on a Raff CPU-Optimized Tier 2 VM with Python 3.12 and pip 24.0.

