How To

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.

Cyber Defence

Recent Posts

Install PHP on Ubuntu 26.04: Apache, Nginx, and Multiple Versions

PHP 8.5 is included in Ubuntu 26.04's default repositories and is the recommended version for…

1 day ago

Upgrade to Ubuntu 26.04 from 25.10 and 24.04 LTS: Complete Guide

Ubuntu 26.04 LTS "Resolute Raccoon" arrived on April 23, 2026 with Linux kernel 7.0, GNOME 50,…

1 day ago

Install Kubernetes on Ubuntu 26.04 with kubeadm and containerd

Kubernetes is the standard platform for running containerized workloads across multiple servers with self-healing, rolling…

1 day ago

Install Ubuntu 26.04: Bootable USB, Partitioning, and First Steps

Ubuntu 26.04 LTS "Resolute Raccoon" was released on April 23, 2026 with Linux kernel 7.0, GNOME desktop, and standard security support until April 2031. A clean install gives you a known-good starting point on new hardware, when replacing another operating system, or when an upgrade path is not practical. This guide walks through how to install Ubuntu 26.04: downloading and verifying the ISO, writing a bootable USB drive, completing the installer, and doing the initial setup after the first boot. Before you start: You need a USB drive with at least 12 GB of free space. Back up any existing data on the target machine — the installer can erase the entire disk. Install Ubuntu 26.04: Download the ISO…

1 day ago

Change Timezone on Ubuntu: timedatectl and Desktop GUI Guide

The correct timezone affects more than the clock on your screen. It drives cron job scheduling, systemd timer execution, log file timestamps, database record timing, and SSL certificate validity checks. A mismatched timezone can cause scheduled jobs to fire at the wrong hour and make log timestamps impossible to match with real-world events. This guide shows how to change timezone on Ubuntu using the timedatectl command (the recommended approach for servers and remote machines) and through the graphical Date & Time settings on desktop systems. The steps apply to all current Ubuntu releases including 24.04 and 26.04. <strong>Prerequisite:</strong>&nbsp;Only&nbsp;the&nbsp;root&nbsp;user&nbsp;or&nbsp;a&nbsp;user&nbsp;with&nbsp;sudo&nbsp;access&nbsp;can&nbsp;change&nbsp;the&nbsp;system&nbsp;timezone. Check the Current Timezone Before You Change It Run timedatectl with no arguments to see the active timezone and clock status: bashtimedatectl Sample output: Local…

1 day ago

Install Atom on Ubuntu 18.04: GitHub’s Code Editor APT Setup

Atom is a free, open-source, cross-platform code editor developed by GitHub. Built on Electron, it uses…

2 days ago