sqlite3 Ruby Gem LoadError and mkmf Errors: What Each Means

Updated

ruby on rails

Three different errors get called “the sqlite3 mkmf error”, and the text tells you which one you have. Read the last line, not the wall above it:

cannot load such file -- sqlite3 (LoadError)                  # the gem is not installed, or not on this load path
cannot load such file -- sqlite3/sqlite3_native (LoadError)   # the gem IS installed; its C extension was never built
mkmf.rb can't find header files for ruby at …/ruby.h           # your RUBY headers are missing. Nothing to do with SQLite.

Both LoadErrors above are unedited output from this machine. Only the third has anything to do with a compiler, and the fix for it is ruby-dev / ruby-devel / xcode-select --install — not libsqlite3-dev. Before any of that: on a supported platform you do not need to compile anything.

Since 1.5.0 the gem brings its own SQLite

sqlite3.h from libsqlite3-dev has not been what the gem needs by default since 2022. From the gem’s own INSTALLATION.md: “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.” And for most people there is no compile step at all: “In v2.5.0 and later, native (precompiled) gems are available for recent Ruby versions on these platforms” — the list includes aarch64-linux-gnu, x86_64-linux-gnu, x86_64-linux-musl and arm64-darwin.

You can watch it happen. gem install sqlite3 -v 1.5.4 on this machine printed Successfully installed sqlite3-1.5.4-arm64-darwin — a prebuilt binary, no compiler invoked. Forcing the source build instead gives the vendoring proof:

$ ruby -e 'require "sqlite3"; puts SQLite3::SQLITE_VERSION'
3.40.0

$ sqlite3 --version
3.51.0 …

The gem is linked against 3.40.0, which it built itself. The system’s SQLite is 3.51.0 and is not involved. So “install libsqlite3-dev and the error goes away” is advice for a version of this gem that shipped four years ago — and installing the dev package when the gem is vendoring does nothing at all.

The extconf.rb abort message before and after 1.5.0

The wording comes from ext/sqlite3/extconf.rb in sqlite3 1.4.4, the last series that produced this message at all:

def asplode missing
  if RUBY_PLATFORM =~ /mingw|mswin/
    abort "#{missing} is missing. Install SQLite3 from " +
          "http://www.sqlite.org/ first."
  else
    abort <<-error
#{missing} is missing. Try 'brew install sqlite3',
'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
and check your shared library search path …

In 1.5.0 and later that was replaced by abort("\nCould not find #{missing}.\nPlease visit https://github.com/sparklemotion/sqlite3-ruby for installation instructions.\n\n").

Reproducing mkmf.rb can't find header files for ruby

This is the failure the slug names, and it fires before any gem-specific code runs. mkmf.rb checks for Ruby’s own headers as it loads. Pointing rubyhdrdir at a path that does not exist — exactly the state of a machine with no ruby-dev package, or a Mac whose Command Line Tools SDK went missing in an OS upgrade — reproduces it:

$ ruby force.rb
mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/include/ruby.h

You might have to install separate package for the ruby development
environment, ruby-dev or ruby-devel for example.
rc=1

The path in that message is not the path it needed. mkmf.rb tries rubyhdrdir first and a source-tree include directory second, and prints whichever candidate lost last — so people go hunting for /usr/lib/ruby/include/ruby.h, a file that was never going to be there. The one to check is the first candidate:

$ ruby -rrbconfig -e 'd=RbConfig::CONFIG["rubyhdrdir"]; puts d; puts File.exist?(d+"/ruby/ruby.h")'
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/include/ruby-2.6.0
true

Note where that lives on macOS: inside the Command Line Tools SDK, not the Ruby framework. On Debian and Ubuntu the package is ruby-dev, on Fedora and RHEL ruby-devel — cited from the message itself, not run.

Often the real answer is “this gem no longer supports your Ruby”

The current gem is 2.9.6 and requires Ruby >= 3.2. On the system Ruby 2.6.10 here:

$ gem install sqlite3
ERROR:  Error installing sqlite3:
	The last version of sqlite3 (>= 0) to support your Ruby & RubyGems was 1.6.9. Try installing it with `gem install sqlite3 -v 1.6.9` and then running the current command again
	sqlite3 requires Ruby version >= 3.2, < 4.1.dev. The current ruby version is 2.6.10.210.

$ gem install sqlite3 -v 1.6.9
ERROR:  Error installing sqlite3:
	The last version of sqlite3 (= 1.6.9) to support your Ruby & RubyGems was 1.6.9. Try installing it with `gem install sqlite3 -v 1.6.9` …
	sqlite3 requires Ruby version >= 2.7, < 3.3.dev. The current ruby version is 2.6.10.210.

RubyGems told me to run a command that then failed with the same shape of error, because its “last supported version” calculation and the gem’s own required_ruby_version disagree. I tried 1.6.9, 1.6.0 and 1.5.4; only 1.5.4 installed. I did not bisect the 1.5.x series, so “1.5.4 is the newest that works on Ruby 2.6” is what I observed, not a claim about 1.5.5 or 1.5.6. No amount of libsqlite3-dev would have changed that, and a Gemfile line such as gem 'sqlite3', '~> 2.5' cannot resolve on any Ruby below 3.2. If you hit this, the fix is a newer Ruby, not a build flag.

--with-sqlite3-dir is silently ignored by current versions

--with-sqlite3-dir works on 1.4.x, which calls dir_config("sqlite3"), and it does nothing at all afterwards. Same flag, same bogus path, two versions, counting how often the path reaches the generated Makefile:

1.4.4  --with-sqlite3-dir=/nope/abc  -> 3 hits    (-I/nope/abc/include, -L/nope/abc/lib)
1.5.4  --with-sqlite3-dir=/nope/abc  -> 0 hits    (ignored, no warning)
1.5.4  --with-opt-dir=/nope/xyz      -> 3 hits

There is no dir_config call for sqlite anywhere in 1.5.4’s extconf.rb or in the current main branch’s. If you genuinely need to build against a system library in an unusual place, the two switches that exist are --enable-system-libraries (documented as: “If you would prefer to build the sqlite3-ruby gem against your system libsqlite3…you may do so by using the --enable-system-libraries flag at gem install time”) and mkmf’s own always-present --with-opt-dir.

Your own Ruby gets the same treatment from sqlite3-ruby-gem-mkmf-loaderror.sh: with GEM_HOME pointed at an empty directory it forces the mkmf header abort, compares the path in that message with the one mkmf consults, provokes the gem-absent LoadError and counts how --with-sqlite3-dir and --with-opt-dir reach the Makefile; the version-refusal check applies only on a Ruby older than 3.2, and the install-based checks report SKIP without a network.

Headers found, build still fails

gem install sqlite3 -v 1.4.4 here got past every configure check — checking for sqlite3.h... yes, checking for sqlite3_libversion_number() in -lsqlite3... yes — and then died in the compiler:

database.c:748:57: error: incompatible function pointer types passing 'int (VALUE, int, char **, char **)' … [-Wincompatible-function-pointer-types]
database.c:748:81: error: incompatible integer to pointer conversion passing 'VALUE' … [-Wint-conversion]
10 warnings and 4 errors generated.
make: *** [database.o] Error 1

make failed, exit code 2

Old C that newer clang and GCC now reject by default, not a missing package. Pinning an old sqlite3 in a Gemfile and then “fixing” it by installing dev headers walks straight into this. The generic reading skill — where RubyGems prints the log path, what belongs in mkmf.log and why most of the errors in it are expected — is on the extconf.rb failed page.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.