| Tested on | Ubuntu 26.04 LTS (Resolute Raccoon) |
|---|---|
| Package | Flask 3.1.3 (PyPI in .venv)python3-flask 3.1.3-1ubuntu1 (apt on 26.04 lab)python3 3.14.4 |
| Applies to | Ubuntu 22.04 LTS, 24.04 LTS, and 26.04 LTS |
| Privilege | sudo or root for apt; normal user inside a venv |
| Scope | Install Flask on Ubuntu with a project virtual environment and pip (recommended), verify with python -m flask --version, run a minimal Hello World app with flask --app hello run and curl, and optionally install Ubuntu's python3-flask package. Does not cover production deployment with Gunicorn or Nginx—that belongs in a separate guide. |
| Related guides | apt command in Linux pip command not found on Ubuntu |
Flask is a small Python web framework built on Werkzeug and Jinja2. For application development on Ubuntu, the recommended approach is to install Flask with pip inside a project virtual environment, as Flask's installation docs describe—not a global pip install into the system interpreter.
The commands below were run on Ubuntu 26.04 with Python 3.14 and Flask 3.1.3 from PyPI. The same .venv steps work on 24.04 and 22.04; only the default system Python and the python3-flask apt version change (see the table below). When you finish, you will have a working Hello World app and a clear split between venv + pip (development) and apt (optional distro package).
Quick answer
For application development, create a separate virtual environment for each Flask project. Install the venv module, create .venv, activate it, and install Flask with pip:
Refresh package indexes before installing system packages:
sudo apt updateInstall Python and the module that provides python3 -m venv (minimal images may need both):
sudo apt install python3 python3-venvCreate a project directory and an isolated environment named .venv:
mkdir -p ~/flask-app && cd ~/flask-appA virtual environment (venv) is an isolated Python environment for one project, so packages you install there do not modify Ubuntu's system Python or another project's dependencies.
Create the environment directory Flask's docs use by default:
python3 -m venv .venvLoad the venv's bin/ directory onto your PATH for this shell:
source .venv/bin/activatepip is Python's package installer; inside the virtual environment it installs Flask only for this project (from PyPI, the Python Package Index):
python -m pip install FlaskConfirm the CLI sees the venv copy of Flask:
python -m flask --versionThe same flow works on Ubuntu 26.04, 24.04, and 22.04. Ubuntu also ships python3-flask in apt for quick distro-managed installs, but a virtual environment is better for real application work because each project keeps its own dependency set.
| Approach | Use when |
|---|---|
| venv + pip (recommended) | Building apps, pinning Flask or extensions, reproducible projects |
sudo apt install python3-flask |
Quick tests, classroom labs, or tools that expect Ubuntu's packaged module |
Before you begin
Run these commands in a terminal on the Ubuntu system where you want to develop the Flask application. You need a user that can run sudo to install Ubuntu packages.
A virtual environment (venv) is an isolated Python environment for one project. Packages installed there do not modify Ubuntu's system Python or another project's dependencies.
In this guide:
python3refers to Ubuntu's system Python before the venv is activated.pythonrefers to the venv's Python after activation (Ubuntu normally invokes the system interpreter aspython3; once the venv is active,pythonpoints at the environment's interpreter).pipinstalls Python packages such as Flask from PyPI (the Python Package Index).- PATH is the ordered list of directories the shell searches when you type commands such as
pythonorpip.
The example runs Flask only on 127.0.0.1 (the local loopback address—this machine) and TCP port 5000 by default (Flask's development server listens there unless you choose another port).
Flask versions on Ubuntu 26.04, 24.04, and 22.04
| Ubuntu | Default Python | APT Flask base version | venv + pip (PyPI) |
|---|---|---|---|
| 26.04 LTS | 3.14 | 3.1.3 | 3.1.3 (current upstream) |
| 24.04 LTS | 3.12 | 3.0.2 | 3.1.3 |
| 22.04 LTS | 3.10 | 2.0.1 | 3.1.3 |
The table shows Flask's base/runtime version from apt, not Ubuntu's full package revision. Canonical may publish newer package revisions with security fixes—for example, current 24.04 and 22.04 archives ship revisions such as 3.0.2-1ubuntu1.1 and 2.0.1-2ubuntu1.2 while python3 -m flask --version may still report 3.0.2 or 2.0.1. Keep apt packages updated even when the runtime version number looks unchanged.
Flask supports Python 3.9 and newer, so all three LTS releases can run current Flask from PyPI inside a venv. Even on Ubuntu 26.04, where apt may match PyPI's Flask version, use a venv anyway—dependency isolation matters more than chasing the newest point release.
See check Ubuntu version if you are unsure which row applies. Python beside the default depends on the release:
- 24.04 — Install Python 3.12 on Ubuntu when 3.12 is enough (already the default
python3) - 26.04 — Install Python 3.14 on Ubuntu documents the default 3.14 path; use install latest Python on Ubuntu for upstream 3.15
- 22.04 — use install latest Python on Ubuntu or Install Python 3.11 on Ubuntu when a dependency pins 3.11
Why Flask should use a virtual environment
A virtual environment is a project-specific Python tree under a directory such as .venv/. Flask, Werkzeug, Jinja2, and anything else you pip install land there instead of in /usr/lib/python3/dist-packages, so upgrading one app does not break another project or an Ubuntu tool that relies on system Python.
Ubuntu marks the system interpreter as externally managed (PEP 668). A bare pip install Flask on /usr/bin/python3 fails with externally-managed-environment on current releases—the intended fix is python3 -m venv .venv, then python -m pip install Flask inside it. Do not use --break-system-packages unless you deliberately accept the risk to apt-managed Python.
Recommended project pip lives at ~/flask-app/.venv/bin/pip. System-wide /usr/bin/pip is not where you should install application libraries.
Install Flask on Ubuntu with venv and pip
Install Python and the venv module
Standard Ubuntu Desktop and Server images usually include python3. Minimal cloud images may not—check first:
python3 --versionSample output on the 26.04 lab:
Python 3.14.4If python3 is missing, install Python and the venv package. You do not normally need the system python3-pip package for this workflow; python3 -m venv creates the environment with its own pip tooling:
sudo apt install python3 python3-venvDo not remove the python3 package with apt; Ubuntu desktop tools and installers depend on it.
Create a Flask project directory
Create a directory for the example project. ~/flask-app keeps the path short:
mkdir -p ~/flask-app && cd ~/flask-appCreate and activate .venv
Flask's docs use a hidden .venv directory—easy to add to .gitignore and visually separate from application files:
python3 -m venv .venvActivate it for the current shell session:
source .venv/bin/activateYour prompt should show (.venv). Activation prepends .venv/bin to your PATH, so python and pip resolve inside the environment instead of under /usr/bin. Confirm with:
command -v python/home/user/flask-app/.venv/bin/pythonThe pip executable should sit beside that Python:
command -v pip/home/user/flask-app/.venv/bin/pipIf activation failed, you may still be on /usr/bin/python3—re-run source .venv/bin/activate from ~/flask-app.
Install Flask
Use python -m pip so the same interpreter that runs your app installs packages (after activation, use python rather than python3 because the venv's interpreter is exposed as python):
python -m pip install FlaskSample output (versions of dependencies may differ slightly):
Collecting Flask
Downloading flask-3.1.3-py3-none-any.whl.metadata (3.2 kB)
...
Successfully installed Flask-3.1.3 blinker-1.9.0 click-8.5.0 itsdangerous-2.2.0 jinja2-3.1.6 markupsafe-3.0.4 werkzeug-3.1.9Pin a specific release when a tutorial requires it:
python -m pip install 'Flask==3.1.3'Verify the installed Flask version
Check which Python, Flask, and Werkzeug versions this environment is using:
python -m flask --versionPython 3.14.4
Flask 3.1.3
Werkzeug 3.1.9With the venv active, flask --version prints the same lines.
Run a Flask Hello World application
This follows the Flask quickstart pattern: one module, one route, local dev server.
Stay in ~/flask-app with .venv activated unless you already deactivated for the apt section below.
Create hello.py
Save a minimal application module:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "Hello World!"Start the Flask development server
Flask's current CLI prefers --app over setting FLASK_APP:
flask --app hello runSample output:
* Serving Flask app 'hello'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit127.0.0.1 is loopback on this machine, so only the host running Flask can reach the server by default. Flask's development server uses TCP port 5000 unless you pass --port.
flask run is for local development only. For production, use a WSGI server such as Gunicorn behind a reverse proxy—see Deploy Flask with Gunicorn and Nginx.
Leave the server running in that terminal and open a second shell for the HTTP check.
Test the application with curl
Request the root URL from another terminal (venv activation is not required for curl):
curl http://127.0.0.1:5000/Hello World!A browser at http://127.0.0.1:5000/ shows the same text. Stop the dev server with Ctrl+C in the server terminal.
To listen on all interfaces for LAN testing on a trusted network:
flask --app hello run --host=0.0.0.00.0.0.0 binds the development server to every network interface on the machine. Use this only on a trusted development network—the Flask development server is not meant to be exposed on the public Internet.
When you are done with the venv for this session:
deactivateContinue building features in Flask Python web app example or add Flask templates with Jinja2 when you need HTML views.
Optional: Install Ubuntu's python3-flask package
When the APT package makes sense
Use apt when you want Ubuntu's dependency set and a single system-wide module—for a quick import test or a lab that standardizes on distro packages. For application development, stay on venv + pip.
Install and verify the Ubuntu package
If you followed the venv steps above, deactivate first. Otherwise python3 and python3 -m flask may still resolve to .venv and you will measure the wrong Flask.
deactivateConfirm you are back on the system interpreter:
command -v python3/usr/bin/python3Install the distro package:
sudo apt updateCheck the candidate version before installing:
apt-cache policy python3-flaskOn Ubuntu 26.04 the lab showed:
python3-flask:
Candidate: 3.1.3-1ubuntu1On 24.04 expect a 3.0.2 base line (for example 3.0.2-1ubuntu1.1 with security revisions); on 22.04 expect 2.0.1 (for example 2.0.1-2ubuntu1.2).
Install the package:
sudo apt install python3-flaskVerify the runtime Flask version and the Debian package revision separately:
python3 -m flask --versionPython 3.14.4
Flask 3.1.3
Werkzeug 3.1.5Compare that to the Debian package revision string:
dpkg-query -W -f='${Version}\n' python3-flask3.1.3-1ubuntu1The dpkg-query line is Ubuntu's package version. python3 -m flask --version reports what Python imports—on 24.04 apt, Flask may read 3.0.2 while PyPI in a venv still gives 3.1.3.
Update Flask
Update Flask inside a virtual environment
Activate the project environment, then upgrade only Flask (not every dependency in a lockfile unless you intend to):
cd ~/flask-app && source .venv/bin/activateUpgrade the Flask wheel only inside that environment:
python -m pip install --upgrade FlaskRe-run the version check to confirm the new release:
python -m flask --versionUpdate Ubuntu's Flask package
Refresh indexes, then upgrade only the distro package:
sudo apt updateRequest an upgrade for the distro Flask package only:
sudo apt install --only-upgrade python3-flaskRoutine Ubuntu security updates may also refresh packaged dependencies such as Werkzeug, so keep applying normal system updates—not only targeted python3-flask upgrades.
Uninstall Flask
Remove the virtual environment
If .venv is active, leave it first:
deactivateDelete only the environment directory when you no longer need that project's dependencies (your hello.py and other code stay unless you remove them yourself). Confirm the path ends in .venv—rm -rf permanently deletes that directory without moving it to Trash:
rm -rf ~/flask-app/.venvRemove the Ubuntu package
Remove the apt package without purging configuration unless you have a reason to:
sudo apt remove python3-flaskTo see which packages autoremove would delete before acting:
sudo apt-get -s autoremoveFor broader apt, Snap, and Flatpak cleanup, see how to remove software on Ubuntu.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
error: externally-managed-environment |
pip install on system Python (PEP 668) |
python3 -m venv .venv, source .venv/bin/activate, python -m pip install Flask |
flask: command not found |
Venv not active or Flask not installed in it | source .venv/bin/activate and python -m pip install Flask |
No module named flask |
Wrong interpreter (system vs venv) | Use python -m flask inside the venv where you installed Flask |
Address already in use on port 5000 |
Another process bound to :5000 |
flask --app hello run --port 5001 or stop the other service |
| Flask version does not match apt package table | Active .venv shadows system Python |
Run deactivate, then command -v python3 and python3 -m flask --version |
Error: Could not locate a Flask application |
Wrong directory or --app module name |
Run from ~/flask-app with flask --app hello run when the file is hello.py |
python3 -m venv fails |
python3-venv not installed |
sudo apt install python3-venv |
| Apt Flask older than a PyPI tutorial | Ubuntu pins python3-flask per release |
Use venv + python -m pip install 'Flask==X.Y.Z' |
pip install Flask with --break-system-packages on Ubuntu unless you deliberately override PEP 668 and accept breakage risk for apt-managed Python tools.
Summary
Installing Flask on Ubuntu starts with python3 -m venv .venv and python -m pip install Flask inside that environment. You get current upstream Flask (3.1.3 today) on every LTS, while Ubuntu's python3-flask package still differs by release—3.1.3 on 26.04, 3.0.2 on 24.04, and 2.0.1 on 22.04. Verification belongs to python -m flask --version with the venv active; apt checks need deactivate first so you are not still importing from .venv.
The Hello World path is a valid hello.py, flask --app hello run, and curl http://127.0.0.1:5000/ for an end-to-end check. Treat the built-in server as development-only; production belongs behind Gunicorn and a reverse proxy in a dedicated deployment guide.
Keep python -m pip install --upgrade Flask scoped to the project venv, and use sudo apt install --only-upgrade python3-flask when you deliberately rely on the distro package. When something fails, PEP 668 and an accidentally active venv explain most "wrong version" and "externally managed" reports.
References
- Flask — Installation
- Flask — Quickstart
- Flask — Command Line Interface
- PEP 668 — Marking Python base environments as externally managed
- Ubuntu — Python development
Frequently Asked Questions
Does Ubuntu 26.04 include Flask?
Ubuntu 26.04 ships python3-flask in main (3.1.3 on current archives). You can sudo apt install python3-flask, but application work should still use a virtual environment and pip so dependencies stay per project.
Does Flask install the same way on Ubuntu 24.04 and 22.04?
Yes. Create .venv, activate it, and python -m pip install Flask on either release. Expect python3-flask from apt to report an older Flask base version (3.0.2 on 24.04, 2.0.1 on 22.04) while PyPI inside the venv installs current Flask such as 3.1.3.
Should I install Flask with pip or APT?
Use pip inside a venv for development and anything that needs extensions or pinned versions. Use apt only for quick distro-managed tests or when you intentionally stay on Ubuntu's package set.
Why does Ubuntu block global pip install Flask?
System Python is externally managed (PEP 668). Global pip installs can break apt-managed tools. Create a venv and install Flask there instead.
Which Python version does Flask require?
Flask 3.1 supports Python 3.9 and newer. Ubuntu 22.04, 24.04, and 26.04 defaults all qualify for a venv install from PyPI.
Can I use flask run in production?
No. The Flask development server is not designed for production security or performance. Use a production WSGI server and a reverse proxy for real deployments.

