Flask is an open-source web framework built with Python programming language. The software is a minimalistic, light, fast framework that doesn’t include ORM, form validation, and other third-party libs. Flash ships with only the essential tools to develop your web applications and maintain them compared to Django, another popular web framework development software. Flask is based on Werkzeug and uses Jinja2 as a template engine.
The tutorial will teach you how to install Flask with the recommended way of using a virtual environment on Ubuntu 22.04 LTS Jammy Jellyfish using the command line terminal.
Table of Contents
Update Ubuntu
First, make sure your Ubuntu system is up to date. You will need root access or sudo privileges for the installation.
sudo apt update && sudo apt upgrade -y
Install Required Packages
To successfully install the Flask framework, you must ensure the default Python package is installed on your system. Run the following command to either install or return an already installed message.
sudo apt install python3
Install PIP
You will need to install PIP for Python, enabling you to create virtual environments. As stated, you will install Flask most commonly in a virtual environment.
sudo apt install build-essential python3-pip libffi-dev python3-dev python3-setuptools libssl-dev -y
Alternatively, use the Python PPA for the latest versions of Python.
sudo add-apt-repository ppa:deadsnakes/ppa -y
Now run an APT update.
sudo apt update
If you have already installed the packages, you can upgrade or use the installation command above to install the packages.
Install Virtual Environment
Now, you can install the virtual environment to isolate and run Flask; this is optional but recommended.
sudo apt install python3-venv -y
Create Flask Location Directory
Once the virtual environment is installed, create a flask direct and open the created directory using the following command.
mkdir -p ~/projects/flask && cd ~/projects/flask
This is just an example; feel free to modify it if required.
Create Virtual Environment
In the directory, you now need to run the command to create the virtual environment as follows.
python3 -m venv venv
Now, activate the virtual environment.
source venv/bin/activate
Once activated, you will notice the terminal command line, which now has “venv” to indicate that you are currently working in the virtual environment that has been created.
Install Flask
The final stage is installing the actual Flask software in your virtual environment. You do this with Pip3, which will install all the components needed for Flask, such as Jinja2, Werkzeug WSG web application library, and its modules.
pip3 install flask
Now, confirm the version of Flask to make sure the installation has been completed correctly.
python -m flask --version
Example output:
Congratulations, you have successfully installed Flask.
Comments and Conclusion
The tutorial has shown you how to set up a Python virtual environment and install Flask. The Flask documentation is the best start to visit to learn how to create and deploy your Python applications using Flask for developers needing documentation.