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