{"id":375,"date":"2025-03-07T06:36:16","date_gmt":"2025-03-07T06:36:16","guid":{"rendered":"https:\/\/blog.hostinger.io\/support\/2025\/03\/07\/10725412-how-to-install-flask-on-ubuntu-24-04\/"},"modified":"2026-03-16T12:11:58","modified_gmt":"2026-03-16T12:11:58","slug":"10725412-how-to-install-flask-on-ubuntu-24-04","status":"publish","type":"post","link":"https:\/\/www.hostinger.com\/support\/10725412-how-to-install-flask-on-ubuntu-24-04\/","title":{"rendered":"How to Install Flask on Ubuntu 24.04"},"content":{"rendered":"<p class=\"no-margin\">Flask is a lightweight, open-source web framework for Python, designed to help developers build modern web applications with ease. It provides essential features like routing, request handling, and Jinja2 templating, while allowing flexibility through extensions. With its simplicity, scalability, and strong community support, Flask is a popular choice for developing applications and APIs, from small prototypes to large-scale projects.<\/p><p class=\"no-margin\">\n<\/p><p class=\"no-margin\">This guide walks you through the process of installing Flask with Gunicorn and Nginx on Ubuntu 24.04 VPS.<\/p><p class=\"no-margin\">\n<\/p><h2 id=\"h_ad6b6f2ad2\">Step 1 &ndash; Connect to your <a href=\"\/support\/5723772-how-to-connect-to-your-vps-via-ssh\" target=\"_blank\" class=\"intercom-content-link\">VPS via SSH<\/a><br><\/h2><h2 id=\"h_dd871f964a\">Step 2 &ndash; Update your package list and installed packages<\/h2><p class=\"no-margin\">Run the following commands:<br>&#8203;<\/p><pre><code>sudo apt update<br>sudo apt upgrade -y<\/code><\/pre><p class=\"no-margin\">\n<\/p><h2 id=\"h_02212eb688\">Step 3 &ndash; Set up Flask application<\/h2><p class=\"no-margin\">A few essential components need to be installed, including Python, venv (the Python virtual environment module), pip (for managing Python packages), and Nginx. Run the following command to install the required packages:<br>&#8203;<\/p><pre><code>sudo apt install python3 python3-pip python3-venv nginx -y<\/code><\/pre><p class=\"no-margin\">\n<\/p><h2 id=\"h_b170b129b0\">Step 4 &ndash; Create a Project Directory<\/h2><p class=\"no-margin\">Navigate to your home directory and set up a new folder by running the commands:<br>&#8203;<\/p><pre><code>mkdir \/home\/user\/flaskapp  <br>cd \/home\/user\/flaskapp<\/code><\/pre><p class=\"no-margin\">\n<\/p><h2 id=\"h_34b86622c0\">Step 5 &ndash; Set up a Virtual Environment<\/h2><p class=\"no-margin\">By using a virtual environment helps manage your Python dependencies efficiently. Run the following commands to create and activate one<br>&#8203;<\/p><pre><code>python3 -m venv flaskappenv  <br>source flaskappenv\/bin\/activate  <\/code><\/pre><p class=\"no-margin\">\n<\/p><h2 id=\"h_04d367b76d\">Step 6 &ndash; Install Flask<\/h2><p class=\"no-margin\">With the virtual environment activated, install Flask along with Gunicorn for running the application:<br>&#8203;<\/p><pre><code>pip install Flask gunicorn  <\/code><\/pre><p class=\"no-margin\">\n<\/p><h2 id=\"h_1ae16f0695\">Step 7 &ndash; Create a basic Flask application<\/h2><p class=\"no-margin\">In your project directory, create a new file named app.py<br>&#8203;<\/p><pre><code>nano flaskapp.py<\/code><\/pre><p class=\"no-margin\">then add the following code:<br>&#8203;<\/p><pre><code>from flask import Flask<br><br>app = Flask(__name__)<br><br>@app.route('\/')<br>def hello_world():<br>    return 'Hello, Flask!'<br><br>if __name__ == '__main__':<br>    app.run(host='0.0.0.0', port=8000)<\/code><\/pre><p class=\"no-margin\">Run the flaskapp.py application using Python:<br>&#8203;<\/p><pre><code>python3 flaskapp.py<\/code><\/pre><p class=\"no-margin\">An output confirming that the server is running should appear. Next, open your web browser and navigate to http:\/\/your_server_ip:8000. If the setup is correct, &ldquo;<b>Hello, Flask!<\/b>&rdquo; will be displayed on the page.<br>&#8203;<\/p><h2 id=\"h_890bb8c37e\">Step 8 &ndash; Create the WSGI entry point<\/h2><p class=\"no-margin\">This will tell the Gunicorn server how to interact with the application, create a new file called wsgi.py<br>&#8203;<\/p><pre><code>nano wsgi.py<\/code><\/pre><p class=\"no-margin\">Then add the following configuration to it:<br>&#8203;<\/p><pre><code>from flaskapp import app<br><br>if __name__ == \"__main__\":<br>    app.run()<\/code><\/pre><p class=\"no-margin\">\n<\/p><h2 id=\"h_cbfec50463\">Step 9 &ndash; Configure Gunicorn<\/h2><p class=\"no-margin\">Once the virtual environment setup is complete, deactivate it by running the following command:<br>&#8203;<\/p><pre><code>deactivate<\/code><\/pre><p class=\"no-margin\">This will return you to the system&rsquo;s default Python environment.<\/p><p class=\"no-margin\">\n<\/p><p class=\"no-margin\">Next, create a systemd service unit file to allow Ubuntu&rsquo;s init system to automatically start Gunicorn and serve the Flask application each time the server boots up.<\/p><p class=\"no-margin\">\n<\/p><p class=\"no-margin\">To begin, create a unit file with a .service extension in the \/etc\/systemd\/system directory.<br>&#8203;<\/p><pre><code>sudo nano \/etc\/systemd\/system\/flaskapp.service<\/code><\/pre><p class=\"no-margin\">Then add the following (replace <b>user <\/b>with your actual user, and paths<b> \/home\/user\/flaskapp <\/b>with your actual project path):<br>&#8203;<\/p><pre><code>[Unit]<br>Description=Gunicorn instance to serve myproject<br>After=network.target<br><br>[Service]<br>User=user<br>Group=www-data<br>WorkingDirectory=\/home\/user\/flaskapp<br>Environment=\"PATH=\/home\/user\/flaskapp\/flaskappenv\/bin\"<br>ExecStart=\/home\/user\/flaskapp\/flaskappenv\/bin\/gunicorn --workers 3 --bind unix:flaskapp.sock -m 007 wsgi:app<br><br>[Install]<br>WantedBy=multi-user.target<\/code><\/pre><p class=\"no-margin\">You can now start the Gunicorn service that was created and enable it to start at boot. To check the service status, use the following command:<br>&#8203;<\/p><pre><code>sudo systemctl start flaskapp<br>sudo systemctl enable flaskapp<\/code><\/pre><p class=\"no-margin\">and check the service status:<br>&#8203;<\/p><pre><code>sudo systemctl status flaskapp<\/code><\/pre><p class=\"no-margin\">If any errors are visible in the service status, they should be resolved before continuing with the tutorial.<br>&#8203;<\/p><h2 id=\"h_569a60ec46\">Step 10 &ndash; Configure Nginx<\/h2><p class=\"no-margin\">The Gunicorn application server should now be running and listening for requests through the socket file in the project directory. Next, configure Nginx to forward web requests to this socket by modifying its configuration.<\/p><p class=\"no-margin\">\n<\/p><p class=\"no-margin\">Start by creating a new server block configuration file in Nginx&rsquo;s <code>sites-available<\/code> directory. Name the file <code>flaskapp<\/code> to maintain consistency with the rest of the tutorial.<br>&#8203;<\/p><pre><code>sudo nano \/etc\/nginx\/sites-available\/flaskapp<\/code><\/pre><p class=\"no-margin\">Then add the following command (Make sure to replace your_domain and path to the correct ones for your project)<br>&#8203;<\/p><pre><code>server {<br>    listen 80;<br>    server_name your_domain www.your_domain;<br><br>    location \/ {<br>        include proxy_params;<br>        proxy_pass http:\/\/unix:\/home\/user\/flaskapp\/flaskapp.sock;<br>    }<br>}<\/code><\/pre><p class=\"no-margin\">To enable the Nginx server block configuration you&rsquo;ve just created, link the file to the sites-enabled directory:<br>&#8203;<\/p><pre><code>sudo ln -s \/etc\/nginx\/sites-available\/flaskapp \/etc\/nginx\/sites-enabled<\/code><\/pre><p class=\"no-margin\">With the file in that directory, test for syntax errors:<br>&#8203;<\/p><pre><code>sudo nginx -t<\/code><\/pre><p class=\"no-margin\">If no issues are reported, restart the Nginx process to apply the new configuration using the following command:<br>&#8203;<\/p><pre><code>sudo systemctl restart nginx<\/code><\/pre><p class=\"no-margin\">Finally, adjust the firewall, allowing full access to the Nginx server:<br>&#8203;<\/p><pre><code>sudo ufw allow 'Nginx Full'<\/code><\/pre><p class=\"no-margin\">That&rsquo;s it! You should now be able to navigate to your server&rsquo;s domain name in your web browser! &#128640; <\/p><p class=\"no-margin\">\n<\/p><p class=\"no-margin\">A Flask application was set up, Gunicorn was configured as the WSGI server, and a systemd service was created for automatic startup. Additionally, Nginx was set up to relay web traffic. This flexible stack can now be used to deploy Flask applications on your VPS.<\/p><p class=\"no-margin\">\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Installing Flask application on Ubuntu 24.04<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"include_on_kodee":true,"footnotes":""},"categories":[199],"tags":[],"class_list":["post-375","post","type-post","status-publish","format-standard","hentry","category-vps-management"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/support\/10725412-how-to-install-flask-on-ubuntu-24-04\/","default":1}],"include_on_kodee":true,"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/posts\/375","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/comments?post=375"}],"version-history":[{"count":0,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/posts\/375\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/media?parent=375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/categories?post=375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/support\/wp-json\/wp\/v2\/tags?post=375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}