How to Install Flask on Debian 12

how to install flask on debian 12

Flask is a lightweight micro web framework for Python. Its simplicity and flexibility make it an excellent choice for building anything from small personal projects to complex, production-ready web applications. In this guide, we’ll walk you through the process of how to install Flask on Debian 12.

Flask depends on the Werkzeug WSGI toolkit, the Jinja template engine, and the Click CLI toolkit. Whether you’re a seasoned developer or just starting your web development journey, we’ll provide you with clear and easy-to-follow steps to ensure you have Flask up and running in no time.

Prerequisites

  • SSH access to the root user or a regular system user with sudo privileges

Step 1. Log in via SSH

Let’s log in to your Debian 12 VPS with SSH. You can log in as a root user, or as a regular user with sudo privileges.

ssh root@IP_Address -p Port_number

If you cannot log in as root, remember to replace “root” with a user that has sudo privileges. Additionally, replace the “IP_Address” and “Port_Number” with your server’s respective IP address and SSH port.

Once logged in, update the package repository files on the system with the command:

# apt update

Step 2. Install Python Packages and Create a Virtual Environment

Debian 12 comes with Python version 3.11 installed by default – you can check this with the command:

# python --version
Python 3.11.2

Now you can install the required packages python3-pip and python3-venv with the command:

# apt install python3-venv python3-pip

You can install Flask globally with the command pip3 install flask, but it’s recommended to create a virtual environment and install the Flask application there.

Let’s create a new user and install the Flask application in a new virtual environment:

#  adduser john

Now, you can log in as the user john with the above command. After logging in, create the directories flaskapp/templates:

# su - john
$ mkdir -p flaskapp/templates

Step 3. Install Flask on Debian 12

Enter the directory called flaskapp – you can now go ahead and create the virtual environment.

$ cd flaskapp
$ python3 -m venv virtualenv

Now enter the virtual environment with:

# source virtualenv/bin/activate

You have now activated the virtual environment, where we can start our installation. Let’s install Flask and Gunicorn:

(virtualenv) john@debian12$ pip3 install flask gunicorn

After the installation is completed, you should receive a similar output:

Successfully installed Jinja2-3.1.2 MarkupSafe-2.1.3 Werkzeug-3.0.0 blinker-1.6.3 click-8.1.7 flask-3.0.0 gunicorn-21.2.0 itsdangerous-2.1.2 packaging-23.2

Running a Simple Flask Application

Once Flask is installed, you can run a simple application to test if everything is working as expected. Make sure you are logged in as the user “john”. Create an app.py file using your preferred text editor:

(virtualenv) john@debian12 ~/flas$ nano simpleapp.py 

Add the following code to the file:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello, World!'

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

Save and close the file. You can now run this example program:

flask run & 

The application will continue running in the background listening on port 5000.

You can check the output of the program with this quick command:

curl http://127.0.0.1:5000

You should get the following output:

127.0.0.1 - - [13/Oct/2023 08:14:17] "GET / HTTP/1.1" 200 -
Hello, World!

Congratulations! You have successfully installed Flask on Debian 12. You can now use Flask to run your programs and develop software.

Of course, you don’t have to know how to install Flask on Debian 12 if you use one of our Debian VPS Hosting services, in which case you can simply ask our expert Linux admins to set up Flask on Debian 12 VPS for you. They are available 24/7 and will take care of your request immediately.

PS. If you liked this post on how to set up Flask on Debian 12, please share it with your friends on social networks or simply leave a reply in the comments section. Thanks.

Leave a Comment