Choose a different version or distribution
Before, we begin talking about the steps to install Flask on Ubuntu 20.04. First, let's understand - What is Flask?
Flask is a popular and lightweight web framework written in Python. It allows developers to build web applications quickly and easily. With its simplicity and flexibility, Flask is suitable for both beginners and experienced developers. It provides essential features like URL routing, template rendering, and session management.
Flask's modular design encourages code reusability and extensibility. It also integrates well with other libraries and tools, making it a versatile choice for web development projects. Whether you're creating a simple website or a complex web application, Flask simplifies the process, allowing you to focus on building great features.
In this tutorial, you will install Flask on Ubuntu 20.04. We will also address a few FAQs on how to install Flask on Ubuntu 20.04.
Advantages of Flask
- Simplicity: Flask's minimalistic design makes it easy to learn and use, perfect for beginners.
- Flexibility: Its modular structure allows developers to customize and adapt Flask to suit their specific project requirements.
- Lightweight: Flask has a small footprint, resulting in faster performance and efficient resource utilization.
- Integration: Flask seamlessly integrates with other Python libraries and tools, enabling developers to leverage their functionality.
- Extensibility: Flask's robust ecosystem provides a wide range of extensions, enhancing its capabilities for various web development needs.
Install Flask on Ubuntu 20.04
1) By default Ubuntu 20.04 comes with Python 3.8 installed. Verify if Python is installed on your system by typing the below command:
You will get the following output:
2) Starting from Python 3.6, the recommendation is to create a virtual environment. You will use the
venv module. So, to install the
python3-venv package providing
venv module, run the below command:
sudo apt install python3-venv
You will be ready to create a virtual environment once the module is installed.
Go to the directory where you want the Python 3 virtual environments to be saved. It might be your home directory or any other directory with read and write permissions for your user.
Switch into the Flask application directory you just created:
mkdir flask_app && cd flask_app
To construct the virtual environment, run the following command inside the directory:
python3 -m venv venv
The command creates the
venv directory, which contains a copy of the Python binaries, the Pip package manager, the standard Python library, and other supporting files. The virtual environment can be given whatever name you desire.
You must activate the virtual environment with the activate script before you may use it:
The virtual environment's bin directory will be placed at the beginning of the $PATH variable once it is activated. The name of the virtual environment you're now utilizing will appear in the prompt of your shell. That's venv in this case.
Use the Python package manager pip to install Flask now that the virtual environment has been activated:
pip install Flask
Run the following command, which prints the Flask version, to verify the installation:
python -m flask --version
The most recent official Flask version is 1.1.2 at the time of writing this article.
You may have a different version from the one sown above.
Create a Minimal Application
We'll make a basic hello world program that simply prints "Hello World!"
Create the following file in your text editor or Python IDE:
from flask import Flask
app = Flask(__name__)
return 'Hello World!'
The following is what the code does:
- The Flask class is imported in the first line.
- The second line of code launches a new Flask instance.
hello_worldfunction is registered for the / route using the
route()decorator. When this route is requested, the function is called, and the client receives the message "Hello World!"
Return to your terminal window and save the file as
To execute the application, we'll use the flask command, but first, we'll need to inform the shell which application to use by setting the
FLASK_APP environment variable:
The command above will start the built-in development server.
The following is an example of what the output will look like:
* Serving Flask app "hello.py"
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
If you installed Flask on a virtual machine and wish to visit the Flask development server, you can add —host=0.0.0.0 to the flask run command to make the server publically available.
In your web browser, go to
http://127.0.0.1:5000, and you'll see the message "Hello World!"
In your terminal, press
CTRL-C to shut down the development server.
When you're finished with your work, type deactivates to exit the environment and return to your regular shell.
FAQs to Install Flask on Ubuntu 20.04
What are the prerequisites for installing Flask on Ubuntu 20.04?
Before installing Flask on Ubuntu 20.04, make sure you have Python 3 and pip (Python package installer) installed. You can check their versions using
python3 --version and
pip --version respectively.
Can I install Flask using apt-get on Ubuntu 20.04?
No, Flask is not available in the default Ubuntu repositories. You need to use pip, the Python package installer, to install Flask on Ubuntu 20.04.
How do I check if Flask is installed correctly on Ubuntu 20.04?
After installation, you can verify Flask's installation by running
pip show flask in the terminal. If Flask is installed, it will display the package details.
How do I install additional Flask extensions on Ubuntu 20.04?
You can install additional Flask extensions by using pip. For example, to install the Flask-SQLAlchemy extension, run
pip install flask-sqlalchemy in the terminal.
How do I update Flask to the latest version on Ubuntu 20.04?
To update Flask to the latest version on Ubuntu 20.04, use the command
pip install --upgrade flask in the terminal. It will upgrade Flask to the newest available version.
How do I uninstall Flask from Ubuntu 20.04?
To uninstall Flask from Ubuntu 20.04, run
pip uninstall flask in the terminal. Confirm the uninstallation when prompted.
Can I use Flask with other web servers on Ubuntu 20.04?
Yes, Flask can work with various web servers on Ubuntu 20.04, such as Gunicorn, uWSGI, or the built-in development server. You can choose the server that best fits your requirements and deploy your Flask application accordingly.
We hope this detailed tutorial helped you to install Flask on Ubuntu 20.04.
If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.