OverIQ.com

Installing Django

Last updated on July 27, 2020


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:

1
2
3
4
C:\Users\Win>python --version
Python 3.5.4

C:\Users\Win>

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.

C:\Users\Win>pip3 --version
pip 9.0.1 from c:\users\win\appdata\local\programs\python\python35\lib\site-packages (python 3.5)

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:

1
2
$ python3 --version
Python 3.5.2

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:

1
2
3
$ sudo add-apt-repository ppa:fkrull/deadsnakes
$ sudo apt-get update
$ sudo apt-get install python3.5

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

$ sudo dnf install python35

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:

$ sudo apt-get install python3-pip

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

$ sudo dnf install python3-pip

Testing Installation on Linux #

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

1
2
3
4
5
$ python3.5 --version
Python 3.5.2    

$ pip3 --version
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)

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 #

1
2
3
4
5
$ python3 --version
Python 3.5.2
$
$ pip3 --version
pip 9.0.1 from /usr/local/bin/site-packages (python 3.5)

Creating Virtual Environment with Virtualenv. #

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

$ mkdir djangobin

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 at 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:

$ cd djangobin

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.

$ pip3 install virtualenv

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

$ pip install virtualenv

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

$ virtualenv env

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

1
2
3
4
5
env/
├── bin/
├── include/
├── lib/
└── pip-selfcheck.json

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:

$ python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"

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:

$ virtualenv env -p /usr/bin/python2.7

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:

1
2
$ source env/bin/activate
(env) $

Windows users can use the following command:

1
2
C:\Users\Win>env\Scripts\activate.bat
(end) C:\Users\Win>

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.

1
2
3
4
5
6
7
(env) $ pip list
Package    Version
---------- -------
pip        10.0.1 
setuptools 39.2.0 
wheel      0.31.1 
(env) $:

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:

(env) $ deactivate

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$ pip3 list
apt-clone (0.2.1)
apt-xapian-index (0.47)
apturl (0.5.2)
blinker (1.3)
Brlapi (0.6.4)
chardet (2.3.0)
command-not-found (0.3)
cryptography (1.2.3)
decorator (4.2.1)
defer (1.0.6)
dirspec (13.10)
httplib2 (0.9.1)
idna (2.0)
...

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
C:\Users\Win>pip list
certifi (2017.4.17)
chardet (3.0.4)
colorama (0.3.9)
decorator (4.0.11)
httpie (0.9.9)
idna (2.5)
ipython (6.1.0)
ipython-genutils (0.2.0)
...

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.

$ pip3 install django==1.11

Testing the Installation #

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

1
2
3
4
5
(env) $ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

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

1
2
3
4
5
6
>>>
>>> import django
>>> 
>>> django.get_version()
'1.11'
>>>

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().