{"id":3616,"date":"2021-01-19T21:01:31","date_gmt":"2021-01-19T21:01:31","guid":{"rendered":"https:\/\/linuxways.net\/?p=3616"},"modified":"2021-01-20T08:43:51","modified_gmt":"2021-01-20T08:43:51","slug":"how-to-install-flask-on-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-install-flask-on-ubuntu-20-04\/","title":{"rendered":"How to Install Flask on Ubuntu 20.04"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>Flask is one of the most well-known web frameworks used by many developers in the world. It is written in Python and designed to help developers build secure and scalable web applications.<\/p>\n<p>Flask is known as a micro-framework because it doesn\u2019t require any particular third-party libraries and tools. However, Flask can support extensions that are implemented in Flask itself to add features into a Flask application.<\/p>\n<p>This article will show you the way to install the Flask framework on your Ubuntu 20.04 machine.<\/p>\n<h2>Installing Flask<\/h2>\n<p>In order to install Flask on Ubuntu 20.04, let\u2019s update the system first with the following command:<\/p>\n<pre>$ sudo apt update<\/pre>\n<p>Once the update is complete, go ahead to the next steps.<\/p>\n<p>By default, Ubuntu 20.04 comes with Python 3.8. You can verify this by the following command:<\/p>\n<pre>$ python3 -V<\/pre>\n<p>Next, you have to install python3-venv package to create a virtual environment for the Flask application:<\/p>\n<pre>$ sudo apt install python3-venv<\/pre>\n<p>After the package is installed, let\u2019s create a virtual environment for the Flask application.<\/p>\n<p>It is recommended to create a new directory for the application and navigate into it:<\/p>\n<pre>$ mkdir flask-dir &amp;&amp; cd flask-dir<\/pre>\n<p>Now, let\u2019s run the following command in flask-dir to create the virtual environment:<\/p>\n<pre>$ python3 -m venv venv<\/pre>\n<p>The command creates a directory named venv in flask-dir directory. In order use the virtual environment, you have to activate it as follows:<\/p>\n<pre>$ source venv\/bin\/activate<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1536\" height=\"268\" class=\"wp-image-3617\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-313.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-313.png 1536w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-313-300x52.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-313-1024x179.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-313-768x134.png 768w\" sizes=\"auto, (max-width: 1536px) 100vw, 1536px\" \/><\/p>\n<p>Once the virtual environment is activated, you can install Flask using the Python package manager pip:<\/p>\n<pre>(venv) $ pip install Flask<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"417\" class=\"wp-image-3618\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-314.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-314.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-314-300x163.png 300w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/p>\n<p>Verify that the Flask is successfully installed by running the following command:<\/p>\n<pre>(venv) $ python -m flask --version<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1536\" height=\"234\" class=\"wp-image-3619\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-315.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-315.png 1536w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-315-300x46.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-315-1024x156.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-315-768x117.png 768w\" sizes=\"auto, (max-width: 1536px) 100vw, 1536px\" \/><\/p>\n<p>Congratulations, now you can create some Flask applications on your Ubuntu 20.04.<\/p>\n<h2>Creating a simple application<\/h2>\n<p>In this section, we will create a simple \u201cHello world\u201d application with Flask. Using your favorite editor to create a Python file named as: hello.py in flash-dir:<\/p>\n<pre><strong>from<\/strong> <strong>flask<\/strong> <strong>import<\/strong> Flask\n\napp = Flask(__name__)\n\n<strong>@app.route<\/strong>('\/')\n\n<strong>def<\/strong> hello_world():\n\n<strong>return<\/strong> 'Hello world'<\/pre>\n<p>In your virtual environment, run the following commands:<\/p>\n<pre>(venv) $ export FLASK_APP=hello.py<\/pre>\n<pre>(venv) $ flask run<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1536\" height=\"394\" class=\"wp-image-3620\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-316.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-316.png 1536w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-316-300x77.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-316-1024x263.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-316-768x197.png 768w\" sizes=\"auto, (max-width: 1536px) 100vw, 1536px\" \/><\/p>\n<p>Using your web browser or curl command to hit <a href=\"http:\/\/127.0.0.1:5000\">http:\/\/127.0.0.1:5000<\/a>, you will get the \u201cHello world\u201d text output as shown in the below screenshot.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-3644 size-large\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/image-4-1024x212.png\" alt=\"Flask Output in Web Browser\" width=\"1020\" height=\"211\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/image-4-1024x212.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/image-4-300x62.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/image-4-768x159.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/image-4.png 1536w\" sizes=\"auto, (max-width: 1020px) 100vw, 1020px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>Flask is a really powerful web framework for any developer. This tutorial went through all the detailed steps of installing Flask on Ubuntu 20.04.<\/p>\n<p>Thank you and feel free to feedback in the below comment section.<\/p>","protected":false},"excerpt":{"rendered":"<p>Introduction Flask is one of the most well-known web frameworks used by many developers in the world. It is written in Python and designed to help developers build&hellip;<\/p>","protected":false},"author":23,"featured_media":3621,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[147,100],"class_list":["post-3616","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-flask","tag-ubuntu-20-04"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/3616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/23"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=3616"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/3616\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/3621"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=3616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=3616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=3616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}