
Install Flask on Ubuntu 24.04 to create modern web applications with a lightweight and open-source Python framework. Flask offers core features like routing, request handling, and Jinja2 templating, while allowing developers to enhance functionality through extensions. Its simplicity, scalability, and active community support make it a preferred choice for building applications and APIs, from small prototypes to large-scale solutions.
This article explains how to install Flask on Ubuntu 24.04. You will install all required development packages, set up a virtual environment, and integrate Flask with Gunicorn to run web applications on the server.
You can install Flask on Ubuntu as a Python package using Pip. Python and Pip are pre-installed on Ubuntu by default while Flask works with any active version. Follow the steps below to verify the installed Python version and create a new virtual environment on your server.
Update the server's package index.
View the installed Python version.
Run the following command to install Python and Pip if unavailable.
View the installed Pip version.
Output:
Install the venv Python virtual environment module.
Create a new directory for your Flask project.
Switch to the Flask project directory.
Create a new flaskenv virtual environment.
The above python3 -m venv flaskenv command creates a new isolated flaskenv environment to install and manage Python packages on the server.
Activate the flaskenv virtual environment.
Verify that your shell prompt changes to the flaskenv virtual environment.
Follow the steps below to install Flask in the active flaskenv virtual environment on your server.
Install Flask using Pip.
Output:
To install Flask globally on your server without using a virtual environment, run the following command instead.
View the installed flask version.
Output:
Follow the steps below to create a basic flask application to test your installation.
Create a new flask_app.py Python application file using a text editor such as nano.
Add the following contents to the file.
Save and close the file.
The above Python application listens for connection requests on port 5000 using all server IP addresses 0.0.0.0 and displays a Hello World! Greetings from Vultr! prompt when accessed.
Allow connections to the TCP port 5000 through the default firewall.
Reload UFW to apply the firewall configuration changes.
Run the flask_app.py application using Python.
Output:
Access port 5000 using your server's IP address in a web browser such as Chrome.
Gunicorn is a Python Web Server Gateway Interface (WSGI) used to serve Python web applications running frameworks such as Flask, Django and FastAPI in production environments. Follow the steps below to install Gunicorn and set it up as a system service to serve Flask applications on your server.
Install Gunicorn using Pip.
Output:
Print your working directory and note the Flask project path.
Your output should be similar to the one below.
Create a new gunicorn_conf.py file.
Add the following configurations to the gunicorn_conf.py file. Replace /home/linuxuser/flask_project with your actual project path.
Save and close the file.
The above Gunicorn configuration sets up a new UNIX socket for communication, dynamically allocates workers based on CPU cores and logs debug-level messages to the Flask project directory.
Create a new flask_app.service systemd service file.
Add the following configurations to the file. Replace linuxuser with your actual user and /home/linuxuser/flask_project with your project path.
Save and close the file.
Reload the systemd daemon to apply the service configuration changes.
Enable the flask_app service to start at boot.
Start the flask_app service.
View the flask_app service status and verify that it's running.
Output:
Run the Flask application using Gunicorn.
The Flask application is active and accepts connection requests on the TCP port 5000 using all server interfaces based on your earlier configurations. Follow the steps below to install Nginx and set it up as a reverse proxy to securely handle connection requests using the flask.example.com domain on your server.
Deactivate the flaskenv virtual environment.
Install Nginx.
Create a new flask_app Nginx virtual host configuration in the /etc/nginx/sites-available directory.
Add the following configuration to the file. Replace flask.example.com with your domain and /home/linuxuser/flask_project with your actual project path.
Save and close the file.
Link the virtual host configuration to the /etc/nginx/sites-enabled to enable it.
Test the Nginx configuration for errors.
Output:
Restart Nginx to apply the configuration changes.
View the firewall status.
Remove the Flask application port 5000 rule from the firewall configuration.
Allow the Nginx Full profile through the firewall to enable HTTP and HTTPS connections.
Reload UFW to apply the firewall configuration changes.
Change your user's home directory permissions mode to 775 to enable read privileges for all users on the server. Replace linuxuser with your actual user.
An SSL certificate encrypts the network connection between users and the Flask application server using HTTPS. The Flask web application listens for connection requests using HTTP which is insecure by default. Follow the steps below to generate trusted SSL certificates using Let's Encrypt to secure the flask application on your server.
Install the Certbot Let's Encrypt client using Snap.
Generate a new SSL certificate using your flask.example.com domain. Replace flask.example.com with your domain and user@example.com with your actual email.
Verify that Certbot auto-renews the SSL certificate every 90 days before it expires.
Your output should be similar to the one below.
Restart Nginx to apply the SSL configuration changes.
Access your domain in a new web browser window and verify that your Flask web application displays.
Follow the steps below to uninstall flask on Ubuntu 24.04 if necessary.
Deactivate your project's virtual environment.
Delete the Flask project directory.
Uninstall Flask using Pip if globally installed on the server.
Follow the suggested solutions below to troubleshoot common Flask web application errors.
Check the Gunicorn logs for detailed errors.
If you receive a 502 Bad Gateway error.
Verify that the Flask service is running.
Verify that your user's home directory has atleast 0755 permissions to enable read privileges for all users.
Check the Nginx error logs for additional information about the error.
Incase the application does not return the expected output, verify your application configuration and run the Flask application again.
You have installed Flask on Ubuntu 24.04 and created a basic application to serve the application on your server. In addition, you installed Gunicorn to deploy the application and handle connection requests using Nginx as a reverse proxy server. Combining the Flask framework's simplicity and flexibility with the Ubuntu server's reliability, you can build and deliver scalable production web applications. For more information, visit the Flask documentation.
0 Comments
Be the first to comment and share your perspective with the community.