Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Install Flask on Ubuntu 20.04 - DesignLinux

designlinux 0 Comments

Flask is a free and open-source micro web framework for Python designed to help developers build secure, scalable, and maintainable web applications. Flask is based on Werkzeug and uses Jinja2 as a template engine.

Unlike Django , by default Flask doesn’t include ORM, form validation, or any other functionalities provided by third-party libraries. Flask is built with extensions in mind, which are Python packages that add functionality to a Flask application.

There are different methods to install Flask on Ubuntu.

Flask packages are included in the official Ubuntu repositories and can be installed using the apt package manager. This is the simplest way to install Flask on Ubuntu 20.04, but not as flexible as installing in a virtual environment. Also, the version included in the repositories may lag behind the latest version of Flask.

Virtual environments allow you to create an isolated environment for different Python projects. This way, you can have multiple different Flask environments on a single computer and install a specific version of a module on a per-project basis without worrying that it will affect your other Flask installations. If you install Flask into the global environment, then you can install only one Flask version on your computer.

In this article, we’ll discuss how to install Flask on Ubuntu 20.04 inside a Python virtual environment.

Installing Flask on Ubuntu 20.04 #

Ubuntu 20.04 ships with Python 3.8. You can verify that Python is installed on your system by typing:

python3 -V

The output should look something like below:

Python 3.8.5

The recommended way to create a virtual environment is by using the venv module, which is provided by the python3-venv package. Run the following command to install the package:

sudo apt install python3-venv

Once the module is installed, we are ready to create a virtual environment for the Flask application.

Navigate to the directory where you want to store the Python 3 virtual environments. It can be your home directory or any other directory where your user has read and write permissions.

Create a new directory for the Flask application and switch into it:

mkdir flask_app && cd flask_app

Run the following command inside the directory to create the virtual environment:

python3 -m venv venv

The command will create a directory called venv, which contains a copy of the Python binary, the Pip package manager , the standard Python library, and other supporting files. You can use any name you want for the virtual environment.

To start using the virtual environment, you need to activate it with the activate script:

source venv/bin/activate

Once activated, the virtual environment’s bin directory will be added at the beginning of the $PATH variable. Your shell’s prompt will also change and show the name of the virtual environment you’re currently using. In this example that is venv.

Now that the virtual environment is activated, use the Python package manager pip to install Flask:

pip install Flask
Within the virtual environment, you can use the command pip instead of pip3 and python instead of python3.

To verify the installation, run the following command, which prints the Flask version:

python -m flask --version

At the time of writing this article, the latest official Flask version is 1.1.2

Python 3.8.5
Flask 1.1.2
Werkzeug 1.0.1

Your Flask version may differ from the version shown in this example.

Creating a Minimal Application #

We will create a simple hello world application that will simply print “Hello World!”.

Open your text editor or Python IDE and create the following file:

~/flask_app/hello.py
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

Here is what the code does:

  1. That first line imports the Flask class.
  2. The second line creates a new instance of the Flask class.
  3. The route() decorator is used to register the hello_world function for the / route. When this route is requested, the function is called, and the message “Hello World!” is returned to the client.

Save the file as hello.py and go back to your terminal window.

We’ll use the flask command to run the application, but before that, we need to tell the shell the application to work with by setting the FLASK_APP environment variable:

export FLASK_APP=hello.pyflask run

The command above will launch the development builtin server.

The output will look something like the following:

 * 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 want to access the Flask development server, you can make the server publicly available by appending --host=0.0.0.0 to the flask run command.

Open http://127.0.0.1:5000 in your web browser , and you will be presented with the “Hello World!” message.

To stop the development server type, CTRL-C in your terminal.

Once you are done with your work, deactivate the environment by typing deactivate, and you will return to your normal shell.

deactivate

Conclusion #

We’ve shown you how to create a Python virtual environment and install Flask on your Ubuntu 20.04 machine. To create additional Flask development environments, repeat the same procedure.

If you are new to Flask, visit the Flask documentation page and learn how to develop your first Flask app.

Feel free to leave a comment below.

python ubuntu

Related

Tags: python, ubuntu

How to Check if a String Contains a Substring in Bash

Prev Post

How to Increment and Decrement Variable in Bash

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 6
  • 699
  • 609,147

DesignLinux.com © All rights reserved

Go to mobile version