Set Up A Python Django Development Environment on Debian 9 Stretch Linux

Introduction

Django is the top Python web development framework and for good reason. It’s powerful, flexible, and doesn’t get in the way of developers. It also scales incredibly well, powering sites like Instagram.

Installing a Django development environment is very simple on Linux, and Debian makes it even simpler. There are two basic ways to go about it; the virtualenv way and the Debian way. The virtualenv way keeps django and all of the other Python packages installed with it contained, allowing for multiple installations with different packages and package versions. The Debian way uses apt to install and manage Django.

The Virtualenv Way

The virtualenv route is the best way to go if you need multiple independent Django installs or you want to work with different versions of Python than the ones Debian uses by default.

Installing the Packages

To stat off, you need to make sure that Python is installed and install the virtualenv package to compartmentalize your Python projects.

# apt install python python3 virtualenv

Setting Up Virtualenv

After the packages finish installing, you can use virtualenv to create a new virtual environment for your Python project.

$ virtualenv -p python3 django-project

By running the above command, you will prompt virtualenv to create a new virtual environment using the current version of Python 3 installed on the system. If you leave off the -p python3 part and virtualenv will use the current system default, which is the 2.7 line in Debian Stretch.

In order to use the new virtual environment cd into the folder and activate it.

$ cd django-project
$ source bin/activate

You will see the name of the folder in parenthesis at the beginning of your prompt. When you’re done using the virtual environment, you can exit by typing deactivate.



The Debian Way

Debian has its own way of doing things, and it usually works well. If you’d prefer a system-wide Django install, you can use the packaged version of Django available form Debian’s repositories.

Installing the Packages

Installing Django this way is very simple. Just install the packages.

# apt install python python3 python-django

Setting Up Django

Now that you have your virtual environment set up and activated, you can install Django. Virtualenv automatically adds the Python package manager pip to each environment it creates. Pip acts exactly like any other package manager and handles installs, removals, and updates. Because you are using virtualenv, the packages that pip manages are specific to that environment and that environment alone.

You can install Django through pip just by telling it to install.

$ pip install django

Pip will handle dependency management and pull in Django. Again, it only applies to that environment.

You can now use Django’s built-in utilities to create your new project.

$ django-admin.py startproject newsite

Django will create the base project files for you at the name you specify. You can now cd into your new Django project. Once there, you need to set up the database by applying the basic migrations and creating your user.

$ cd newsite
$ python manage.py migrate
$ python manage.py createsuperuser

You will be asked to enter the information for your site’s admin user. Fill it out however you’d like. This information will be entered into the development database, so it doesn’t matter too much, unless you plan to import it into your production one.

You can test your new Django project. Use manage.py one more time to start up the development server.

$ python manage.py runserver

You can view the static start page at localhost:8000 in your browser.

Django running on Debian Stretch

Conclusion

No matter which way you chose, you now have a complete and working Django development environment on Debian Stretch. You can continue to build and expand your project with either method, and there are plenty of Django packages available through both pip and the Debian repositories.