How to Install Flask on Ubuntu

Deepak Prasad
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
Short on time?Beta Get the gist of this 13-minute read in seconds, then ask follow-up questions

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:

bash
sudo apt update

Install Python and the module that provides python3 -m venv (minimal images may need both):

bash
sudo apt install python3 python3-venv

Create a project directory and an isolated environment named .venv:

bash
mkdir -p ~/flask-app && cd ~/flask-app

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

bash
python3 -m venv .venv

Load the venv's bin/ directory onto your PATH for this shell:

bash
source .venv/bin/activate

pip is Python's package installer; inside the virtual environment it installs Flask only for this project (from PyPI, the Python Package Index):

bash
python -m pip install Flask

Confirm the CLI sees the venv copy of Flask:

bash
python -m flask --version

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

  • python3 refers to Ubuntu's system Python before the venv is activated.
  • python refers to the venv's Python after activation (Ubuntu normally invokes the system interpreter as python3; once the venv is active, python points at the environment's interpreter).
  • pip installs 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 python or pip.

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:


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:

bash
python3 --version

Sample output on the 26.04 lab:

output
Python 3.14.4

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

bash
sudo apt install python3 python3-venv

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

bash
mkdir -p ~/flask-app && cd ~/flask-app

Create and activate .venv

Flask's docs use a hidden .venv directory—easy to add to .gitignore and visually separate from application files:

bash
python3 -m venv .venv

Activate it for the current shell session:

bash
source .venv/bin/activate

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

bash
command -v python
output
/home/user/flask-app/.venv/bin/python

The pip executable should sit beside that Python:

bash
command -v pip
output
/home/user/flask-app/.venv/bin/pip

If 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):

bash
python -m pip install Flask

Sample output (versions of dependencies may differ slightly):

output
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.9

Pin a specific release when a tutorial requires it:

bash
python -m pip install 'Flask==3.1.3'

Verify the installed Flask version

Check which Python, Flask, and Werkzeug versions this environment is using:

bash
python -m flask --version
output
Python 3.14.4
Flask 3.1.3
Werkzeug 3.1.9

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

python
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "Hello World!"
Output

Start the Flask development server

Flask's current CLI prefers --app over setting FLASK_APP:

bash
flask --app hello run

Sample output:

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 quit

127.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):

bash
curl http://127.0.0.1:5000/
output
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:

bash
flask --app hello run --host=0.0.0.0

0.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:

bash
deactivate

Continue 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.

bash
deactivate

Confirm you are back on the system interpreter:

bash
command -v python3
output
/usr/bin/python3

Install the distro package:

bash
sudo apt update

Check the candidate version before installing:

bash
apt-cache policy python3-flask

On Ubuntu 26.04 the lab showed:

output
python3-flask:
  Candidate: 3.1.3-1ubuntu1

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

bash
sudo apt install python3-flask

Verify the runtime Flask version and the Debian package revision separately:

bash
python3 -m flask --version
output
Python 3.14.4
Flask 3.1.3
Werkzeug 3.1.5

Compare that to the Debian package revision string:

bash
dpkg-query -W -f='${Version}\n' python3-flask
output
3.1.3-1ubuntu1

The 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):

bash
cd ~/flask-app && source .venv/bin/activate

Upgrade the Flask wheel only inside that environment:

bash
python -m pip install --upgrade Flask

Re-run the version check to confirm the new release:

bash
python -m flask --version

Update Ubuntu's Flask package

Refresh indexes, then upgrade only the distro package:

bash
sudo apt update

Request an upgrade for the distro Flask package only:

bash
sudo apt install --only-upgrade python3-flask

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

bash
deactivate

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

bash
rm -rf ~/flask-app/.venv

Remove the Ubuntu package

Remove the apt package without purging configuration unless you have a reason to:

bash
sudo apt remove python3-flask

To see which packages autoremove would delete before acting:

bash
sudo apt-get -s autoremove

For 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'
WARNING
Do not run 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


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.

Finished reading?Beta Get the key takeaways in seconds, then ask follow-up questions
Omer Cakmak

Linux Administrator

Highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware.

  • Debian
  • Ubuntu
  • Linux
  • Red Hat Enterprise Linux
  • Shell Script
  • System Administration