{"title":"Installing Spatialite","version":"1.9","locale":"en","docname":"ref/contrib/gis/install/spatialite","url":"/en/1.9/ref/contrib/gis/install/spatialite/","canonical":"https://djangodocs.dev/en/1.9/ref/contrib/gis/install/spatialite/","summary":"SpatiaLite adds spatial support to SQLite, turning it into a full-featured spatial database. First, check 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>First, check if you can install Spatialite from system packages or binaries.</p>\n<p>For example, on Debian-based distributions, try to install the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">spatialite-bin</span></code> package. For distributions that package SpatiaLite 4.2+,\ninstall <code class=\"docutils literal notranslate\"><span class=\"pre\">libsqlite3-mod-spatialite</span></code>.</p>\n<p>For Mac OS X, follow the <a class=\"reference internal\" href=\"#spatialite-macosx\"><span class=\"std std-ref\">instructions below</span></a>.</p>\n<p>For Windows, you may find binaries on the <a class=\"reference external\" href=\"http://www.gaia-gis.it/gaia-sins/\">Gaia-SINS</a> home page.</p>\n<p>In any case, you should always be able to <a class=\"reference internal\" href=\"#spatialite-source\"><span class=\"std std-ref\">install from source</span></a>.</p>\n<aside class=\"admonition-spatialite-library-path-setting-required-for-spatialite-4-2 admonition\" id=\"spatialite-source\">\n<p class=\"admonition-title\"><code class=\"docutils literal notranslate\"><span class=\"pre\">SPATIALITE_LIBRARY_PATH</span></code> setting required for SpatiaLite 4.2+</p>\n<p>If you’re using SpatiaLite 4.2+, you must put this in your settings:</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;mod_spatialite&#39;</span>\n</code></pre></div>\n</aside>\n<section id=\"installing-from-source\">\n<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.9/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=\"https://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=\"https://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 https://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>\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 creating a SpatiaLite database, 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>","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":"Mac OS X-specific instructions","anchor":"mac-os-x-specific-instructions","children":[{"title":"KyngChaos","anchor":"kyngchaos","children":[]},{"title":"Homebrew","anchor":"homebrew","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/en/1.9/ref/"},{"docname":"ref/contrib/index","title":"contrib packages","url":"/en/1.9/ref/contrib/"},{"docname":"ref/contrib/gis/index","title":"GeoDjango","url":"/en/1.9/ref/contrib/gis/"},{"docname":"ref/contrib/gis/install/index","title":"GeoDjango Installation","url":"/en/1.9/ref/contrib/gis/install/"}],"prev":{"docname":"ref/contrib/gis/install/postgis","title":"Installing PostGIS","url":"/en/1.9/ref/contrib/gis/install/postgis/"},"next":{"docname":"ref/contrib/gis/model-api","title":"GeoDjango Model API","url":"/en/1.9/ref/contrib/gis/model-api/"},"formats":{"html":"/en/1.9/ref/contrib/gis/install/spatialite/","markdown":"/en/1.9/ref/contrib/gis/install/spatialite.md","json":"/en/1.9/ref/contrib/gis/install/spatialite.json"},"source":"https://github.com/django/django/blob/stable/1.9.x/docs/ref/contrib/gis/install/spatialite.txt","official":"https://docs.djangoproject.com/en/1.9/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","fr","ja","id","pt-br","es"]}