install.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. =====================
  2. How to install Django
  3. =====================
  4. This document will get you up and running with Django.
  5. Install Python
  6. ==============
  7. Being a Python Web framework, Django requires Python. It works with Python 2.7
  8. or 3.3+.
  9. Get the latest version of Python at https://www.python.org/download/ or with
  10. your operating system's package manager.
  11. .. admonition:: Django on Jython
  12. If you use Jython_ (a Python implementation for the Java platform), you'll
  13. need to follow a few additional steps. See :doc:`/howto/jython` for details.
  14. .. _jython: http://jython.org/
  15. .. admonition:: Python on Windows
  16. If you are just starting with Django and using Windows, you may find
  17. :doc:`/howto/windows` useful.
  18. Install Apache and mod_wsgi
  19. =============================
  20. If you just want to experiment with Django, skip ahead to the next
  21. section; Django includes a lightweight web server you can use for
  22. testing, so you won't need to set up Apache until you're ready to
  23. deploy Django in production.
  24. If you want to use Django on a production site, use `Apache`_ with
  25. `mod_wsgi`_. mod_wsgi can operate in one of two modes: an embedded
  26. mode and a daemon mode. In embedded mode, mod_wsgi is similar to
  27. mod_perl -- it embeds Python within Apache and loads Python code into
  28. memory when the server starts. Code stays in memory throughout the
  29. life of an Apache process, which leads to significant performance
  30. gains over other server arrangements. In daemon mode, mod_wsgi spawns
  31. an independent daemon process that handles requests. The daemon
  32. process can run as a different user than the Web server, possibly
  33. leading to improved security, and the daemon process can be restarted
  34. without restarting the entire Apache Web server, possibly making
  35. refreshing your codebase more seamless. Consult the mod_wsgi
  36. documentation to determine which mode is right for your setup. Make
  37. sure you have Apache installed, with the mod_wsgi module activated.
  38. Django will work with any version of Apache that supports mod_wsgi.
  39. See :doc:`How to use Django with mod_wsgi </howto/deployment/wsgi/modwsgi>`
  40. for information on how to configure mod_wsgi once you have it
  41. installed.
  42. If you can't use mod_wsgi for some reason, fear not: Django supports many other
  43. deployment options. One is :doc:`uWSGI </howto/deployment/wsgi/uwsgi>`; it works
  44. very well with `nginx`_. Additionally, Django follows the WSGI spec
  45. (:pep:`3333`), which allows it to run on a variety of server platforms.
  46. .. _Apache: http://httpd.apache.org/
  47. .. _nginx: http://nginx.org/
  48. .. _mod_wsgi: http://code.google.com/p/modwsgi/
  49. .. _database-installation:
  50. Get your database running
  51. =========================
  52. If you plan to use Django's database API functionality, you'll need to make
  53. sure a database server is running. Django supports many different database
  54. servers and is officially supported with PostgreSQL_, MySQL_, Oracle_ and
  55. SQLite_.
  56. If you are developing a simple project or something you don't plan to deploy
  57. in a production environment, SQLite is generally the simplest option as it
  58. doesn't require running a separate server. However, SQLite has many differences
  59. from other databases, so if you are working on something substantial, it's
  60. recommended to develop with the same database as you plan on using in
  61. production.
  62. In addition to the officially supported databases, there are :ref:`backends
  63. provided by 3rd parties <third-party-notes>` that allow you to use other
  64. databases with Django.
  65. In addition to a database backend, you'll need to make sure your Python
  66. database bindings are installed.
  67. * If you're using PostgreSQL, you'll need the `psycopg2`_ package. Refer to the
  68. :ref:`PostgreSQL notes <postgresql-notes>` for further details.
  69. * If you're using MySQL, you'll need a :ref:`DB API driver
  70. <mysql-db-api-drivers>` like ``mysqlclient``. See :ref:`notes for the MySQL
  71. backend <mysql-notes>` for details.
  72. * If you're using SQLite you might want to read the :ref:`SQLite backend notes
  73. <sqlite-notes>`.
  74. * If you're using Oracle, you'll need a copy of cx_Oracle_, but please
  75. read the :ref:`notes for the Oracle backend <oracle-notes>` for details
  76. regarding supported versions of both Oracle and ``cx_Oracle``.
  77. * If you're using an unofficial 3rd party backend, please consult the
  78. documentation provided for any additional requirements.
  79. If you plan to use Django's ``manage.py migrate`` command to automatically
  80. create database tables for your models (after first installing Django and
  81. creating a project), you'll need to ensure that Django has permission to create
  82. and alter tables in the database you're using; if you plan to manually create
  83. the tables, you can simply grant Django ``SELECT``, ``INSERT``, ``UPDATE`` and
  84. ``DELETE`` permissions. After creating a database user with these
  85. permissions, you'll specify the details in your project's settings file,
  86. see :setting:`DATABASES` for details.
  87. If you're using Django's :doc:`testing framework</topics/testing/index>` to test
  88. database queries, Django will need permission to create a test database.
  89. .. _PostgreSQL: http://www.postgresql.org/
  90. .. _MySQL: http://www.mysql.com/
  91. .. _psycopg2: http://initd.org/psycopg/
  92. .. _SQLite: http://www.sqlite.org/
  93. .. _pysqlite: http://trac.edgewall.org/wiki/PySqlite
  94. .. _cx_Oracle: http://cx-oracle.sourceforge.net/
  95. .. _Oracle: http://www.oracle.com/
  96. .. _removing-old-versions-of-django:
  97. Remove any old versions of Django
  98. =================================
  99. If you are upgrading your installation of Django from a previous version,
  100. you will need to uninstall the old Django version before installing the
  101. new version.
  102. If you installed Django using pip_ or ``easy_install`` previously, installing
  103. with pip_ or ``easy_install`` again will automatically take care of the old
  104. version, so you don't need to do it yourself.
  105. If you previously installed Django using ``python setup.py install``,
  106. uninstalling is as simple as deleting the ``django`` directory from your Python
  107. ``site-packages``. To find the directory you need to remove, you can run the
  108. following at your shell prompt (not the interactive Python prompt):
  109. .. code-block:: console
  110. $ python -c "import django; print(django.__path__)"
  111. .. _install-django-code:
  112. Install the Django code
  113. =======================
  114. Installation instructions are slightly different depending on whether you're
  115. installing a distribution-specific package, downloading the latest official
  116. release, or fetching the latest development version.
  117. It's easy, no matter which way you choose.
  118. .. _installing-official-release:
  119. Installing an official release with ``pip``
  120. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  121. This is the recommended way to install Django.
  122. 1. Install pip_. The easiest is to use the `standalone pip installer`_. If your
  123. distribution already has ``pip`` installed, you might need to update it if
  124. it's outdated. (If it's outdated, you'll know because installation won't
  125. work.)
  126. 2. (optional) Take a look at virtualenv_ and virtualenvwrapper_. These tools
  127. provide isolated Python environments, which are more practical than
  128. installing packages systemwide. They also allow installing packages
  129. without administrator privileges. It's up to you to decide if you want to
  130. learn and use them.
  131. 3. If you're using Linux, Mac OS X or some other flavor of Unix, enter the
  132. command ``sudo pip install Django`` at the shell prompt. If you're using
  133. Windows, start a command shell with administrator privileges and run
  134. the command ``pip install Django``. This will install Django in your Python
  135. installation's ``site-packages`` directory.
  136. If you're using a virtualenv, you don't need ``sudo`` or administrator
  137. privileges, and this will install Django in the virtualenv's
  138. ``site-packages`` directory.
  139. .. _pip: http://www.pip-installer.org/
  140. .. _virtualenv: http://www.virtualenv.org/
  141. .. _virtualenvwrapper: http://virtualenvwrapper.readthedocs.org/en/latest/
  142. .. _standalone pip installer: http://www.pip-installer.org/en/latest/installing.html#install-pip
  143. Installing an official release manually
  144. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  145. 1. Download the latest release from our `download page`_.
  146. 2. Untar the downloaded file (e.g. ``tar xzvf Django-X.Y.tar.gz``,
  147. where ``X.Y`` is the version number of the latest release).
  148. If you're using Windows, you can download the command-line tool
  149. bsdtar_ to do this, or you can use a GUI-based tool such as 7-zip_.
  150. 3. Change into the directory created in step 2 (e.g. ``cd Django-X.Y``).
  151. 4. If you're using Linux, Mac OS X or some other flavor of Unix, enter the
  152. command ``sudo python setup.py install`` at the shell prompt. If you're
  153. using Windows, start a command shell with administrator privileges and
  154. run the command ``python setup.py install``. This will install Django in
  155. your Python installation's ``site-packages`` directory.
  156. .. admonition:: Removing an old version
  157. If you use this installation technique, it is particularly important
  158. that you :ref:`remove any existing
  159. installations<removing-old-versions-of-django>` of Django
  160. first. Otherwise, you can end up with a broken installation that
  161. includes files from previous versions that have since been removed from
  162. Django.
  163. .. _download page: https://www.djangoproject.com/download/
  164. .. _bsdtar: http://gnuwin32.sourceforge.net/packages/bsdtar.htm
  165. .. _7-zip: http://www.7-zip.org/
  166. Installing a distribution-specific package
  167. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  168. Check the :doc:`distribution specific notes </misc/distributions>` to see if
  169. your platform/distribution provides official Django packages/installers.
  170. Distribution-provided packages will typically allow for automatic installation
  171. of dependencies and easy upgrade paths; however, these packages will rarely
  172. contain the latest release of Django.
  173. .. _installing-development-version:
  174. Installing the development version
  175. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  176. .. admonition:: Tracking Django development
  177. If you decide to use the latest development version of Django,
  178. you'll want to pay close attention to `the development timeline`_,
  179. and you'll want to keep an eye on the :ref:`release notes for the
  180. upcoming release <development_release_notes>`. This will help you stay
  181. on top of any new features you might want to use, as well as any changes
  182. you'll need to make to your code when updating your copy of Django.
  183. (For stable releases, any necessary changes are documented in the
  184. release notes.)
  185. .. _the development timeline: https://code.djangoproject.com/timeline
  186. If you'd like to be able to update your Django code occasionally with the
  187. latest bug fixes and improvements, follow these instructions:
  188. 1. Make sure that you have Git_ installed and that you can run its commands
  189. from a shell. (Enter ``git help`` at a shell prompt to test this.)
  190. 2. Check out Django's main development branch (the 'trunk' or 'master') like
  191. so:
  192. .. code-block:: console
  193. $ git clone git://github.com/django/django.git django-trunk
  194. This will create a directory ``django-trunk`` in your current directory.
  195. 3. Make sure that the Python interpreter can load Django's code. The most
  196. convenient way to do this is via pip_. Run the following command:
  197. .. code-block:: console
  198. $ sudo pip install -e django-trunk/
  199. (If using a virtualenv_ you can omit ``sudo``.)
  200. This will make Django's code importable, and will also make the
  201. ``django-admin`` utility command available. In other words, you're all
  202. set!
  203. If you don't have pip_ available, see the alternative instructions for
  204. `installing the development version without pip`_.
  205. .. warning::
  206. Don't run ``sudo python setup.py install``, because you've already
  207. carried out the equivalent actions in step 3.
  208. When you want to update your copy of the Django source code, just run the
  209. command ``git pull`` from within the ``django-trunk`` directory. When you do
  210. this, Git will automatically download any changes.
  211. .. _Git: http://git-scm.com/
  212. .. _`modify Python's search path`: https://docs.python.org/install/index.html#modifying-python-s-search-path
  213. .. _installing-the-development-version-without-pip:
  214. Installing the development version without pip
  215. ----------------------------------------------
  216. If you don't have pip_, you can instead manually `modify Python's search
  217. path`_.
  218. First follow steps 1 and 2 above, so that you have a ``django-trunk`` directory
  219. with a checkout of Django's latest code in it. Then add a ``.pth`` file
  220. containing the full path to the ``django-trunk`` directory to your system's
  221. ``site-packages`` directory. For example, on a Unix-like system:
  222. .. code-block:: console
  223. $ echo WORKING-DIR/django-trunk > SITE-PACKAGES-DIR/django.pth
  224. In the above line, change ``WORKING-DIR/django-trunk`` to match the full path
  225. to your new ``django-trunk`` directory, and change ``SITE-PACKAGES-DIR`` to
  226. match the location of your system's ``site-packages`` directory.
  227. The location of the ``site-packages`` directory depends on the operating
  228. system, and the location in which Python was installed. To find your system's
  229. ``site-packages`` location, execute the following:
  230. .. code-block:: console
  231. $ python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
  232. (Note that this should be run from a shell prompt, not a Python interactive
  233. prompt.)
  234. Some Debian-based Linux distributions have separate ``site-packages``
  235. directories for user-installed packages, such as when installing Django from
  236. a downloaded tarball. The command listed above will give you the system's
  237. ``site-packages``, the user's directory can be found in ``/usr/local/lib/``
  238. instead of ``/usr/lib/``.
  239. Next you need to make the ``django-admin.py`` utility available in your
  240. shell PATH.
  241. On Unix-like systems, create a symbolic link to the file
  242. ``django-trunk/django/bin/django-admin`` in a directory on your system
  243. path, such as ``/usr/local/bin``. For example:
  244. .. code-block:: console
  245. $ ln -s WORKING-DIR/django-trunk/django/bin/django-admin.py /usr/local/bin/
  246. (In the above line, change WORKING-DIR to match the full path to your new
  247. ``django-trunk`` directory.)
  248. This simply lets you type ``django-admin.py`` from within any directory,
  249. rather than having to qualify the command with the full path to the file.
  250. On Windows systems, the same result can be achieved by copying the file
  251. ``django-trunk/django/bin/django-admin.py`` to somewhere on your system
  252. path, for example ``C:\Python27\Scripts``.
  253. Note that the rest of the documentation assumes this utility is installed
  254. as ``django-admin``. You'll have to substitute ``django-admin.py`` if you use
  255. this method.