We are going to learn flask installation on an Ubuntu system/machine. Flask is an open-source Python micro-framework for making APIs and Web applications.
Here are the steps for installing flask:
1. Check the python version on your system
First, use the below command to check the python version for installing Flask.
1 | python3 -V or python3 --version |
It will return the installed version of Python.
Example output
Python 3.9.10
2. Package installation for Virtua Environment
Now, we have to create a virtual environment using a python3-venv package. Run the below command for installing the package.
1 | sudo apt install python3-venv |
3. Create a new directory for your application
Create the new project Flask directory and go to it now.
1 | mkdir flask_app |
4. Create a Virtual environment
Go to the created directory and run the below command for creating a virtual environment.
1 | python3 -m venv venv |
5. Activate the environment using the below command
1 | source venv/bin/activate |
6. Install Python package manager pip to install Flask
Now the virtual environment has been activated. Install the pip(Package Installer for Python) to install Flask.
1 | sudo apt install python3-pip |
7. Flask Installation using PIP
Now PIP has been installed. Install the Flask using the below command.
1 | pip install Flask |
8. Check the Flask version
Flask has been successfully installed. Use the following command to validate the installation. It will show Python and Flask versions.
1 | python -m flask --version |
9. Add Code to your App folder
Now create a file(index.py) in your application folder and add the below code:
1 2 3 4 5 6 | from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' |
10. Set your file in the environment variable
Set the created file(index.py) in the environment.
1 | export FLASK_APP=index.py |
11. Run the Flask application
1 | flask run |
After running the command output will be shown below:
* 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)
Flask has been installed on your ubuntu system. You can develop your python application using Flask.