Configuring Ubuntu 18.04 for a Python Dev Environment with Flask, Gunicron, and VirtualEnv
Tags: ubuntu, coding, python, flask
Published: 2019-07-22 21:17:23

---
title: Configuring Ubuntu 18.04 for a Python Dev Environment with Flask, Gunicron, and VirtualEnv
published: true
tags: ubuntu,coding,python,flask
canonical_url: https://dev.to/jovansn0w/configuring-ubuntu-18-04-for-a-python-dev-environment-with-flask-gunicron-and-virtualenv
---

![](https://cdn-images-1.medium.com/max/1024/1*gK7JTwGHlbhnqLOB18Xy_w.png)

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](http://gunicorn.org), 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
```

![](https://cdn-images-1.medium.com/max/1024/1*viHn_D1PVeNfccbiMgV14g.png)<figcaption>Installing Python packages.</figcaption>

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
```

![](https://cdn-images-1.medium.com/max/1024/1*ZcDXHBP7poZqV9W_uj2YKw.png)<figcaption>Installing packages and setting up a virtual environment for your Flask project.</figcaption>

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
```

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
```

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)
```

![](https://cdn-images-1.medium.com/max/1024/1*K8NBrPSsu5H-pL7CzHQWug.png)<figcaption>Your test app code.</figcaption>

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
```

![](https://cdn-images-1.medium.com/max/1024/1*Iqi40pXgYWVcOwijg6-ozw.png)<figcaption>Running Gunicorn with Flask to test your app.</figcaption>

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.

![](https://cdn-images-1.medium.com/max/1024/1*XVuUvqXNx8bloPO24D-nGg.png)<figcaption>You should see this if you are successful.</figcaption>

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.