How to Install Flask on Ubuntu 24.04


There are two major open-source options for building web applications with Python. These two are Django and Flask. Django is a full-featured framework with a structure (MVC model) to streamline development. It includes reusable components and helps you write less code overall.

Meanwhile, the Flask is a minimalist microframework. It’s designed for simple applications with more flexibility. With Flask, you have more control over the main project blocks.

This installation article covers methods for installing Flask on Ubuntu 24.04.

Table of Contents:

How to Install Flask on Ubuntu 24.04

To install Flask on Ubuntu 24.04 you can use the pip. For that, ensure Python3 and pip are installed on Ubuntu 24.04. Follow the given steps for Flask installation using pip on Ubuntu 24.04.

Step 1: Update System

Before you start the Flask installation, first update your system packages:

sudo apt update

Step 2: Installing Python

Flask is written in Python, so you need Python installed on your system to run Flask applications. It acts as the foundation for the framework to function. Similarly, pip should also be available on your Ubuntu system. Flask is by default not available in the Ubuntu repository, so you will need pip to install Flask, which is a Python package.

To install Python3 and pip using one command, run this:

sudo apt install python3 python3-pip python3-venv

To confirm the Python3 installation, run this command:

python3 -V

Step 3: Installing Flask Using Pip and Python

While installing Flask, it’s recommended to create a virtual environment for your Flask project. This isolates the project’s Python packages and dependencies from the system-wide ones. It also prevents conflicts and keeps your project clean.

If you’d prefer to install Flask globally, skip to the next step.

Now create a directory for Flask and navigate to it:

mkdir flask-app && cd flask-app 

Now using Python3, create a virtual environment for Flask:

python3 -m venv flask-venv

Now run this command to activate the virtual environment:

source flask-venv/bin/activate

Note: Here flask-venv is the name for our virtual environment, you can also set any custom name for it.

You will note that on the terminal the prompt will change to flask-venv, this indicates that now you are working inside the virtual environment.

Finally, install Flask using the pip3 command:

pip3 install flask

If you are outside the virtual environment, then run this command for Flask installation:

pip install flask --user

Now wait for a few seconds till the Flask is completely downloaded and installed on your system.

At last, check the Flask version to confirm if it is successfully installed on your system or not:

flask --version

Step 4: Basic Application of Flask

Now let’s create a basic Flask application to test if it’s working. First, you have to create a Python file using any text editor:

nano flasktest.py

You can rename the file with any name you want, simply define it in the above command.

Once the editor is open, simply paste the below code to display the Hello message on the screen:

from flask import Flask

app = Flask(__name__)

@app.route('/')

def hello_world():

 return 'Hello, Flask!'

if __name__ == '__main__':

 app.run()

This code snippet tells Python to import the Flask class. This class is imported from the Flask module. We’ll use this class to create our simple web application. After that, the rest of the code will print the Hello message.

Finally, run the Python file:

python3 flasktest.py

This will give you the local address of your Flask development server. Type the local server address (http://127.0.0.1:5000) in your browser and you will see the message we defined inside the above code.

You can stop the development server by typing Ctrl + C in the terminal.

After you are done with work in the Flask virtual environment, you can exit it by entering this command.

deactivate

This will return you to the normal terminal.

How to Uninstall Flask on Ubuntu 24.04

To uninstall Flask from Ubuntu 24.04 you need to run the same pip3 command that you used for its installation:

pip3 uninstall Flask

Note: Before uninstalling Flask from Ubuntu 24.04 make sure you are inside the virtual environment if you have created it while installing Flask. Otherwise, you can normally run this uninstallation command in the main directory of your system.

Conclusion

Flask is one of the major frameworks for designing Python-based web tools and apps. Unlike Django, Flask is a simple and lightweight microframework. To install Flask, you can use Python3 and pip for its package installation. First, make sure both these are installed, and then create a virtual environment for Flask to work. Once done, you can install Flask using the pip3 install command. After installation, Flask can be accessed by creating any new Python file using any text editor.

Print Friendly, PDF & Email
Categories