This article is part of in the series
Last Updated: Wednesday 12th March 2014

In this article, we are going to learn how to install Django on Windows, Mac and Linux. Since Mac and Linux are both derived from the Unix platform, the instructions about installing Django on Mac and Linux are almost identical to each other and we will present them in the same section. Windows, however, is so different from the other two operating systems that we need use one section to present the instructions about how to install Django on Windows.

Python Virtual Environment Setup

Often Python is already installed on your system if you're using a mainstream OS such as Windows, Mac OS X or Linux. However if it is not, you can download and install the appropriate Python version for your OS on the official Python website. Assuming you have Python 2.7.x installed on your OS, we will walk you through the steps to create a Django development environment on Window, Mac OS X and Linux in the following sections.

Before we dive into the steps to create a Django development environment, we'd like to review a useful tool called virtualenv. virtualenv is a tool to create isolated Python environments. Instead of writing code on top of one global Python environment, virtualenv allows you to create isolated "islands" or directories of Python environments each of which is an independent Python environment that has its own "system-level" packages and libraries.

Why do we need virtualenv since we can just write code running on top of the global Python environment?

Well, let's imagine a situation where my_library depends on another package dependent_library whose version has to be 1.0.0. When you upgrade the global Python environment from 2.7.3 to 2.3.3, dependent_library also gets upgraded to 1.2.0. Now my_library won't work anymore since it's calling methods and using classes from the 1.0.0 of dependent_library. Wouldn't it be nice if you can write my_library against an independent 1.0.0 dependent_library as well as another upgraded_my_library against 1.2.0?

Or imagine that you are programming on a shared hosting environment where your user does not have access to root-level directories such as /usr/lib which means you cannot modify the global Python environment into a version that you like. Wouldn't it be nice if you can create a Python environment inside your home directory?

Luckily, virtualenv solves all the aforementioned issues by creating an environment that has its own installation directories who do not share any library with other environments.

Setup virtualenv and Django in Windows

First, open your browser and navigate to virtualenv. Click the download button to get the source code of the latest virtualenv.

Second, open a Powershell instance and navigate to the directory into which you have downloaded the virtualenv source code and extract the tar file into a directory. Then you can change into that directory to install virtualenv for your current Python interpreter which could be invoked from command line.

Now you can create a virtualenv instance in your home directory.

Now we can activate the new environment using the activate script. Note that Windows' execution policy is restricted by default which means scripts such as activate cannot be executed. Therefore, we need to change the execution policy to AllSigned in order to be able to activate the virtualenv.

Notice that once the virtualenv is activated, you will see a string "(python2-workspace)" prepended to your command line's shell prompt. Now you can install Django inside the new virtual environment.

Setup virtualenv and Django in Mac OS X and Linux

Installing virtualenv and Django on Mac OS X and Linux is similar to Windows. First, you download the virtualenv source code, unpack it and install it using your global Python interpreter.

Then you go back to your home directory and create a new virtualenv in that directory.

Once the environment has been created, you can activate the environment and install Django in it.

Summary and Tips

In this article, we learned how to install virtualenv in Windows, Mac OS X and Linux and using its pip command to install Django. Since the virtual environments are separated from the rest of the system, the installed Django library only affects files executed within that particular environment. Compared to Mac OS X and Linux, setting up virtualenv in Windows requires one extra step to change the execution policy of scripts. Otherwise the steps to set up a Django virtual environment is almost identical across all platforms.

About The Author