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.
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
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:
Flask class from the flask package__name__ tells Flask where to find templates and static files@app.route('/'): registers hello_world as the handler for the root URLhello_world(): returns the string sent to the browser when someone visits /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> The Flask development server is for local testing only. For production, use a WSGI server like <strong>Gunicorn</strong> or <strong>uWSGI</strong> 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.
Opera is one of the most popular cross-platform web browsers in the world, available on Windows,…
Gogs is a free, self-hosted Git service written in Go. It gives you a private GitHub-like…
Elasticsearch is an open-source distributed search and analytics engine built on Apache Lucene. It supports RESTful…
Memcached is a free, open-source, high-performance in-memory caching system. It stores data as key-value pairs in…
TensorFlow is a free, open-source machine learning platform developed by Google. It is used by major…
Minecraft is one of the most popular games ever made — a sandbox game where players…