Django is written in 100% pure Python code, so you’ll need to install Python on your system. Django requires Python 2.3 or higher. If you’re on Linux or Mac OS X, you probably already have Python installed. Type python at a command prompt (or in Terminal, in OS X). If you see something like this, then Python is installed:
Python 2.4.1 (#2, Mar 31 2005, 00:05:10) [GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin Type "help", "copyright", "credits" or "license" for more information.
Otherwise, if you see an error such as “command not found”, you’ll have to download and install Python. See http://www.python.org/download/ to get started. The installation is fast and easy.
In this section, we cover various installation options.
Installing an Official Release – Most people will want to install the latest official release from http://www.djangoproject.com/download/. Django uses the standard Python distutils installation method, which in Linux land looks like this:
- Download the tarball, which will be named something like Django-2.0.9.tar.gz.
- tar xzvf Django-*.tar.gz.
- cd Django-*.
- sudo python setup.py install.
On Windows, we recommend using 7-Zip to handle all manner of compressed files, including .tar.gz. Change into some other directory and start python. If everything worked, you should be able to import the module django:
>> import django >> django.VERSION (0, 96, None)
The Python interactive interpreter is a command-line program that lets you write a Python program interactively. To start it, just run the command python at the command line. Throughout this book, we feature example Python code that’s printed as if it’s being entered in the interactive interpreter. The triple greater-than signs (>>>) signify a Python prompt.
Installing an official release with pip – This is the recommended way to install Django.
- Install pip. The easiest is to use the standalone pip installer. If your distribution already has pip installed, you might need to update it if it’s outdated. If it’s outdated, you’ll know because installation won’t work.
- Take a look at virtualenv and virtualenvwrapper. These tools provide isolated Python environments, which are more practical than installing packages systemwide. They also allow installing packages without administrator privileges. The contributing tutorial walks through how to create a virtualenv.
- After you’ve created and activated a virtual environment, enter the command pip install Django at the shell prompt. As, to install Django 2.0.5 stable version to the server with pip command – pip install Django==2.0.5
- After the installation is complete, check the Django version with the command – django-admin –version
Install Django with Virtualenv – Virtualenv is a python environment builder. It is used to create isolated python environments. We can choose the version of python that will be installed in the virtualenv environment. This is very useful for developers, they can run and develop an application with different python versions and different environments on one OS. Virtualenv is available on PyPI repository – we can install it with the pip command – pip install virtualenv
Now we can use the virtualenv command to create a new environment with python3 as default python version. So let’s create a new environment “env01” with python3 as the python version and pip3 for the django installation.
virtualenv --python=python3 env01
The command will create a new directory called ‘env01’ which contains the directories bin, include and lib for pyhton. The virtualenv has been created, now let’s log into the new environment with the following command:
cd env01/ source bin/activate Next, install Django in the the virtual environment that we've created - pip install django==2.0.5 When the installation finished, check the Django installation - django-admin --version
Install Django from Git Repository – In order to install Django from the Git Repository, we need to install git to the system.
- Install git.
- Next, create new virtual environment named ‘git01’ using the virtualenv command – virtualenv –python=python git01
- Activate the ‘git01’ virtual environment.
cd git01/ source bin/activate
- Next, clone the django source code from github using git.
git clone git://github.com/django/django django-dev
- Install django development version using pip command as shown below.
pip install -e django-dev
When the installation process is done, let’s check the Django version with command –
django-admin --version
Django Source code repository
The Django source code repository uses Git to track changes to the code over time, so you’ll need a copy of the Git client. The Django Git repository is located online at github.com/django/django. It contains the full source code for all Django releases, which you can browse online.
The Git repository includes several branches:
- master contains the main in-development code which will become the next packaged release of Django. This is where most development activity is focused.
- stable/A.B.x are the branches where release preparation work happens. They are also used for bugfix and security releases which occur as necessary after the initial release of a feature version.
- soc20XX/<project> branches were used by students who worked on Django during the 2009 and 2010 Google Summer of Code programs.
- attic/<project> branches were used to develop major or experimental new features without affecting the rest of Django’s code.
The Git repository also contains tags. These are the exact revisions from which packaged Django releases were produced, since version 1.0. The source code for the Djangoproject.com website can be found at github.com/django/djangoproject.com.
The master branch – If you’d like to try out the in-development code for the next release of Django, or if you’d like to contribute to Django by fixing bugs or developing new features, you’ll want to get the code from the master branch.
Note that this will get all of Django: in addition to the top-level django module containing Python code, you’ll also get a copy of Django’s documentation, test suite, packaging scripts and other miscellaneous bits. Django’s code will be present in your clone as a directory named django.
Django uses branches to prepare for releases of Django. In the past when Django was hosted on Subversion, branches were also used for feature development. Now Django is hosted on Git and feature development is done on contributor’s forks, but the Subversion feature branches remain in Git for historical reference.
Stable branches – These branches can be found in the repository as stable/A.B.x branches and will be created right after the first alpha is tagged. For example, immediately after Django 1.5 alpha 1 was tagged, the branch stable/1.5.x was created and all further work on preparing the code for the final 1.5 release was done there.