Installing Django

To create a new Django application you must have the following things installed on your computer:

  • Python
  • A Virtual Environment
  • Django

Note: Throughout this tutorial instructions are given for Ubuntu, Mac and Windows. Most of the commands will be same, no matter which OS you are using. However, there are a few commands which vary from one system to another. If that’s the case, I have clearly mentioned it and provided commands specific to the system.

Let’s start by installing Python on Windows first.

Installing Python on Windows

Download the Python 3.5 installer from here. Once the download finishes; double click to start the installer and you will be presented a screen like this:

Tick the “Add Python 3.5 to PATH” option. This allows us to execute Python from anywhere in the directory structure without specifying the absolute path to the Python executable. To start the installation click on “INSTALL Now”. The installer will then install Python on your system. Once done, click the “Finish” button to close the installer.

Testing Installation on Windows

To test whether the Python installation was successful or not, open Command Prompt and type the following command:

Python Package Manager or PIP

In Python, we use pip (Python Package Index) to install and manage different packages (or libraries) available at https://pypi.python.org/pypi. It is important to note that pip itself is a package and is used to install other packages. Python installer for Windows automatically installs pip, so you don’t need to do anything else. To check the version of pip installed on your system execute the following command.

Installing Python on Linux

Almost all modern Linux distributions these days comes with Python 3.5 or above installed. To check the version of Python on your distribution type the following command:

As you can see, I have Python 3.5 installed which means I am ready for the next step. If your system has Python 3.4 or 3.6, you can follow along perfectly fine without having to install Python 3.5. Remember that Django 1.11 can be used with Python 2.7, 3.4, 3.5 and 3.6.

If for some reason your distribution doesn’t come with Python installed or have any old version of Python, then you will have to install Python manually. To install Python 3.5 on a Debian based system like Ubuntu type the following command:

If you are on a Red Hat Based system like Fedora type the following:

Unlike Windows, In Linux, you have to install pip manually. This is true whether Python comes pre-installed with your distribution or you have installed it manually. To install pip on a Debian based system type the following command:

To install pip on a Red Hat based system type the following command:

Testing Installation on Linux

In case you have installed Python manually you can use the following command to test the Python installation.

Installing Python on Mac OS

Download the Python installer from here. After the download finishes double-click to start the installer and go through the regular installation steps. Unlike Windows, Python installer for Mac OS doesn’t prompt you to add Python executable to the PATH environment variable, instead, it will automatically do so. In addition to that Python installer for Mac also installs pip so you don’t need to do anything else.

Testing installation on Mac OS

Creating Virtual Environment with Virtualenv.

Create a new directory named djangobin using the mkdir command as follows:

We will use this folder to store our Django application. Our application will be a code-sharing site which allows users to create, manage and search code snippets. You can see a live example of such a site https://djangosnippets.org.

You can create the djangobin directory anywhere, location doesn’t really matter. I am using Ubuntu and I have created this directory in my home directory. Once done, change your current working directory to djangobin using the cd command, as follows:

We are now ready to create a virtual environment.

A virtual environment helps us to run isolated instances of Python/Django projects on a machine without conflicting with one another. To understand the philosophy behind Virtual Environment, consider the following example:

Let’s say we are working on two projects, a blog and a forum for two different clients. Our blog uses version 2 of the super_lib library whereas our forum uses version 1. At a given point in time, we can only have a single version of super_lib installed on our system. We can’t have both versions side by side. A Virtual Environment helps us to tackle these kinds of problems.

Think of Virtual environment as a separate Python installation. Once installed, any libraries you install there using pip, will not conflict with the libraries available at the system-wide Python installation.

The package required to create these isolated environments is called virtualenv.

To install virtualenv on Ubuntu/Mac OS use pip3 instead of pip.

To install virtualenv on Windows, open command prompt and type the following command.

To create virtual environment type virtualenv command followed by the name of the virtual environment. For example:

This command will create a directory named env inside your current working directory
(djangobin). The directory structure of env should look like this:

The env directory contains a separate Python installation. Any libraries or packages you install here will be placed in the env/lib/python3.5/site-packages directory instead of global site-packages directory. You can print the path to global site-packages using the following command:

By default, virtualenv creates a virtual environment using the version of Python under which it is installed. In other words, if you have installed virtualenv as a package under Python 3.5, then that’s the version of Python which will be available inside the virtual environment.

To specify any other version of the Python, use the -p option as follows:

This will create a virtual environment using Python 2.7 instead of 3.5.

Activating Virtual Environment

In the previous step, we have created our virtual environment. Now to use it we first have to activate it. To active virtual environment in Linux or Mac OS type the following command:

Windows users can use the following command:

Notice (env) in front of the prompt string, it indicates that the virtual environment named env is up and running. From this point on, any package you add or remove using pip will only affect this virtual environment. Your system-wide Python installation will remain intact. We can use pip list command to view packages installed in this virtual environment.

This virtual environment has 3 packages installed. It is important to note that once the virtual environment is active you can invoke pip either using pip or pip3. This is true for Window, Ubuntu as well as Mac OS.

Deactivating Virtual Environment

To deactivate virtual environment type the following command:

This command is same for Windows, Ubuntu, and Mac. Now we are out of the virtual environment. Run pip3 list command again, but this time it will show you all the system-wide packages installed on your system.

Note: The ellipsis (...) indicates that the code snippet is truncated to save space.

On Windows, you can pip list instead of pip3 list to view the system-wide packages.

So these packages are available on my system-wide Python installation. Yours could be different.

In the next step, we will install Django.

Installing Django

In this tutorial, we will use Django 1.11. To install Django, activate virtual envionment and then type the following command.

Testing the Installation

Inside the virtual environment start the Python shell by typing python3 or just python if you are on Windows.

To verify whether the installation was successful or not, import django package and call get_version() function as follows.

If everything went fine, you should get the version of the Django installed. If you have encountered any error go through all the above steps or post a comment in the box below and we will figure out the problem.

To exit Python shell type Ctrl+D in Linux/Mac or Ctrl+Z in Windows or just type quit().

 

Leave a Comment