Django is the OG Grandaddy of all Python frameworks: it's by far Python's most fully-featured MVC framework out of the box. Today we're going to look at the relatively painless process of setting up Django on a Ubuntu server.

Stack

  • Ubuntu
  • Python 3
  • Pip 3
  • Nginx
  • Django (latest)

Install all Dependencies

We'll start with the obligatory update to ensure we're getting the latest packages.

$ apt-get update
$ apt-get upgrade

Let's verify that the latest version of Python 3 is installed on your box:

$ python3 --version
Python 3.6.3

Regardless, it's probably a good idea to download the latest anyway:

apt-get install python3 python3-pip python3-dev

I highly recommend setting up a Python virtual environment before moving forward with any installs. If you're living in the stone age, virtualenv and virtualenvwrapper will probably server you fine. If you're a gentleman, use pipenv or poetry instead.

Now let's go ahead and install Django. We can find out what the latest distribution is by checking Django's download page: https://www.djangoproject.com/download/

With the version number in hand, we'll install Django using pip:

pip3 install Django==2.1.7

Note that we’re using pip3 here as opposed to pip, which explicitly downloads Django under our Python3 installation. If we’re installing inside our virtual environment, specifying pip3 is redundant; pip will work under the assumption that we’re using the only Python version installed to our environment.

Let's verify that you've installed Django correctly. Open your Python3 shell and input the following:

$ python3
>>> import django
>>> print(django.get_version())
1.11

If you receive an error along the lines of ModuleNotFoundError: No module named 'Django', Django was probably installed on Python2 as opposed to 3. Make sure that you used pip3 instead of pip to install Django, and try again.

Why is This Somewhat Convoluted?

All major Linux distributions come with Python 2.7 pre-installed. Python2 is still critical to the core functionality of most linux distributions, therefore Python2 must be left intact and cannot be deleted or modified without suffering damage to the operating system.

Unfortunately, the python and pip commands will forever refer to Python 2 as a result, thus forcing Python 3 users to forever utilize the python3 and pip3 commands. This isn't that big of a deal, but is a common pitfall for those switching over to the light side.