29.8 C
Texas
angeloma
Senior Writer and partner

Install Flask on Ubuntu 20.04

Hello, friends. Python application development is at a fairly high point of popularity. This extends to all kinds of applications both web and desktop. Speaking of the web, today we will show you how to install Flask on Ubuntu 20.04 which is a microframework for making web applications with Python.

Now I will give you a brief introduction

Flask is a microframework for web applications created in Python. The “micro” in microframework means Flask aims to keep the core simple but extensible.

So that’s why this framework becomes very popular for developers who want to have full control of what happens in their application and the process.

- Advertisement -

So, today we are going to install it without problems on Ubuntu 20.04

Install Flask on Ubuntu 20.04

Before you start make sure that the whole system is up to date. To do this run in a terminal:

sudo apt update
sudo apt upgrade

Now, install python along with its dependencies as well as the package needed to create a virtual environment. This way the installation will be more secure.

sudo apt install python3 python3-venv
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  python-pip-whl python3.8-venv
The following NEW packages will be installed:
  python-pip-whl python3-venv python3.8-venv
0 upgraded, 3 newly installed, 0 to remove and 19 not upgraded.
Need to get 1,812 kB of archives.
After this operation, 2,350 kB of additional disk space will be used.
Do you want to continue? [Y/n]

Now create a new folder where your Flask project will be located. You can name it whatever you want but I will choose project.

mkdir project
cd project

Once in the folder, you need to create the virtual environment with the following command

python3 -m venv venv

Now, activate the virtual environment

source venv/bin/activate

You will see that the prompt changes and now proceed to install Flask using PIP

pip install Flask

After the process finishes you will be able to check the installed version with this command

python -m flask --version
Python 3.8.10
Flask 2.0.1
Werkzeug 2.0.1

Testing the Flask installation

As I always say, if we don’t test the installation then we won’t know if the process is correct.

To do this, create a file with some code. I will call it helloworld.py.

nano helloworld.py

Now, add the following code

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World! welcome to osradar.com'

Save the changes and close the editor.

Now, export the FLASK_APP variable.

export FLASK_APP=helloworld.py

Now, if you are using Flask on a local computer, you can test it with the following command

flask run
 * Serving Flask app 'helloworld.py' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Now open your web browser, and go to http://127.0.0.1 and you will see the following

1.- Flask running on Ubuntu 20.04
1.- Flask running on Ubuntu 20.04

In case you have installed Flask on a remote server you can specify a host

flask run -host=0.0.0.0

So, enjoy it. Flask is well installed.

Conclusion

Flask is a good framework that allows us to start web development in Python. This way you can use it in your projects. Now that you know how to install it in Ubuntu 20.04 without problems.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article