Python Flask is a lightweight Python web framework that makes it easy to build web applications quickly. It’s a microframework that doesn’t include an ORM (Object Relational Mapper) or such features and is instead extensible through Flask plug-ins.

Advertisement

Flask is easy to get started with and doesn’t require any particular directory structure. A Flask application is a Python script that imports the Flask module creates an instance of the Flask class, and then start the development server using a line of code.

In this article, we’ll show you how to install Flask on Ubuntu 22.04. Also, create a simple Hello World application using the Flask Python module.

Step 1: Installing Python

Before you start, make sure you have Python and pip installed on your system. If you don’t have them already, you can install them by running the following commands:

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

Step 2: Install Flask on Ubuntu

Once Python and pip are installed, you’re ready to install Flask. To do this, you’ll need to open a terminal and enter the following command: `pip3 install Flask`

But we recommend creating Python virtual environment to isolate your application. To do this create and/or switch to your flask application directory:

mkdir flask-app && cd flask-app 

Now, create and activate the virtual environment:

python3 -m venv venv
source venv/bin/activate

The above commands will create a directory with the name “venv” for storing virtual environment files. The second command will activate it. The system command prompt will be modified with the virtual environment name.

Once the virtual environment is activated, you can install Flask and other required Python modules.

pip3 install Flask 

This will install the latest version of Flask and all its dependencies. Once the installation is complete, you can verify that Flask is installed correctly by running the following command:

python3 -m flask --version 

If everything is working correctly, you should see the version number of Flask printed on the terminal.

Step 3: Create a Sample Flask Application

Once Flask is installed, you can start building your web application. To do this, create a new Python script and import the Flask module. Then, create a new Flask app using the following code:

This code creates a simple Flask app that listens for requests at the root URL and returns the message “Hello, World!”. You can run this script by entering the following command:

python3 app.py 

This will start the Flask development server and listen for requests on port 5000. To access your application, open

Step 4: Finishing Your Work

You can create the `requirements.txt` file which is helpful for deploying applications on other systems. This file contains the Python modules required for your application.

pip freeze > requirements.txt

Once you finish your work with this project, You can simply deactivate the virtual environment by running the following command:

deactivate

Conclusion

In conclusion, installing Flask on Ubuntu 22.04 is a straightforward process that involves installing Python and pip, and then using pip to install Flask. Once Flask is installed, you can start building your web application by creating a new Python script and importing the Flask module. Then, create a new Flask app and start the development server using a line of code.

Flask is a lightweight and flexible web framework that is easy to get started with and doesn’t require any particular directory structure. It’s a good choice for small projects and prototyping, and it can be extended with a variety of third-party libraries to add additional functionality. With Flask installed, you’re now ready to start building your web applications on Ubuntu 22.04.

Share.

2 Comments

  1. pip3 install Flask –save

    Usage:
    pip3 install [options] [package-index-options] …
    pip3 install [options] -r [package-index-options] …
    pip3 install [options] [-e] …
    pip3 install [options] [-e] …
    pip3 install [options] …

    no such option: –save

Leave A Reply

Exit mobile version