1. =====================
    
  2. How to install Django
    
  3. =====================
    
  4. 
    
  5. This document will get you up and running with Django.
    
  6. 
    
  7. Install Python
    
  8. ==============
    
  9. 
    
  10. Django is a Python web framework. See :ref:`faq-python-version-support` for
    
  11. details.
    
  12. 
    
  13. Get the latest version of Python at https://www.python.org/downloads/ or with
    
  14. your operating system's package manager.
    
  15. 
    
  16. .. admonition:: Python on Windows
    
  17. 
    
  18.     If you are just starting with Django and using Windows, you may find
    
  19.     :doc:`/howto/windows` useful.
    
  20. 
    
  21. Install Apache and ``mod_wsgi``
    
  22. ===============================
    
  23. 
    
  24. If you just want to experiment with Django, skip ahead to the next
    
  25. section; Django includes a lightweight web server you can use for
    
  26. testing, so you won't need to set up Apache until you're ready to
    
  27. deploy Django in production.
    
  28. 
    
  29. If you want to use Django on a production site, use `Apache`_ with
    
  30. `mod_wsgi`_. mod_wsgi operates in one of two modes: embedded
    
  31. mode or daemon mode. In embedded mode, mod_wsgi is similar to
    
  32. mod_perl -- it embeds Python within Apache and loads Python code into
    
  33. memory when the server starts. Code stays in memory throughout the
    
  34. life of an Apache process, which leads to significant performance
    
  35. gains over other server arrangements. In daemon mode, mod_wsgi spawns
    
  36. an independent daemon process that handles requests. The daemon
    
  37. process can run as a different user than the web server, possibly
    
  38. leading to improved security. The daemon process can be restarted
    
  39. without restarting the entire Apache web server, possibly making
    
  40. refreshing your codebase more seamless. Consult the mod_wsgi
    
  41. documentation to determine which mode is right for your setup. Make
    
  42. sure you have Apache installed with the mod_wsgi module activated.
    
  43. Django will work with any version of Apache that supports mod_wsgi.
    
  44. 
    
  45. See :doc:`How to use Django with mod_wsgi </howto/deployment/wsgi/modwsgi>`
    
  46. for information on how to configure mod_wsgi once you have it
    
  47. installed.
    
  48. 
    
  49. If you can't use mod_wsgi for some reason, fear not: Django supports many other
    
  50. deployment options. One is :doc:`uWSGI </howto/deployment/wsgi/uwsgi>`; it works
    
  51. very well with `nginx`_. Additionally, Django follows the WSGI spec
    
  52. (:pep:`3333`), which allows it to run on a variety of server platforms.
    
  53. 
    
  54. .. _Apache: https://httpd.apache.org/
    
  55. .. _nginx: https://nginx.org/
    
  56. .. _mod_wsgi: https://modwsgi.readthedocs.io/en/develop/
    
  57. 
    
  58. .. _database-installation:
    
  59. 
    
  60. Get your database running
    
  61. =========================
    
  62. 
    
  63. If you plan to use Django's database API functionality, you'll need to make
    
  64. sure a database server is running. Django supports many different database
    
  65. servers and is officially supported with PostgreSQL_, MariaDB_, MySQL_, Oracle_
    
  66. and SQLite_.
    
  67. 
    
  68. If you are developing a small project or something you don't plan to deploy in
    
  69. a production environment, SQLite is generally the best option as it doesn't
    
  70. require running a separate server. However, SQLite has many differences from
    
  71. other databases, so if you are working on something substantial, it's
    
  72. recommended to develop with the same database that you plan on using in
    
  73. production.
    
  74. 
    
  75. In addition to the officially supported databases, there are :ref:`backends
    
  76. provided by 3rd parties <third-party-notes>` that allow you to use other
    
  77. databases with Django.
    
  78. 
    
  79. In addition to a database backend, you'll need to make sure your Python
    
  80. database bindings are installed.
    
  81. 
    
  82. * If you're using PostgreSQL, you'll need the `psycopg2`_ package. Refer to the
    
  83.   :ref:`PostgreSQL notes <postgresql-notes>` for further details.
    
  84. 
    
  85. * If you're using MySQL or MariaDB, you'll need a :ref:`DB API driver
    
  86.   <mysql-db-api-drivers>` like ``mysqlclient``. See :ref:`notes for the MySQL
    
  87.   backend <mysql-notes>` for details.
    
  88. 
    
  89. * If you're using SQLite you might want to read the :ref:`SQLite backend notes
    
  90.   <sqlite-notes>`.
    
  91. 
    
  92. * If you're using Oracle, you'll need a copy of cx_Oracle_, but please
    
  93.   read the :ref:`notes for the Oracle backend <oracle-notes>` for details
    
  94.   regarding supported versions of both Oracle and ``cx_Oracle``.
    
  95. 
    
  96. * If you're using an unofficial 3rd party backend, please consult the
    
  97.   documentation provided for any additional requirements.
    
  98. 
    
  99. If you plan to use Django's ``manage.py migrate`` command to automatically
    
  100. create database tables for your models (after first installing Django and
    
  101. creating a project), you'll need to ensure that Django has permission to create
    
  102. and alter tables in the database you're using; if you plan to manually create
    
  103. the tables, you can grant Django ``SELECT``, ``INSERT``, ``UPDATE`` and
    
  104. ``DELETE`` permissions. After creating a database user with these permissions,
    
  105. you'll specify the details in your project's settings file, see
    
  106. :setting:`DATABASES` for details.
    
  107. 
    
  108. If you're using Django's :doc:`testing framework</topics/testing/index>` to test
    
  109. database queries, Django will need permission to create a test database.
    
  110. 
    
  111. .. _PostgreSQL: https://www.postgresql.org/
    
  112. .. _MariaDB: https://mariadb.org/
    
  113. .. _MySQL: https://www.mysql.com/
    
  114. .. _psycopg2: https://www.psycopg.org/
    
  115. .. _SQLite: https://www.sqlite.org/
    
  116. .. _cx_Oracle: https://oracle.github.io/python-cx_Oracle/
    
  117. .. _Oracle: https://www.oracle.com/
    
  118. 
    
  119. .. _install-django-code:
    
  120. 
    
  121. Install the Django code
    
  122. =======================
    
  123. 
    
  124. Installation instructions are slightly different depending on whether you're
    
  125. installing a distribution-specific package, downloading the latest official
    
  126. release, or fetching the latest development version.
    
  127. 
    
  128. .. _installing-official-release:
    
  129. 
    
  130. Installing an official release with ``pip``
    
  131. -------------------------------------------
    
  132. 
    
  133. This is the recommended way to install Django.
    
  134. 
    
  135. #. Install pip_. The easiest is to use the `standalone pip installer`_. If your
    
  136.    distribution already has ``pip`` installed, you might need to update it if
    
  137.    it's outdated. If it's outdated, you'll know because installation won't
    
  138.    work.
    
  139. 
    
  140. #. Take a look at :doc:`venv <python:tutorial/venv>`. This tool provides
    
  141.    isolated Python environments, which are more practical than installing
    
  142.    packages systemwide. It also allows installing packages without
    
  143.    administrator privileges. The :doc:`contributing tutorial
    
  144.    </intro/contributing>` walks through how to create a virtual environment.
    
  145. 
    
  146. #. After you've created and activated a virtual environment, enter the command:
    
  147. 
    
  148.    .. console::
    
  149. 
    
  150.         $ python -m pip install Django
    
  151. 
    
  152. .. _pip: https://pip.pypa.io/
    
  153. .. _standalone pip installer: https://pip.pypa.io/en/latest/installation/
    
  154. 
    
  155. .. _installing-distribution-package:
    
  156. 
    
  157. Installing a distribution-specific package
    
  158. ------------------------------------------
    
  159. 
    
  160. Check the :doc:`distribution specific notes </misc/distributions>` to see if
    
  161. your platform/distribution provides official Django packages/installers.
    
  162. Distribution-provided packages will typically allow for automatic installation
    
  163. of dependencies and supported upgrade paths; however, these packages will rarely
    
  164. contain the latest release of Django.
    
  165. 
    
  166. .. _installing-development-version:
    
  167. 
    
  168. Installing the development version
    
  169. ----------------------------------
    
  170. 
    
  171. .. admonition:: Tracking Django development
    
  172. 
    
  173.     If you decide to use the latest development version of Django,
    
  174.     you'll want to pay close attention to `the development timeline`_,
    
  175.     and you'll want to keep an eye on the :ref:`release notes for the
    
  176.     upcoming release <development_release_notes>`. This will help you stay
    
  177.     on top of any new features you might want to use, as well as any changes
    
  178.     you'll need to make to your code when updating your copy of Django.
    
  179.     (For stable releases, any necessary changes are documented in the
    
  180.     release notes.)
    
  181. 
    
  182. .. _the development timeline: https://code.djangoproject.com/timeline
    
  183. 
    
  184. If you'd like to be able to update your Django code occasionally with the
    
  185. latest bug fixes and improvements, follow these instructions:
    
  186. 
    
  187. #. Make sure that you have Git_ installed and that you can run its commands
    
  188.    from a shell. (Enter ``git help`` at a shell prompt to test this.)
    
  189. 
    
  190. #. Check out Django's main development branch like so:
    
  191. 
    
  192.    .. console::
    
  193. 
    
  194.         $ git clone https://github.com/django/django.git
    
  195. 
    
  196.    This will create a directory ``django`` in your current directory.
    
  197. 
    
  198. #. Make sure that the Python interpreter can load Django's code. The most
    
  199.    convenient way to do this is to use a virtual environment and pip_. The
    
  200.    :doc:`contributing tutorial </intro/contributing>` walks through how to
    
  201.    create a virtual environment.
    
  202. 
    
  203. #. After setting up and activating the virtual environment, run the following
    
  204.    command:
    
  205. 
    
  206.    .. console::
    
  207. 
    
  208.         $ python -m pip install -e django/
    
  209. 
    
  210.    This will make Django's code importable, and will also make the
    
  211.    ``django-admin`` utility command available. In other words, you're all
    
  212.    set!
    
  213. 
    
  214. When you want to update your copy of the Django source code, run the command
    
  215. ``git pull`` from within the ``django`` directory. When you do this, Git will
    
  216. download any changes.
    
  217. 
    
  218. .. _Git: https://git-scm.com/