Fork me on GitHub

Configuring Python 3, Bottle and Gunicorn for Development on Ubuntu 16.04 LTS

The Ubuntu 16.04 Long Term Support (LTS) Linux operating system was released in April 2016. This latest Ubuntu release is named "Xenial Xerus" and it is the first Ubuntu release to include Python 3, instead of Python 2.x, as the default Python installation.

We can quickly start a new Bottle web application project and run it with Green Unicorn (Gunicorn) on Ubuntu 16.04.

Tools We Need

Our setup requires the Ubuntu 16.04 release along with a few other code libraries. Don't install these tools just yet since we'll get to them as we go through the walkthrough. Our requirements and their current versions as of May 13, 2016 are:

If you are developing on Mac OS X or Windows, make sure to use virtualization software such as Parallels or VirtualBox with the Ubuntu .iso file. Either the amd64 or i386 version of 16.04 is fine. I use the amd64 version for my own local development.

A desktop screen like this one appears when you boot up Ubuntu.

Open a terminal window to install the system packages.

System Packages

We can see the python3 system version Ubuntu comes with and where its executable is stored using these commands.

python3 --version
which python3

Our Ubuntu installation requires a few system packages. We will get prompted for the superuser password because restricted system access is needed to install packages through apt.

sudo apt-get install virtualenv python-pip python3-dev

Enter y to let the system package installation process do its job.

The packages we need are now installed. We can continue on to install our Python-specific dependencies.

Virtualenv

In the previous section, virtualenv and pip were installed to handle our application dependencies. We can now use them to download and install Bottle and Gunicorn.

Create a directory for the virtualenvs. Then create a new virtualenv.

# the tilde "~" specifies the user's home directory, like /home/matt
cd ~
mkdir venvs
# specify the system python3 installation
virtualenv --python=/usr/bin/python3 venvs/bottleproj

Activate the virtualenv.

source ~/venvs/bottleproj/bin/activate

Our prompt will change after we properly activate the virtualenv.

Our virtualenv is now activated with Python 3. We can install whatever dependencies we want, in our case Bottle and Gunicorn.

Bottle and Gunicorn

We can now install Bottle and Green Unicorn via the pip command.

pip install bottle gunicorn

No errors like we see in the following screenshot is a good sign.

Use the mkdir command to create a new directory to keep our Bottle project then use the cd (change directory) command to move into the new folder.

mkdir ~/bottleproj
cd ~/bottleproj

Create a new file named app.py within our bottleproj directory so we can test to make sure Bottle is working properly. I prefer to use Vim but Emacs and other development environments work great as well.

Within the new app.py file write the following code.

import bottle
from bottle import route, run, Response

# a basic URL route to test whether Bottle is responding properly
@route('/')
def index():
    return Response("It works!")

# these two lines are only used for python app.py
if __name__ == '__main__':
    run(host='0.0.0.0', port=8000, debug=True, reloader=True)

# this is the hook for Gunicorn to run Bottle
app = bottle.default_app()

We could run our app with the Bottle development server using the python app.py command. Let's instead run our Bottle app with Gunicorn.

gunicorn -w 2 app:app

Sweet, we can bring up our shell Bottle app in the web browser at the localhost:8000 or 127.0.0.1:8000 address.

Time to develop a full-fledged web application with Bottle!

Ready for Development

Now you have a simple setup to develop Bottle web apps using Gunicorn as the WSGI server on Ubuntu 16.04. If you need a full step-by-step tutorial to deploy your Python web application to a production environment, check out the Full Stack Python Guide to Deployments book.

To decide what to do next with your Python project, check out the Full Stack Python table of contents page.

See something wrong in this post? Fork this page's source on GitHub and submit a pull request.


Sign up here to receive a monthly email with major updates to this site, tutorials and discount codes for Python books.