Install Flask on Ubuntu: 4 Easy Steps and 1 Common Error

To install Flask on Ubuntu today, create a virtual environment and run pip install flask inside it. The obvious shortcut, pip3 install flask system-wide, now fails with an error on current Ubuntu releases. I tested the whole path on 22 November 2026 in clean Ubuntu 24.04.5 LTS (Python 3.12.3) and Ubuntu 26.04.1 LTS (Python 3.14.4) containers: the pip error, the apt package, the virtual environment, the development server and a production server with Gunicorn.

Step 1: install Python tools

sudo apt update
sudo apt install python3 python3-pip python3-venv

Both releases ship Python 3 already; python3-venv is the package that matters, because without it python3 -m venv cannot create environments with pip inside.

$ python3 --version; pip3 --version        # Ubuntu 24.04
Python 3.12.3
pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12)

Why "pip3 install flask" fails on Ubuntu

Install Flask on Ubuntu 24.04: pip3 install flask fails with externally-managed-environment, the venv install succeeds
Lab: the system-wide pip error and the working virtual environment install.
$ pip3 install flask
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.

This is the first thing that goes wrong when people install Flask on a new server. Ubuntu marks its system Python as managed by apt, so pip refuses to write into it. Same result on 24.04 and 26.04. You have two clean options: the apt package, or a virtual environment. Do not use --break-system-packages to force it; the name says what it does.

Option A: install Flask on Ubuntu with apt

$ apt-cache policy python3-flask | head -3
python3-flask:
  Installed: (none)
  Candidate: 3.0.2-1ubuntu1.1          # Ubuntu 24.04 (26.04: 3.1.3-1ubuntu1)
$ sudo apt install python3-flask
$ python3 -c "import flask, importlib.metadata as m; print(\"apt flask\", m.version(\"flask\"))"
apt flask 3.0.2

Good for a small tool on one server that should get security updates with the rest of the system. The catch: you get the version Ubuntu chose (3.0.2 on 24.04, while pip offered 3.1.3 the same day), and every app on the machine shares it.

$ mkdir -p /srv/hello && cd /srv/hello
$ python3 -m venv venv && . venv/bin/activate && pip install -q flask && flask --version
Python 3.12.3
Flask 3.1.3
Werkzeug 3.1.9
$ which python flask
/srv/hello/venv/bin/python
/srv/hello/venv/bin/flask

Flask pulled in its six dependencies: blinker 1.9.0, click 8.5.1, itsdangerous 2.2.0, Jinja2 3.1.6, MarkupSafe 3.0.4 and Werkzeug 3.1.9. Each project gets its own copy and its own versions, and nothing touches the system Python. Freeze them with pip freeze > requirements.txt so the next install is identical. On Ubuntu 26.04 the same commands gave the same Flask 3.1.3, on Python 3.14.4.

Step 3: run a first app with the development server

A minimal app.py:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, Flask on Ubuntu!"

Start it with flask --app app run (inside a folder that contains app.py, plain flask run finds it too):

 * Serving Flask app 'app'
 * 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

Two things trip people up here. First, the server listens on 127.0.0.1 only: from the same machine curl got the page, from the container's own network address the connection failed (curl exit code 7). To reach it from another machine during development, add --host 0.0.0.0:

$ flask --app app run --host 0.0.0.0 --port 8000 --debug
 * Debug mode: on
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:8000
 * Running on http://10.88.0.12:8000
 * Restarting with stat

Second, never combine --host 0.0.0.0 with --debug on a network you do not trust. Debug mode turns on the interactive debugger, and anyone who can reach the port can reach it.

Step 4: production with Gunicorn

The Flask documentation on deploying to production says the built-in development server, debugger and reloader "should not be used in production" and recommends a dedicated WSGI server. Gunicorn installs in the same virtual environment:

$ pip install -q gunicorn && gunicorn --version
gunicorn (version 27.0.1)
$ gunicorn -w 2 -b 127.0.0.1:8001 app:app
[INFO] Starting gunicorn 27.0.1
[INFO] Listening at: http://127.0.0.1:8001 (3714)
[INFO] Using worker: sync
[INFO] Booting worker with pid: 3716
[INFO] Booting worker with pid: 3717
$ curl -s http://127.0.0.1:8001/
Hello, Flask on Ubuntu!

app:app means "module app, object app". Keep Gunicorn on localhost, put nginx in front for TLS and static files, and run it under systemd so it restarts after a reboot.

Errors you will meet when you install Flask

I ran a second, deliberately careless session on a fresh Ubuntu 24.04 container to collect the usual messages. Forgetting python3-venv:

$ python3 -m venv venv
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt install python3.12-venv

Installing Flask into the environment but not activating it:

$ flask --version
bash: line 1: flask: command not found

Activate with . venv/bin/activate, or call venv/bin/flask directly, which is also what you should put in a systemd unit. Running flask run in a folder without an app:

Error: Could not locate a Flask application. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi.py' or 'app.py' file in the current directory.

And starting a second server while the first still runs:

Address already in use
Port 5000 is in use by another program. Either identify and stop that program, or start the server with a different port.

Use --port, or find the old process with ss -ltnp | grep 5000 and stop it.

Which way to install Flask on Ubuntu

apt (python3-flask)virtual environment + pip
Flask version (2026-11-22)3.0.2 on 24.04, 3.1.3 on 26.043.1.3 on both
Updateswith apt, security patches by Ubuntuyou run pip, you choose when
Several apps, different versionsnoyes
Works with pip-only extensionsmixing is fragileyes

For anything you develop yourself, use the virtual environment. If you need a newer Python than the one Ubuntu ships, see installing Python 3.13 on Ubuntu, and for containers in the cloud, deploying Flask to AWS Fargate. The official Flask installation guide also recommends a virtual environment for every project.

Comments

Popular posts from this blog

Linux Desktop Frozen? 5 Proven Fixes Before a Hard Reboot

Minecraft Bedrock on Linux: 4 Proven Ways to Play in 2026

DXVK in Wine on Linux: 7 Easy Setup Steps, Tested