index.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. ======================
  2. GeoDjango Installation
  3. ======================
  4. Overview
  5. ========
  6. In general, GeoDjango installation requires:
  7. #. :ref:`Python and Django <django>`
  8. #. :ref:`spatial_database`
  9. #. :doc:`geolibs`
  10. Details for each of the requirements and installation instructions
  11. are provided in the sections below. In addition, platform-specific
  12. instructions are available for:
  13. * :ref:`macos`
  14. * :ref:`windows`
  15. .. admonition:: Use the Source
  16. Because GeoDjango takes advantage of the latest in the open source geospatial
  17. software technology, recent versions of the libraries are necessary.
  18. If binary packages aren't available for your platform, installation from
  19. source may be required. When compiling the libraries from source, please
  20. follow the directions closely, especially if you're a beginner.
  21. Requirements
  22. ============
  23. .. _django:
  24. Python and Django
  25. -----------------
  26. Because GeoDjango is included with Django, please refer to Django's
  27. :ref:`installation instructions <installing-official-release>` for details on
  28. how to install.
  29. .. _spatial_database:
  30. Spatial database
  31. ----------------
  32. PostgreSQL (with PostGIS), MySQL, Oracle, and SQLite (with SpatiaLite) are the
  33. spatial databases currently supported.
  34. .. note::
  35. PostGIS is recommended, because it is the most mature and feature-rich
  36. open source spatial database.
  37. The geospatial libraries required for a GeoDjango installation depends
  38. on the spatial database used. The following lists the library requirements,
  39. supported versions, and any notes for each of the supported database backends:
  40. ================== ============================== ================== =========================================
  41. Database Library Requirements Supported Versions Notes
  42. ================== ============================== ================== =========================================
  43. PostgreSQL GEOS, GDAL, PROJ, PostGIS 14+ Requires PostGIS.
  44. MySQL GEOS, GDAL 8.0.11+ :ref:`Limited functionality <mysql-spatial-limitations>`.
  45. Oracle GEOS, GDAL 19+ XE not supported.
  46. SQLite GEOS, GDAL, PROJ, SpatiaLite 3.31.0+ Requires SpatiaLite 4.3+
  47. ================== ============================== ================== =========================================
  48. See also `this comparison matrix`__ on the OSGeo Wiki for
  49. PostgreSQL/PostGIS/GEOS/GDAL possible combinations.
  50. __ https://trac.osgeo.org/postgis/wiki/UsersWikiPostgreSQLPostGIS
  51. Installation
  52. ============
  53. Geospatial libraries
  54. --------------------
  55. .. toctree::
  56. :maxdepth: 1
  57. geolibs
  58. Database installation
  59. ---------------------
  60. .. toctree::
  61. :maxdepth: 1
  62. postgis
  63. spatialite
  64. :setting:`DATABASES` configuration
  65. ----------------------------------
  66. Set the :setting:`ENGINE <DATABASE-ENGINE>` setting to one of the :ref:`spatial
  67. backends <spatial-backends>`.
  68. Add ``django.contrib.gis`` to :setting:`INSTALLED_APPS`
  69. -------------------------------------------------------
  70. Like other Django contrib applications, you will *only* need to add
  71. :mod:`django.contrib.gis` to :setting:`INSTALLED_APPS` in your settings.
  72. This is so that the ``gis`` templates can be located -- if not done, then
  73. features such as the geographic admin or KML sitemaps will not function properly.
  74. Troubleshooting
  75. ===============
  76. If you can't find the solution to your problem here then participate in the
  77. community! You can:
  78. * Ask your question on the `GeoDjango`__ forum.
  79. * File a ticket on the `Django trac`__ if you think there's a bug. Make
  80. sure to provide a complete description of the problem, versions used,
  81. and specify the component as "GIS".
  82. __ https://forum.djangoproject.com/c/internals/geodjango/13
  83. __ https://code.djangoproject.com/newticket
  84. .. _libsettings:
  85. Library environment settings
  86. ----------------------------
  87. By far, the most common problem when installing GeoDjango is that the
  88. external shared libraries (e.g., for GEOS and GDAL) cannot be located. [#]_
  89. Typically, the cause of this problem is that the operating system isn't aware
  90. of the directory where the libraries built from source were installed.
  91. In general, the library path may be set on a per-user basis by setting
  92. an environment variable, or by configuring the library path for the entire
  93. system.
  94. ``LD_LIBRARY_PATH`` environment variable
  95. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  96. A user may set this environment variable to customize the library paths
  97. they want to use. The typical library directory for software
  98. built from source is ``/usr/local/lib``. Thus, ``/usr/local/lib`` needs
  99. to be included in the ``LD_LIBRARY_PATH`` variable. For example, the user
  100. could place the following in their bash profile:
  101. .. code-block:: shell
  102. export LD_LIBRARY_PATH=/usr/local/lib
  103. Setting system library path
  104. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  105. On GNU/Linux systems, there is typically a file in ``/etc/ld.so.conf``, which may include
  106. additional paths from files in another directory, such as ``/etc/ld.so.conf.d``.
  107. As the root user, add the custom library path (like ``/usr/local/lib``) on a
  108. new line in ``ld.so.conf``. This is *one* example of how to do so:
  109. .. code-block:: shell
  110. $ sudo echo /usr/local/lib >> /etc/ld.so.conf
  111. $ sudo ldconfig
  112. For OpenSolaris users, the system library path may be modified using the
  113. ``crle`` utility. Run ``crle`` with no options to see the current configuration
  114. and use ``crle -l`` to set with the new library path. Be *very* careful when
  115. modifying the system library path:
  116. .. code-block:: shell
  117. # crle -l $OLD_PATH:/usr/local/lib
  118. .. _binutils:
  119. Install ``binutils``
  120. ~~~~~~~~~~~~~~~~~~~~
  121. GeoDjango uses the ``find_library`` function (from the ``ctypes.util`` Python
  122. module) to discover libraries. The ``find_library`` routine uses a program
  123. called ``objdump`` (part of the ``binutils`` package) to verify a shared
  124. library on GNU/Linux systems. Thus, if ``binutils`` is not installed on your
  125. Linux system then Python's ctypes may not be able to find your library even if
  126. your library path is set correctly and geospatial libraries were built perfectly.
  127. The ``binutils`` package may be installed on Debian and Ubuntu systems using the
  128. following command:
  129. .. code-block:: shell
  130. $ sudo apt-get install binutils
  131. Similarly, on Red Hat and CentOS systems:
  132. .. code-block:: shell
  133. $ sudo yum install binutils
  134. Platform-specific instructions
  135. ==============================
  136. .. _macos:
  137. macOS
  138. -----
  139. Because of the variety of packaging systems available for macOS, users have
  140. several different options for installing GeoDjango. These options are:
  141. * :ref:`postgresapp` (easiest and recommended)
  142. * :ref:`homebrew`
  143. * :ref:`fink`
  144. * :ref:`macports`
  145. * :ref:`build_from_source`
  146. This section also includes instructions for installing an upgraded version
  147. of :ref:`macos_python` from packages provided by the Python Software
  148. Foundation, however, this is not required.
  149. .. _macos_python:
  150. Python
  151. ~~~~~~
  152. Although macOS comes with Python installed, users can use `framework
  153. installers`__ provided by the Python Software Foundation. An advantage to
  154. using the installer is that macOS's Python will remain "pristine" for internal
  155. operating system use.
  156. __ https://www.python.org/ftp/python/
  157. .. note::
  158. You will need to modify the ``PATH`` environment variable in your
  159. ``.profile`` file so that the new version of Python is used when
  160. ``python`` is entered at the command-line:
  161. .. code-block:: shell
  162. export PATH=/Library/Frameworks/Python.framework/Versions/Current/bin:$PATH
  163. .. _postgresapp:
  164. Postgres.app
  165. ~~~~~~~~~~~~
  166. `Postgres.app <https://postgresapp.com/>`_ is a standalone PostgreSQL server
  167. that includes the PostGIS extension. You will also need to install ``gdal`` and
  168. ``libgeoip`` with :ref:`homebrew`.
  169. After installing Postgres.app, add the following to your ``.bash_profile`` so
  170. you can run the package's programs from the command-line. Replace ``X.Y`` with
  171. the version of PostgreSQL in the Postgres.app you installed:
  172. .. code-block:: bash
  173. export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/X.Y/bin
  174. You can check if the path is set up correctly by typing ``which psql`` at a
  175. terminal prompt.
  176. .. _homebrew:
  177. Homebrew
  178. ~~~~~~~~
  179. `Homebrew`__ provides "recipes" for building binaries and packages from source.
  180. It provides recipes for the GeoDjango prerequisites on Macintosh computers
  181. running macOS. Because Homebrew still builds the software from source, `Xcode`_
  182. is required.
  183. Summary:
  184. .. code-block:: shell
  185. $ brew install postgresql
  186. $ brew install postgis
  187. $ brew install gdal
  188. $ brew install libgeoip
  189. __ https://brew.sh/
  190. .. _Xcode: https://developer.apple.com/xcode/
  191. .. _fink:
  192. Fink
  193. ^^^^
  194. `Kurt Schwehr`__ has been gracious enough to create GeoDjango packages for users
  195. of the `Fink`__ package system. `Different packages are available`__ (starting
  196. with ``django-gis``), depending on which version of Python you want to use.
  197. __ https://schwehr.blogspot.com/
  198. __ https://www.finkproject.org/
  199. __ https://pdb.finkproject.org/pdb/browse.php?summary=django-gis
  200. .. _macports:
  201. MacPorts
  202. ~~~~~~~~
  203. `MacPorts`__ may be used to install GeoDjango prerequisites on computers
  204. running macOS. Because MacPorts still builds the software from source,
  205. `Xcode`_ is required.
  206. Summary:
  207. .. code-block:: shell
  208. $ sudo port install postgresql14-server
  209. $ sudo port install geos
  210. $ sudo port install proj6
  211. $ sudo port install postgis3
  212. $ sudo port install gdal
  213. $ sudo port install libgeoip
  214. .. note::
  215. You will also have to modify the ``PATH`` in your ``.profile`` so
  216. that the MacPorts programs are accessible from the command-line:
  217. .. code-block:: shell
  218. export PATH=/opt/local/bin:/opt/local/lib/postgresql14/bin
  219. In addition, add the ``DYLD_FALLBACK_LIBRARY_PATH`` setting so that
  220. the libraries can be found by Python:
  221. .. code-block:: shell
  222. export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib:/opt/local/lib/postgresql14
  223. __ https://www.macports.org/
  224. .. _windows:
  225. Windows
  226. -------
  227. Proceed through the following sections sequentially in order to install
  228. GeoDjango on Windows. In this tutorial we will install 64 bit versions of each
  229. application.
  230. Python
  231. ~~~~~~
  232. Install a 64 bit version of Python. See :doc:`Install Python </howto/windows>`
  233. for further information.
  234. PostgreSQL
  235. ~~~~~~~~~~
  236. Download the latest `PostgreSQL 15.x installer`__ from the
  237. `EnterpriseDB`__ website. After downloading, run the installer, follow the
  238. on-screen directions, and keep the default options unless you know the
  239. consequences of changing them.
  240. .. note::
  241. The PostgreSQL installer creates a new ``postgres`` database superuser
  242. You will be prompted once to set the password -- make sure to remember it!
  243. When the installer completes, it will ask to "Launch Stack Builder at exit?" --
  244. keep this checked, as it is necessary to install :ref:`postgisasb`.
  245. .. note::
  246. If installed successfully, the PostgreSQL server will run in the background
  247. each time the system as started as a Windows service. A
  248. :menuselection:`PostgreSQL 15` start menu group will created and contains
  249. shortcuts for the Application Stack Builder (ASB) as well as the
  250. 'SQL Shell', which will launch a ``psql`` command window.
  251. __ https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
  252. __ https://www.enterprisedb.com
  253. .. _postgisasb:
  254. PostGIS
  255. ~~~~~~~
  256. From within the Stack Builder (to run outside of the installer,
  257. :menuselection:`Start --> PostgreSQL 15 --> Application Stack Builder`), select
  258. :menuselection:`PostgreSQL 15 (x64) on port 5432` from the drop down
  259. menu and click next. Expand the
  260. :menuselection:`Categories --> Spatial Extensions` menu tree and select
  261. :menuselection:`PostGIS X.Y for PostgreSQL 15`.
  262. After clicking next, you will be prompted to confirm the selected package and
  263. "Download directory". Click next again, this will download PostGIS and you will
  264. be asked to click next to begin the PostGIS installer. Select the default
  265. options during install. The install process includes four Yes/No dialog boxes,
  266. the default option for all four is "No".
  267. .. _osgeo4w:
  268. OSGeo4W
  269. ~~~~~~~
  270. The `OSGeo4W installer`_ helps to install the PROJ, GDAL, and GEOS libraries
  271. required by GeoDjango. First, download the `OSGeo4W installer`_, and
  272. run it. Select :menuselection:`Express Web-GIS Install` and click next. In the
  273. 'Select Packages' list, ensure that GDAL is selected. If any other packages are
  274. enabled by default, they are not required by GeoDjango and may be unchecked
  275. safely. After clicking next and accepting the license agreements, the packages
  276. will be automatically downloaded and installed, after which you may exit the
  277. installer.
  278. .. _OSGeo4W installer: https://trac.osgeo.org/osgeo4w/
  279. Modify Windows environment
  280. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  281. In order to use GeoDjango, you will need to add your OSGeo4W
  282. directories to your Windows system ``Path``, as well as create ``GDAL_DATA``
  283. and ``PROJ_LIB`` environment variables. The following set of commands,
  284. executable with ``cmd.exe``, will set this up. Restart your device
  285. once this is complete for new environment variables to be recognized:
  286. .. code-block:: bat
  287. set OSGEO4W_ROOT=C:\OSGeo4W
  288. set GDAL_DATA=%OSGEO4W_ROOT%\apps\gdal\share\gdal
  289. set PROJ_LIB=%OSGEO4W_ROOT%\share\proj
  290. set PATH=%PATH%;%OSGEO4W_ROOT%\bin
  291. reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /f /d "%PATH%"
  292. reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v GDAL_DATA /t REG_EXPAND_SZ /f /d "%GDAL_DATA%"
  293. reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PROJ_LIB /t REG_EXPAND_SZ /f /d "%PROJ_LIB%"
  294. .. note::
  295. Administrator privileges are required to execute these commands.
  296. To do this, run command prompt as administrator and enter the commands
  297. above. You need to log out and log back in again for the settings to take
  298. effect.
  299. .. note::
  300. If you customized the OSGeo4W installation directories, then you will need
  301. to modify the ``OSGEO4W_ROOT`` variables accordingly.
  302. Install Django and set up database
  303. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  304. :ref:`Install Django <installing-official-release>` on your system. It is
  305. recommended that you create a :doc:`virtual environment
  306. <python:tutorial/venv>` for each project you create.
  307. psycopg
  308. ~~~~~~~
  309. The ``psycopg`` Python module provides the interface between Python and the
  310. PostgreSQL database. ``psycopg`` can be installed via pip within your Python
  311. virtual environment:
  312. .. code-block:: doscon
  313. ...\> py -m pip install psycopg
  314. .. rubric:: Footnotes
  315. .. [#] GeoDjango uses the :func:`~ctypes.util.find_library` routine from
  316. ``ctypes.util`` to locate shared libraries.