DEV Community

Jovan Hernandez
Jovan Hernandez

Posted on • Originally published at dev.to

Configuring Ubuntu 18.04 for a Python Dev Environment with Flask, Gunicron, and VirtualEnv

Flask is a great Python framework for getting ideas up and running quickly on a lightweight app environment. In this tutorial we are going to get Python 3.6 configured with development system packages to start a new Flask web application project and run it with Green Unicorn (Gunicorn).

But first, what is Green Unicorn?

Green Unicorn (from here on out, we will refer to it as Gunicorn) is “…a Python WSGI HTTP Server for UNIX. It’s a pre-fork worker model. The Gunicorn server is broadly compatible with various web frameworks, simply implemented, light on server resources, and fairly speedy.”

You can find out more about Gunicorn, from their website, which is also where I pulled that quote. But for the sake of brevity, Gunicorn is an easy to use HTTP server for Python web apps.

System Package Installations

To begin, we’re going to install three packages. python-dev python-pip & python-virtualenv. You should also check which version of Python you have installed and it’s location, then run the following command below:

sudo apt-get install python3-dev python3-pip python3-virtualenv
Enter fullscreen mode Exit fullscreen mode

Installing Python packages.

If run correctly, the package manager should spit a bunch of output and report when everything is set up. From there, we’ll use pip and virtualenv, both packages we just installed, to form a directory and handle our app dependencies.

###CREATE A NEW DIRECTORY TO HOUSE YOUR VIRTUAL ENVS
cd ~
mkdir -p ~/venvs

###SPECIFY PYTHON3 FOR THE INSTALLTION AND THEN ACTIVATE YOUR VIRTUAL ENV
python3 -m venv venvs/flaskproject
source ~/venvs/flaskproject/bin/activate
Enter fullscreen mode Exit fullscreen mode

Installing packages and setting up a virtual environment for your Flask project.

If all goes well, you should see your terminal prompt changes with the name of your virtual environment that was activated. In our case, we have (flaskproject) now in our terminal prompt.

Using PIP for upgrades, Wheel, and Gunicorn

We’re going to use pip to install a few dependencies and packages. Let’s start by updating our packages list and then installing wheel. Then we will install Flask and Gunicorn as well.

###UPDATING AND INSTALLING WHEEL
pip install --upgrade && pip install wheel

###INSTALLING FLASK AND GUNICORN
pip install flask gunicorn
Enter fullscreen mode Exit fullscreen mode

You should see a bunch of output expressing successful installations. If all goes well, you shouldn’t see any errors.

Testing Flask and Running it with Gunicorn

We need to create a new directory to hold our app files. This directory will live within our home folder, not within our virtualenvs folder.

###CREATE A DIRECTORY AND MOVE INTO IT
mkdir ~/flaskproject
cd ~/flaskproject
###CREATE FILE FOR TEST APP CODE
nano app.py
Enter fullscreen mode Exit fullscreen mode

Within the app.py file, write the following code:

from flask import Flask, Response

app = Flask(\_\_name\_\_)

@app.route("/")
def index():
 return Response("Your Python Flask Project is Working!"), 200

if \_\_name\_\_ == "\_\_main\_\_":
 app.run(debug=True)
Enter fullscreen mode Exit fullscreen mode

Your test app code.

Now move to the directory above the flaskproject folder, which in our case is the user home folder. You can also just typecd ~ to get there.

From your home folder, we can use Gunicorn to run your app with the following command:

###RUN THIS FROM YOUR HOME FOLDER ~/
gunicorn flaskproject.app:app
Enter fullscreen mode Exit fullscreen mode

Running Gunicorn with Flask to test your app.

If successful, we’ll get output of Gunicorn starting and listening on localhost:8000 or 127.0.0.0:8000 and going to that address should show your app.

You should see this if you are successful.

That’s it! You basically set up a quick Python test environment using PIP, Flask, and Gunicorn. From here, you can build more dynamic web apps using Flask and test them the same way.

Top comments (0)