{"title":"Installing Spatialite","version":"1.8","locale":"en","docname":"ref/contrib/gis/install/spatialite","url":"/en/1.8/ref/contrib/gis/install/spatialite/","canonical":"https://djangodocs.dev/en/1.8/ref/contrib/gis/install/spatialite/","summary":"SpatiaLite adds spatial support to SQLite, turning it into a full-featured spatial database. Check first if you can install Spatialite from system packages or…","html":"<h1>Installing Spatialite<a class=\"heading-anchor\" href=\"#installing-spatialite\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p><a class=\"reference external\" href=\"https://www.gaia-gis.it/fossil/libspatialite\">SpatiaLite</a> adds spatial support to SQLite, turning it into a full-featured\nspatial database.</p>\n<p>Check first if you can install Spatialite from system packages or binaries. For\nexample, on Debian-based distributions, try to install the <code class=\"docutils literal notranslate\"><span class=\"pre\">spatialite-bin</span></code>\npackage. For Mac OS X, follow the\n<a class=\"reference internal\" href=\"#spatialite-macosx\"><span class=\"std std-ref\">specific instructions below</span></a>. For Windows, you may\nfind binaries on <a class=\"reference external\" href=\"http://www.gaia-gis.it/gaia-sins/\">Gaia-SINS</a> home page. In any case, you should always\nbe able to <a class=\"reference internal\" href=\"#spatialite-source\"><span class=\"std std-ref\">install from source</span></a>.</p>\n<p>When you are done with the installation process, skip to <a class=\"reference internal\" href=\"#create-spatialite-db\"><span class=\"std std-ref\">Creating a spatial database for SpatiaLite</span></a>.</p>\n<section id=\"installing-from-source\">\n<span id=\"spatialite-source\"></span><h2>Installing from source<a class=\"heading-anchor\" href=\"#installing-from-source\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/install/geolibs/\"><span class=\"doc\">GEOS and PROJ.4</span></a> should be installed\nprior to building SpatiaLite.</p>\n<section id=\"sqlite\">\n<h3>SQLite<a class=\"heading-anchor\" href=\"#sqlite\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Check first if SQLite is compiled with the <a class=\"reference external\" href=\"http://www.sqlite.org/rtree.html\">R*Tree module</a>. Run the sqlite3\ncommand line interface and enter the following query:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">sqlite</span><span class=\"o\">&gt;</span> <span class=\"n\">CREATE</span> <span class=\"n\">VIRTUAL</span> <span class=\"n\">TABLE</span> <span class=\"n\">testrtree</span> <span class=\"n\">USING</span> <span class=\"n\">rtree</span><span class=\"p\">(</span><span class=\"nb\">id</span><span class=\"p\">,</span><span class=\"n\">minX</span><span class=\"p\">,</span><span class=\"n\">maxX</span><span class=\"p\">,</span><span class=\"n\">minY</span><span class=\"p\">,</span><span class=\"n\">maxY</span><span class=\"p\">);</span>\n</code></pre></div>\n<p>If you obtain an error, you will have to recompile SQLite from source. Otherwise,\njust skip this section.</p>\n<p>To install from sources, download the latest amalgamation source archive from\nthe <a class=\"reference external\" href=\"http://www.sqlite.org/download.html\">SQLite download page</a>, and extract:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ wget http://sqlite.org/sqlite-amalgamation-3.6.23.1.tar.gz\n$ tar xzf sqlite-amalgamation-3.6.23.1.tar.gz\n$ cd sqlite-3.6.23.1\n</code></pre></div>\n<p>Next, run the <code class=\"docutils literal notranslate\"><span class=\"pre\">configure</span></code> script – however the <code class=\"docutils literal notranslate\"><span class=\"pre\">CFLAGS</span></code> environment variable\nneeds to be customized so that SQLite knows to build the R*Tree module:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ CFLAGS=&quot;-DSQLITE_ENABLE_RTREE=1&quot; ./configure\n$ make\n$ sudo make install\n$ cd ..\n</code></pre></div>\n</section>\n<section id=\"spatialite-library-libspatialite\">\n<span id=\"spatialitebuild\"></span><h3>SpatiaLite library (<code class=\"docutils literal notranslate\"><span class=\"pre\">libspatialite</span></code>)<a class=\"heading-anchor\" href=\"#spatialite-library-libspatialite\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Get the latest SpatiaLite library source bundle from the\n<a class=\"reference external\" href=\"http://www.gaia-gis.it/gaia-sins/libspatialite-sources/\">download page</a>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ wget http://www.gaia-gis.it/gaia-sins/libspatialite-sources/libspatialite-4.1.0.tar.gz\n$ tar xaf libspatialite-4.1.0.tar.gz\n$ cd libspatialite-4.1.0\n$ ./configure\n$ make\n$ sudo make install\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>For Mac OS X users building from source, the SpatiaLite library <em>and</em> tools\nneed to have their <code class=\"docutils literal notranslate\"><span class=\"pre\">target</span></code> configured:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ ./configure --target=macosx\n</code></pre></div>\n</aside>\n</section>\n<section id=\"pysqlite2\">\n<span id=\"id6\"></span><h3>pysqlite2<a class=\"heading-anchor\" href=\"#pysqlite2\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you’ve decided to use a <a class=\"reference internal\" href=\"/en/1.8/ref/databases/#using-newer-versions-of-pysqlite\"><span class=\"std std-ref\">newer version of pysqlite2</span></a> instead of the <code class=\"docutils literal notranslate\"><span class=\"pre\">sqlite3</span></code> Python stdlib\nmodule, then you need to make sure it can load external extensions (i.e. the\nrequired <code class=\"docutils literal notranslate\"><span class=\"pre\">enable_load_extension</span></code> method is available so <code class=\"docutils literal notranslate\"><span class=\"pre\">SpatiaLite</span></code> can be\nloaded).</p>\n<p>This might involve building it yourself. For this, download pysqlite2 2.6, and\nuntar:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ wget https://pypi.python.org/packages/source/p/pysqlite/pysqlite-2.6.3.tar.gz\n$ tar xzf pysqlite-2.6.3.tar.gz\n$ cd pysqlite-2.6.3\n</code></pre></div>\n<p>Next, use a text editor to edit the <code class=\"docutils literal notranslate\"><span class=\"pre\">setup.cfg</span></code> file to look like the\nfollowing:</p>\n<div class=\"code-block\" data-language=\"ini\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Ini</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Ini code\"><code><span class=\"k\">[build_ext]</span>\n<span class=\"c1\">#define=</span>\n<span class=\"na\">include_dirs</span><span class=\"o\">=</span><span class=\"s\">/usr/local/include</span>\n<span class=\"na\">library_dirs</span><span class=\"o\">=</span><span class=\"s\">/usr/local/lib</span>\n<span class=\"na\">libraries</span><span class=\"o\">=</span><span class=\"s\">sqlite3</span>\n<span class=\"c1\">#define=SQLITE_OMIT_LOAD_EXTENSION</span>\n</code></pre></div>\n<p>or if you are on Mac OS X:</p>\n<div class=\"code-block\" data-language=\"ini\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Ini</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Ini code\"><code><span class=\"k\">[build_ext]</span>\n<span class=\"c1\">#define=</span>\n<span class=\"na\">include_dirs</span><span class=\"o\">=</span><span class=\"s\">/Library/Frameworks/SQLite3.framework/unix/include</span>\n<span class=\"na\">library_dirs</span><span class=\"o\">=</span><span class=\"s\">/Library/Frameworks/SQLite3.framework/unix/lib</span>\n<span class=\"na\">libraries</span><span class=\"o\">=</span><span class=\"s\">sqlite3</span>\n<span class=\"c1\">#define=SQLITE_OMIT_LOAD_EXTENSION</span>\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>The important thing here is to make sure you comment out the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">define=SQLITE_OMIT_LOAD_EXTENSION</span></code> flag and that the <code class=\"docutils literal notranslate\"><span class=\"pre\">include_dirs</span></code>\nand <code class=\"docutils literal notranslate\"><span class=\"pre\">library_dirs</span></code> settings are uncommented and set to the appropriate\npath if the SQLite header files and libraries are not in <code class=\"docutils literal notranslate\"><span class=\"pre\">/usr/include</span></code>\nand <code class=\"docutils literal notranslate\"><span class=\"pre\">/usr/lib</span></code>, respectively.</p>\n</aside>\n<p>After modifying <code class=\"docutils literal notranslate\"><span class=\"pre\">setup.cfg</span></code> appropriately, then run the <code class=\"docutils literal notranslate\"><span class=\"pre\">setup.py</span></code> script\nto build and install:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ python setup.py install\n</code></pre></div>\n</section>\n</section>\n<section id=\"mac-os-x-specific-instructions\">\n<span id=\"spatialite-macosx\"></span><h2>Mac OS X-specific instructions<a class=\"heading-anchor\" href=\"#mac-os-x-specific-instructions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>To install the SpatiaLite library and tools, Mac OS X users can choose between\n<a class=\"reference internal\" href=\"#kyngchaos\"><span class=\"std std-ref\">KyngChaos</span></a> and <a class=\"reference external\" href=\"http://brew.sh/\">Homebrew</a>.</p>\n<section id=\"kyngchaos\">\n<h3>KyngChaos<a class=\"heading-anchor\" href=\"#kyngchaos\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>First, follow the instructions in the <a class=\"reference internal\" href=\"#kyngchaos\"><span class=\"std std-ref\">KyngChaos</span></a> section.</p>\n<p>When <a class=\"reference internal\" href=\"#create-spatialite-db\"><span class=\"std std-ref\">Creating a spatial database for SpatiaLite</span></a>, the <code class=\"docutils literal notranslate\"><span class=\"pre\">spatialite</span></code> program is required.\nHowever, instead of attempting to compile the SpatiaLite tools from source,\ndownload the <a class=\"reference external\" href=\"http://www.gaia-gis.it/spatialite-2.3.1/binaries.html\">SpatiaLite Binaries</a> for OS X, and install <code class=\"docutils literal notranslate\"><span class=\"pre\">spatialite</span></code> in a\nlocation available in your <code class=\"docutils literal notranslate\"><span class=\"pre\">PATH</span></code>.  For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ curl -O http://www.gaia-gis.it/spatialite/spatialite-tools-osx-x86-2.3.1.tar.gz\n$ tar xzf spatialite-tools-osx-x86-2.3.1.tar.gz\n$ cd spatialite-tools-osx-x86-2.3.1/bin\n$ sudo cp spatialite /Library/Frameworks/SQLite3.framework/Programs\n</code></pre></div>\n<p>Finally, for GeoDjango to be able to find the KyngChaos SpatiaLite library,\nadd the following to your <code class=\"docutils literal notranslate\"><span class=\"pre\">settings.py</span></code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">SPATIALITE_LIBRARY_PATH</span><span class=\"o\">=</span><span class=\"s1\">&#39;/Library/Frameworks/SQLite3.framework/SQLite3&#39;</span>\n</code></pre></div>\n</section>\n<section id=\"homebrew\">\n<h3>Homebrew<a class=\"heading-anchor\" href=\"#homebrew\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><a class=\"reference external\" href=\"http://brew.sh/\">Homebrew</a> handles all the SpatiaLite related packages on your behalf,\nincluding SQLite3, SpatiaLite, PROJ, and GEOS. Install them like this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ brew update\n$ brew install spatialite-tools\n$ brew install gdal\n</code></pre></div>\n<p>Finally, for GeoDjango to be able to find the SpatiaLite library, add the\nfollowing to your <code class=\"docutils literal notranslate\"><span class=\"pre\">settings.py</span></code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">SPATIALITE_LIBRARY_PATH</span><span class=\"o\">=</span><span class=\"s1\">&#39;/usr/local/lib/mod_spatialite.dylib&#39;</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"creating-a-spatial-database-for-spatialite\">\n<span id=\"create-spatialite-db\"></span><h2>Creating a spatial database for SpatiaLite<a class=\"heading-anchor\" href=\"#creating-a-spatial-database-for-spatialite\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>When running <code class=\"docutils literal notranslate\"><span class=\"pre\">manage.py</span> <span class=\"pre\">migrate</span></code> with a SQLite or SpatiaLite database, the\ndatabase file will be automatically created if it doesn’t exist. Django will\nalso ensure that the spatial metadata are initialized in the database.</p>\n<aside class=\"version-note version-changed\" data-version=\"1.8\">\n<p class=\"version-note-title\">Changed in Django 1.8</p><p>Prior to Django 1.8, you had to initialize spatial metadata tables yourself\nby manually running the “SELECT InitSpatialMetaData();” query.</p>\n</aside>\n</section>","rootId":"installing-spatialite","toc":[{"title":"Installing from source","anchor":"installing-from-source","children":[{"title":"SQLite","anchor":"sqlite","children":[]},{"title":"SpatiaLite library (libspatialite)","anchor":"spatialite-library-libspatialite","children":[]},{"title":"pysqlite2","anchor":"pysqlite2","children":[]}]},{"title":"Mac OS X-specific instructions","anchor":"mac-os-x-specific-instructions","children":[{"title":"KyngChaos","anchor":"kyngchaos","children":[]},{"title":"Homebrew","anchor":"homebrew","children":[]}]},{"title":"Creating a spatial database for SpatiaLite","anchor":"creating-a-spatial-database-for-spatialite","children":[]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/en/1.8/ref/"},{"docname":"ref/contrib/index","title":"contrib packages","url":"/en/1.8/ref/contrib/"},{"docname":"ref/contrib/gis/index","title":"GeoDjango","url":"/en/1.8/ref/contrib/gis/"},{"docname":"ref/contrib/gis/install/index","title":"GeoDjango Installation","url":"/en/1.8/ref/contrib/gis/install/"}],"prev":{"docname":"ref/contrib/gis/install/postgis","title":"Installing PostGIS","url":"/en/1.8/ref/contrib/gis/install/postgis/"},"next":{"docname":"ref/contrib/gis/model-api","title":"GeoDjango Model API","url":"/en/1.8/ref/contrib/gis/model-api/"},"formats":{"html":"/en/1.8/ref/contrib/gis/install/spatialite/","markdown":"/en/1.8/ref/contrib/gis/install/spatialite.md","json":"/en/1.8/ref/contrib/gis/install/spatialite.json"},"source":"https://github.com/django/django/blob/stable/1.8.x/docs/ref/contrib/gis/install/spatialite.txt","official":"https://docs.djangoproject.com/en/1.8/ref/contrib/gis/install/spatialite/","inVersions":["dev","6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9","1.8"],"inLocales":["en"]}