Cybersecurity Updates & Tools

Install Flask on Ubuntu 18.04: venv Setup and Hello World App

Flask is a free, open-source micro web framework for Python. It is built on Werkzeug and uses Jinja2 as its template engine. Unlike Django, Flask does not include an ORM, form validation, or authentication out of the box. Instead, it relies on extensions to add functionality. Extensions are Python packages that plug into your Flask app to provide things like database access, login handling, or form validation. Because Flask gives you a bare skeleton to build on, there is no magic happening behind the scenes, every piece of your application is explicit and visible in your code.

This guide walks you through installing Flask on Ubuntu 18.04 inside a Python virtual environment, then creates and runs a minimal web application to confirm everything works.

Prerequisite: You need sudo access.

Install Flask on Ubuntu: Set Up a Virtual Environment

Ubuntu 18.04 ships with Python 3.6 by default. Verify it is available:

bashpython3 -V

Install the python3-venv package to enable virtual environment support:

bashsudo apt install python3-venv

Create a project directory and navigate into it:

bashmkdir my_flask_appcd my_flask_app

Create the virtual environment inside the project directory:

bashpython3 -m venv venv

This creates a venv subdirectory containing a private copy of Python, pip, and the standard library. Any packages installed here stay isolated and do not affect other Python projects on the machine. You can create as many virtual environments as you need — one per project — and each one maintains its own independent set of packages.

Activate the virtual environment:

bashsource venv/bin/activate

Your shell prompt changes to (venv) to show the environment is active. Inside a virtual environment, use pip instead of pip3 and python instead of python3.

Install Flask:

bashpip install Flask

Verify the installation:

bashpython -m flask --version

Create a Basic Flask Application

Open a text editor and create a file named hello.py inside the project directory:

pythonfrom flask import Flaskapp = Flask(__name__)@app.route('/')def hello_world():    return 'Hello World!'

Here is what each part does:

  • Line 1: imports the Flask class from the flask package
  • Line 2: creates an app instance; __name__ tells Flask where to find templates and static files
  • @app.route('/'): registers hello_world as the handler for the root URL
  • hello_world(): returns the string sent to the browser when someone visits /

Run the Flask Development Server

Set the FLASK_APP environment variable so Flask knows which file to load, then start the server:

bashexport FLASK_APP=hello.pyflask run

Flask starts a local development server at http://127.0.0.1:5000/. Open that URL in a browser to see the Hello World! message.

<strong>Note:</strong>&nbsp;The Flask development server is for local testing only. For production, use a WSGI server like&nbsp;<strong>Gunicorn</strong>&nbsp;or&nbsp;<strong>uWSGI</strong>&nbsp;behind an Nginx reverse proxy.

Stop the server with CTRL+C. When you are done working, deactivate the virtual environment:

bashdeactivate

To resume work later, navigate back to the project folder and run source venv/bin/activate again.

Flask is now installed on your Ubuntu 18.04 machine. Explore the official Flask documentation to learn about blueprints, request handling, Jinja2 templates, and database integration with Flask-SQLAlchemy. Leave a comment below if you run into any issues.