|
| 1 | + |
| 2 | +# Installation and Using SQLite3 extensions |
| 3 | + |
| 4 | +This document will help you install the `sqlite3` ruby gem. It also contains instructions on loading database extensions and building against drop-in replacements for sqlite3. |
| 5 | + |
| 6 | +## Installation |
| 7 | + |
| 8 | +### Native Gems (recommended) |
| 9 | + |
| 10 | +In v1.5.0 and later, native (precompiled) gems are available for recent Ruby versions on these platforms: |
| 11 | + |
| 12 | +- `aarch64-linux` (requires: glibc >= 2.29) |
| 13 | +- `arm-linux` (requires: glibc >= 2.29) |
| 14 | +- `arm64-darwin` |
| 15 | +- `x64-mingw32` / `x64-mingw-ucrt` |
| 16 | +- `x86-linux` (requires: glibc >= 2.17) |
| 17 | +- `x86_64-darwin` |
| 18 | +- `x86_64-linux` (requires: glibc >= 2.17) |
| 19 | + |
| 20 | +If you are using one of these Ruby versions on one of these platforms, the native gem is the recommended way to install sqlite3-ruby. |
| 21 | + |
| 22 | +For example, on a linux system running Ruby 3.1: |
| 23 | + |
| 24 | +``` text |
| 25 | +$ ruby -v |
| 26 | +ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux] |
| 27 | +
|
| 28 | +$ time gem install sqlite3 |
| 29 | +Fetching sqlite3-1.5.0-x86_64-linux.gem |
| 30 | +Successfully installed sqlite3-1.5.0-x86_64-linux |
| 31 | +1 gem installed |
| 32 | +
|
| 33 | +real 0m4.274s |
| 34 | +user 0m0.734s |
| 35 | +sys 0m0.165s |
| 36 | +``` |
| 37 | + |
| 38 | +#### Avoiding the precompiled native gem |
| 39 | + |
| 40 | +The maintainers strongly urge you to use a native gem if at all possible. It will be a better experience for you and allow us to focus our efforts on improving functionality rather than diagnosing installation issues. |
| 41 | + |
| 42 | +If you're on a platform that supports a native gem but you want to avoid using it in your project, do one of the following: |
| 43 | + |
| 44 | +- If you're not using Bundler, then run `gem install sqlite3 --platform=ruby` |
| 45 | +- If you are using Bundler |
| 46 | + - version 2.3.18 or later, you can specify [`gem "sqlite3", force_ruby_platform: true`](https://bundler.io/v2.3/man/gemfile.5.html#FORCE_RUBY_PLATFORM) |
| 47 | + - version 2.1 or later, then you'll need to run `bundle config set force_ruby_platform true` |
| 48 | + - version 2.0 or earlier, then you'll need to run `bundle config force_ruby_platform true` |
| 49 | + |
| 50 | + |
| 51 | +### Compiling the source gem |
| 52 | + |
| 53 | +If you are on a platform or version of Ruby that is not covered by the Native Gems, then the vanilla "ruby platform" (non-native) gem will be installed by the `gem install` or `bundle` commands. |
| 54 | + |
| 55 | + |
| 56 | +#### Packaged libsqlite3 |
| 57 | + |
| 58 | +By default, as of v1.5.0 of this library, the latest available version of libsqlite3 is packaged with the gem and will be compiled and used automatically. This takes a bit longer than the native gem, but will provide a modern, well-supported version of libsqlite3. |
| 59 | + |
| 60 | +For example, on a linux system running Ruby 2.5: |
| 61 | + |
| 62 | +``` text |
| 63 | +$ ruby -v |
| 64 | +ruby 2.5.9p229 (2021-04-05 revision 67939) [x86_64-linux] |
| 65 | +
|
| 66 | +$ time gem install sqlite3 |
| 67 | +Building native extensions. This could take a while... |
| 68 | +Successfully installed sqlite3-1.5.0 |
| 69 | +1 gem installed |
| 70 | +
|
| 71 | +real 0m20.620s |
| 72 | +user 0m23.361s |
| 73 | +sys 0m5.839s |
| 74 | +``` |
| 75 | + |
| 76 | + |
| 77 | +#### System libsqlite3 |
| 78 | + |
| 79 | +If you would prefer to build the sqlite3-ruby gem against your system libsqlite3, which requires that you install libsqlite3 and its development files yourself, you may do so by using the `--enable-system-libraries` flag at gem install time. |
| 80 | + |
| 81 | +PLEASE NOTE: |
| 82 | + |
| 83 | +- you must avoid installing a precompiled native gem (see [previous section](#avoiding-the-precompiled-native-gem)) |
| 84 | +- only versions of libsqlite3 `>= 3.5.0` are supported, |
| 85 | +- and some library features may depend on how your libsqlite3 was compiled. |
| 86 | + |
| 87 | +For example, on a linux system running Ruby 2.5: |
| 88 | + |
| 89 | +``` text |
| 90 | +$ time gem install sqlite3 -- --enable-system-libraries |
| 91 | +Building native extensions with: '--enable-system-libraries' |
| 92 | +This could take a while... |
| 93 | +Successfully installed sqlite3-1.5.0 |
| 94 | +1 gem installed |
| 95 | +
|
| 96 | +real 0m4.234s |
| 97 | +user 0m3.809s |
| 98 | +sys 0m0.912s |
| 99 | +``` |
| 100 | + |
| 101 | +If you're using bundler, you can opt into system libraries like this: |
| 102 | + |
| 103 | +``` sh |
| 104 | +bundle config build.sqlite3 --enable-system-libraries |
| 105 | +``` |
| 106 | + |
| 107 | +If you have sqlite3 installed in a non-standard location, you may need to specify the location of the include and lib files by using `--with-sqlite-include` and `--with-sqlite-lib` options (or a `--with-sqlite-dir` option, see [MakeMakefile#dir_config](https://ruby-doc.org/stdlib-3.1.1/libdoc/mkmf/rdoc/MakeMakefile.html#method-i-dir_config)). If you have pkg-config installed and configured properly, this may not be necessary. |
| 108 | + |
| 109 | +``` sh |
| 110 | +gem install sqlite3 -- \ |
| 111 | + --enable-system-libraries \ |
| 112 | + --with-sqlite3-include=/opt/local/include \ |
| 113 | + --with-sqlite3-lib=/opt/local/lib |
| 114 | +``` |
| 115 | + |
| 116 | + |
| 117 | +#### System libsqlcipher |
| 118 | + |
| 119 | +If you'd like to link against a system-installed libsqlcipher, you may do so by using the `--with-sqlcipher` flag: |
| 120 | + |
| 121 | +``` text |
| 122 | +$ time gem install sqlite3 -- --with-sqlcipher |
| 123 | +Building native extensions with: '--with-sqlcipher' |
| 124 | +This could take a while... |
| 125 | +Successfully installed sqlite3-1.5.0 |
| 126 | +1 gem installed |
| 127 | +
|
| 128 | +real 0m4.772s |
| 129 | +user 0m3.906s |
| 130 | +sys 0m0.896s |
| 131 | +``` |
| 132 | + |
| 133 | +If you have sqlcipher installed in a non-standard location, you may need to specify the location of the include and lib files by using `--with-sqlite-include` and `--with-sqlite-lib` options (or a `--with-sqlite-dir` option, see [MakeMakefile#dir_config](https://ruby-doc.org/stdlib-3.1.1/libdoc/mkmf/rdoc/MakeMakefile.html#method-i-dir_config)). If you have pkg-config installed and configured properly, this may not be necessary. |
| 134 | + |
| 135 | + |
| 136 | +## Using SQLite3 extensions |
| 137 | + |
| 138 | +### How do I load a sqlite extension? |
| 139 | + |
| 140 | +Some add-ons are available to sqlite as "extensions". The instructions that upstream sqlite provides at https://www.sqlite.org/loadext.html are the canonical source of advice, but here's a brief example showing how you can do this with the `sqlite3` ruby gem. |
| 141 | + |
| 142 | +In this example, I'll be loading the ["spellfix" extension](https://www.sqlite.org/spellfix1.html): |
| 143 | + |
| 144 | +``` text |
| 145 | +# download spellfix.c from somewherehttp://www.sqlite.org/src/finfo?name=ext/misc/spellfix.c |
| 146 | +$ wget https://raw.githubusercontent.com/sqlite/sqlite/master/ext/misc/spellfix.c |
| 147 | +spellfix.c 100%[=================================================>] 100.89K --.-KB/s in 0.09s |
| 148 | +
|
| 149 | +# follow instructions at https://www.sqlite.org/loadext.html |
| 150 | +# (you will need sqlite3 development packages for this) |
| 151 | +$ gcc -g -fPIC -shared spellfix.c -o spellfix.o |
| 152 | +
|
| 153 | +$ ls -lt |
| 154 | +total 192 |
| 155 | +-rwxrwxr-x 1 flavorjones flavorjones 87984 2023-05-24 10:44 spellfix.o |
| 156 | +-rw-rw-r-- 1 flavorjones flavorjones 103310 2023-05-24 10:43 spellfix.c |
| 157 | +``` |
| 158 | + |
| 159 | +Then, in your application, use that `spellfix.o` file like this: |
| 160 | + |
| 161 | +``` ruby |
| 162 | +require "sqlite3" |
| 163 | + |
| 164 | +db = SQLite3::Database.new(':memory:') |
| 165 | +db.enable_load_extension(true) |
| 166 | +db.load_extension("/path/to/sqlite/spellfix.o") |
| 167 | +db.execute("CREATE VIRTUAL TABLE demo USING spellfix1;") |
| 168 | +``` |
| 169 | + |
| 170 | +### How do I use an alternative sqlite3 implementation? |
| 171 | + |
| 172 | +Some packages, like pSQLite Encryption Extension ("SEE"), are intended to be ABI-compatible drop-in replacements for the sqlite3 shared object. |
| 173 | + |
| 174 | +If you've installed your alternative as an autotools-style installation, the directory structure will look like this: |
| 175 | + |
| 176 | +``` |
| 177 | +/opt/see |
| 178 | +├── bin |
| 179 | +│ └── sqlite3 |
| 180 | +├── include |
| 181 | +│ ├── sqlite3.h |
| 182 | +│ └── sqlite3ext.h |
| 183 | +├── lib |
| 184 | +│ ├── libsqlite3.a |
| 185 | +│ ├── libsqlite3.la |
| 186 | +│ ├── libsqlite3.so -> libsqlite3.so.0.8.6 |
| 187 | +│ ├── libsqlite3.so.0 -> libsqlite3.so.0.8.6 |
| 188 | +│ ├── libsqlite3.so.0.8.6 |
| 189 | +│ └── pkgconfig |
| 190 | +│ └── sqlite3.pc |
| 191 | +└── share |
| 192 | + └── man |
| 193 | + └── man1 |
| 194 | + └── sqlite3.1 |
| 195 | +``` |
| 196 | + |
| 197 | +You can build this gem against that library like this: |
| 198 | + |
| 199 | +``` |
| 200 | +gem install sqlite3 --platform=ruby -- \ |
| 201 | + --enable-system-libraries \ |
| 202 | + --with-opt-dir=/opt/see |
| 203 | +``` |
| 204 | + |
| 205 | +Explanation: |
| 206 | + |
| 207 | +- use `--platform=ruby` to avoid the precompiled native gems (see the README) |
| 208 | +- the `--` separates arguments passed to "gem install" from arguments passed to the C extension builder |
| 209 | +- use `--enable-system-libraries` to avoid the vendored sqlite3 source |
| 210 | +- use `--with-opt-dir=/path/to/installation` to point the build process at the desired header files and shared object files |
| 211 | + |
| 212 | +Alternatively, if you've simply downloaded an "amalgamation" and so your compiled library and header files are in arbitrary locations, try this more detailed command: |
| 213 | + |
| 214 | +``` |
| 215 | +gem install sqlite3 --platform=ruby -- \ |
| 216 | + --enable-system-libraries \ |
| 217 | + --with-opt-include=/path/to/include \ |
| 218 | + --with-opt-lib=/path/to/lib |
| 219 | +``` |
| 220 | + |
0 commit comments