Flask is a popular Python web framework that allows you to create web applications quickly and easily. In this article, we’ll show you how to install Flask on Ubuntu 18.04.
Step 1: Update the Package Repository Before installing any new software on Ubuntu, it’s always a good idea to update the package repository to make sure you have the latest version of everything. You can do this by running the following command:
sudo apt update
Step 2: Install Python and Pip Flask is a Python framework, so you’ll need to have Python installed on your system. You can check if Python is already installed by running the following command:
python3 -V
If Python is not installed, you can install it using the following command:
sudo apt install python3
Once Python is installed, you’ll also need to install pip, which is a package manager for Python. You can install pip using the following command:
sudo apt install python3-pip
Step 3: Create a Virtual Environment It’s a good idea to create a virtual environment for your Flask application so that you can isolate it from other Python applications and avoid any conflicts between different versions of Python packages. You can create a virtual environment using the following command:
python3 -m venv myprojectenv
This will create a new virtual environment in a directory called myprojectenv
.
Step 4: Activate the Virtual Environment After creating the virtual environment, you need to activate it by running the following command:
source myprojectenv/bin/activate
You should see the name of your virtual environment in the command prompt, indicating that the virtual environment is now active.
Step 5: Install Flask Now that you have Python and pip installed, and you have created and activated a virtual environment for your project, you can install Flask using pip. To install Flask, run the following command:
pip install Flask
This will download and install Flask and its dependencies.
Step 6: Test the Installation To test that Flask has been installed correctly, create a new file called app.py
and paste the following code into it:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, World!"
if __name__ == "__main__":
app.run()
Save the file and run it using the following command:
export FLASK_APP=app.py
flask run
This will start a local web server on port 5000. Open your web browser and go http://localhost:5000/
to see the “Hello, World!” message displayed.
Step 7: Deactivate the Virtual Environment When you’re done working on your Flask application, you can deactivate the virtual environment by running the following command:
deactivate
This will return you to your normal command prompt.
Conclusion In this article, we’ve shown you how to install Flask on Ubuntu 18.04, create a virtual environment for your Flask application, and test that Flask is working correctly. With Flask installed, you’re ready to start building your own web applications!
Leave a Reply