UbuntuUbuntu Based

How To Install Flask on Ubuntu 24.04 LTS

Install Flask on Ubuntu 24.04

Flask is a lightweight and powerful web framework for Python, widely used for building web applications. Its simplicity and flexibility make it a popular choice among developers. If you’re running Ubuntu 24.04 and want to dive into Flask development, this guide will walk you through the installation process step-by-step. By the end of this article, you’ll have a fully functional Flask application up and running.

Prerequisites

System Requirements

Before starting, ensure your system meets the following requirements:

  • Ubuntu 24.04 installed on your machine.
  • A minimum of 1 GB RAM (2 GB recommended).
  • At least 10 GB of free disk space.

Access Rights

You will need sudo access to install software packages on your system. This allows you to execute commands with administrative privileges.

Check Existing Installations

First, verify if Python and pip are already installed on your system. Open your terminal and run:

python3 --version
pip3 --version

If these commands return version numbers, you can skip the installation steps for Python and pip.

Step 1: Update System Packages

Keeping your system updated is crucial for security and performance. To update your package lists, run the following command:

sudo apt update && sudo apt upgrade

This command fetches the latest package information from the repositories and upgrades any outdated packages on your system.

Step 2: Install Python and Pip

Installing Python

If Python is not already installed, you can install it using the following command:

sudo apt install python3

This command installs Python 3, which is required for running Flask applications.

Installing Pip

Pip is the package manager for Python that allows you to install additional libraries easily. To install pip, use this command:

sudo apt install python3-pip

Once installed, verify the installation by checking the version:

pip3 --version

Step 3: Set Up a Virtual Environment

What is a Virtual Environment?

A virtual environment is an isolated environment in which you can install packages without affecting the global Python installation on your system. This practice helps manage dependencies for different projects effectively.

Installing Virtual Environment Package

You need to install the virtual environment package first. Run:

sudo apt install python3-venv

Creating a Virtual Environment

Create a new directory for your Flask project and navigate into it:

mkdir flask-app && cd flask-app

Create a virtual environment named `venv`:

python3 -m venv venv

Activating the Virtual Environment

To start using the virtual environment, activate it with this command:

source venv/bin/activate

You should see `(venv)` at the beginning of your terminal prompt, indicating that the virtual environment is active. All subsequent installations will be contained within this environment.

Step 4: Install Flask

Using Pip to Install Flask

With your virtual environment activated, you can now install Flask using pip. Run:

pip install Flask

This command downloads and installs Flask along with its dependencies in your virtual environment.

Verifying Flask Installation

To confirm that Flask has been installed correctly, check its version with:

flask --version

Step 5: Create a Simple Flask Application

Setting Up Your First Flask App

Create a new file named `app.py` in your project directory:

touch app.py

Edit `app.py` using your preferred text editor (e.g., nano or vim) and add the following code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, Flask!'

if __name__ == '__main__':
    app.run(debug=True)

Running the Application

You can run your Flask application by executing:

python app.py

This command starts a development server that listens on port 5000 by default.

Accessing the Application

You can access your application by opening a web browser and navigating to http://127.0.0.1:5000. You should see “Hello, Flask!” displayed on the page.

Step 6: Configuring Firewall (if necessary)

Importance of Firewall Configuration

If you’re planning to deploy your application or access it from other devices on your network, you’ll need to configure your firewall settings appropriately.

Allowing HTTP and HTTPS Traffic

If you’re using UFW (Uncomplicated Firewall), allow traffic through HTTP and HTTPS with these commands:

sudo ufw allow 'Nginx Full'
sudo ufw reload

Troubleshooting Tips

  • Error: “ModuleNotFoundError: No module named ‘flask'”: Ensure that you have activated your virtual environment before running your application.
  • Error: “Address already in use”: This indicates that another process is using port 5000. You can either stop that process or run Flask on a different port by modifying the `app.run()` line in `app.py` to include `port=5001` (or any other available port).
  • Error: “Permission denied”: This may occur if you’re trying to bind to a port below 1024 without root permissions. Use ports above 1024 or run as root (not recommended for production).
  • Error while installing packages with pip: If you encounter errors during installation, ensure that pip is up-to-date by running `pip install --upgrade pip` within your virtual environment.

Congratulations! You have successfully installed Flask. Thanks for using this tutorial for installing the Flask framework on Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the official Flask website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button