How to Install Flask in Terminal? Explained!
Flask is a lightweight web framework for Python that’s perfect for building simple yet powerful web applications. Whether you’re just starting with web development or looking to create a quick prototype, Flask is a great tool to have in your arsenal. One of the best things about Flask is how easy it is to set up. In this guide, we’ll walk you through the step-by-step process of installing Flask using your terminal.
What is Flask?
Flask is a micro web framework for Python, which means it’s simple, minimal, and leaves many choices up to the developer. It’s designed to make getting a web server up and running quick and straightforward. Flask provides you with the essentials to build web apps, but you can also expand it with additional libraries and extensions.
Why Use Flask for Web Development?
Flask is perfect for smaller applications and prototyping because it has fewer built-in decisions than larger frameworks like Django. This means more flexibility for developers. Flask offers:
Now that you know why Flask is a great tool, let’s jump into how to install it.
Prerequisites for Installing Flask
Before installing Flask, there are a few things to check. Make sure you have the following:
- Python: Flask is a Python-based framework, so you need Python installed on your system.
- pip: This is the package installer for Python that allows you to install libraries like Flask.
Let’s go over these requirements quickly.
Checking Python Installation
You need Python 3.7 or higher for Flask. To check if Python is installed on your machine, open your terminal and type:
bashCopy codepython --version
or, for Python 3:
bashCopy codepython3 --version
If you see a version number like Python 3.9.1
, you’re good to go. If not, you’ll need to download and install Python from python.org.
Installing pip (Python Package Installer)
To check if pip is installed, type the following in your terminal:
bashCopy codepip --version
or
bashCopy codepip3 --version
If you get a version number in response, pip is already installed. If not, you can install it by running:
bashCopy codepython -m ensurepip --upgrade
Now, with Python and pip ready, we can proceed to install Flask.
Step 1: Open the Terminal
The terminal is the gateway to setting up Flask. It’s where you will enter all the commands for installation and configuration.
- On Mac/Linux, you can open the terminal directly from the applications menu or use the keyboard shortcut
Cmd + Space
and type “Terminal.” - On Windows, you can use Command Prompt or Windows PowerShell. Press
Win + R
, typecmd
, and hit Enter.
Make sure you’re in the right directory before proceeding. Navigate to your project folder (or where you want to create it) by using the cd
(change directory) command:
bashCopy codecd /path/to/your/project
Step 2: Create a Virtual Environment
It’s highly recommended to create a virtual environment when working with Python projects. This helps keep your project’s dependencies isolated, ensuring they don’t interfere with other Python projects.
Why Use a Virtual Environment?
A virtual environment allows you to manage dependencies specifically for your project without affecting the global Python installation. It keeps things neat and clean.
Command to Create a Virtual Environment
To create a virtual environment, run the following command:
bashCopy codepython3 -m venv venv
Here, venv
is the name of your virtual environment folder. You can name it anything you want, but using venv
is a common convention.
Activating the Virtual Environment
After creating the virtual environment, you need to activate it. Depending on your operating system, the commands are different:
- Mac/Linux:
bashCopy codesource venv/bin/activate
- Windows:
bashCopy codevenv\Scripts\activate
You should now see the virtual environment’s name in your terminal prompt, something like (venv)
, indicating it’s active.
Step 3: Installing Flask
Now that your virtual environment is up and running, it’s time to install Flask.
Command to Install Flask via pip
In your terminal, with the virtual environment activated, run the following command to install Flask:
bashCopy codepip install Flask
Flask will be installed, along with any dependencies it needs. You’ll see output detailing the installation process, including the Flask version and any other packages that were installed alongside it.
Verifying the Installation
To ensure Flask is installed correctly, you can run:
bashCopy codepip show Flask
This will display details about Flask, including the version, which should confirm the installation was successful.
Step 4: Create a Simple Flask App
Let’s test if Flask is working correctly by creating a simple app. In your project folder, create a file named app.py
:
bashCopy codetouch app.py
Open app.py
in a text editor and add the following code:
pythonCopy codefrom flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
This is a basic Flask app that returns “Hello, World!” when accessed. Now, to run the app, go back to your terminal and type:
bashCopy codepython app.py
You should see output like this:
csharpCopy code * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Open your browser and go to http://127.0.0.1:5000/
. You should see “Hello, World!” displayed on the page.
Step 5: Deactivating the Virtual Environment
Once you’re done working in the virtual environment, you can deactivate it by running:
bashCopy codedeactivate
This will return your terminal to its normal state.
Conclusion
That’s it! You’ve successfully installed Flask, set up a virtual environment, and even created a simple web app to test the installation. Flask’s simplicity makes it ideal for both beginners and experienced developers looking to build web applications quickly. Now that you know how to install Flask, you’re ready to start building and experimenting with web applications.
FAQs
1. What is Flask used for?
Flask is a Python framework used to create web applications. It’s known for its simplicity and flexibility.
2. Can I install Flask globally instead of using a virtual environment?
Yes, but it’s recommended to use a virtual environment to avoid version conflicts between projects.
3. What are the prerequisites for installing Flask?
You need Python and pip installed on your machine.
4. How can I update Flask?
You can update Flask by running the following command in your terminal: pip install --upgrade Flask
.
5. How do I uninstall Flask?
You can uninstall Flask by running: pip uninstall Flask
.