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.
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 April 2017 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.
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 python3-pip python3-dev
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.
Create a directory for the virtualenvs. Then create a new virtualenv.
# make sure pip and setuptools are the latest version pip3 install --upgrade pip setuptools # 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 python3 -m venv venvs/bottleproj
Activate the virtualenv.
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.
We can now install Bottle and Green Unicorn via the
pip install bottle gunicorn
No errors like we see in the following screenshot is a good sign.
mkdir command to create a new directory to keep our Bottle
project then use the
cd (change directory) command to move into the
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
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 -w 2 app:app
Sweet, we can bring up our shell Bottle app in the web browser at
Time to develop a full-fledged web application with Bottle!
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.
Fix errors in your Python applications before your users ever see them by monitoring your app with Rollbar.