index.txt 17 KB

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