LoginSignup
10
2

More than 3 years have passed since last update.

【Railsアプリ構築】rails new コマンド実行時、SQLite3のビルドに失敗する

Last updated at Posted at 2019-05-19

概要

Railsアプリケーション生成のため、rails new ryo_sample(アプリ名)を実行すると、sqlite3- 1.4.1のビルド(インストール)に失敗しました。
本記事では、そのエラー内容および解決法を記載致します。

環境

・OSおよびバージョン
Windows7 64bit
・Rubyバージョン
2.5.5
・Railsバージョン
5.2.3

エラー

rails new ryo_sampleを実行する。

エラー詳細
D:\Rails>rails new ryo_sample
      create
      create  README.md
      create  Rakefile
      create  .ruby-version
      create  config.ru
      create  .gitignore
      create  Gemfile
         run  git init from "."
Initialized empty Git repository in D:/Rails/ryo_sample/.git/
      create  package.json
      create  app
 -----------------------------------------------------------------
         run  bundle install
Fetching gem metadata from https://rubygems.org/.............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies.....
Using rake 12.3.2
-----------------------------------------------------------------
Fetching sqlite3 1.4.1
Installing sqlite3 1.4.1 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/sqlite3-1.4.1/ext/sqlite3
C:/Ruby25-x64/bin/ruby.exe -r ./siteconf20190519-10684-1h26ztm.rb extconf.rb
checking for sqlite3.h... no
sqlite3.h is missing. Install SQLite3 from http://www.sqlite.org/ first.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
-----------------------------------------------------------------
To see why this extension failed to compile, please check the mkmf.log which can

be found here:

C:/Ruby25-x64/lib/ruby/gems/2.5.0/extensions/x64-mingw32/2.5.0/sqlite3-1.4.1/mkm
f.log

extconf failed, exit code 1

Gem files will remain installed in
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/sqlite3-1.4.1 for inspection.
Results logged to
C:/Ruby25-x64/lib/ruby/gems/2.5.0/extensions/x64-mingw32/2.5.0/sqlite3-1.4.1/gem
_make.out

An error occurred while installing sqlite3 (1.4.1), and Bundler cannot continue.

Make sure that `gem install sqlite3 -v '1.4.1' --source 'https://rubygems.org/'`

succeeds before bundling.

In Gemfile:
  sqlite3

sqlite3-1.4.1のビルドに失敗していることが分かります。
以下は、上記レスポンスの内、エラー箇所をピックアップしたものです。

エラー抜粋①
Installing sqlite3 1.4.1 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/sqlite3-1.4.1/ext/sqlite3
C:/Ruby25-x64/bin/ruby.exe -r ./siteconf20190519-10684-1h26ztm.rb extconf.rb
checking for sqlite3.h... no # sqlite3.hの存在チェックに失敗している
sqlite3.h is missing. Install SQLite3 from http://www.sqlite.org/ first.

②エラー詳細の中に以下の通り出力されている通り、mkmf.logを確認してビルドに失敗する詳細な理由を確かめます。

エラー抜粋②
To see why this extension failed to compile, please check the mkmf.log which can

be found here:

C:/Ruby25-x64/lib/ruby/gems/2.5.0/extensions/x64-mingw32/2.5.0/sqlite3-1.4.1/mkm
f.log

③mkmf.logを確認します。

mkmf.log
"pkg-config --exists sqlite3"
package configuration for sqlite3 is not found
find_header: checking for sqlite3.h... -------------------- no

"x86_64-w64-mingw32-gcc -o conftest.exe -IC:/Ruby25-x64/include/ruby-2.5.0/x64-mingw32 -IC:/Ruby25-x64/include/ruby-2.5.0/ruby/backward -IC:/Ruby25-x64/include/ruby-2.5.0 -I. -D_FORTIFY_SOURCE=2 -D__USE_MINGW_ANSI_STDIO=1 -DFD_SETSIZE=2048 -D_WIN32_WINNT=0x0501 -D__MINGW_USE_VC2005_COMPAT -D_FILE_OFFSET_BITS=64  -O3 -fno-fast-math conftest.c  -L. -LC:/Ruby25-x64/lib -L. -pipe -s     -lx64-msvcrt-ruby250  -lgmp -lshell32 -lws2_32 -liphlpapi -limagehlp -lshlwapi  "
checked program was:
-----------------------------------------------------------------

エラー詳細の出力と概ね同じ内容で、特にこれ以上細かいエラー要因は出力されておりませんでした。

ここまでで、sqlite3-1.4.1のビルドに失敗する要因はsqlite3-1.4.1のインストールに必要なパッケージであるsqlite3.hがないためであると判明しました。
なお、sqlite3.hはsqlite3-1.4.1をインストールする際に使用されるヘッダファイルです。
次に、エラーを解消する方法を説明いたします。

補足:native extensionとは

gemのnative extenstionは、主にCやC++などで書かれるライブラリを必要とするRubyのコードあたりを指す。
たとえば、mysql2のnative extensionが~と出てきたら、mysql2のgemのコードの一部は、CやC++で書かれたMySQLのクライアントライブラリを必要としている、ということ。native extenstionである部分を含むgemをインストールする場合は、CやC++で書かれたライブラリを別途gemのインストール先にインストールしておく必要がある。
ネイティブ(CやC++で書かれた)ライブラリにRubyで書かれたgemが依存してるのであって、ネイティブライブラリがRubyのgemに依存しているわけではない。

引用元:Rubyのgemをインストールしている際に他のライブラリのインストールを要求してくるnative extensionとは何者か

エラー解消方法

SQLite3公式サイトからソースファイルとDLLファイルをダウンロードします。
ソースファイル(sqlite3.h)はsqlite-amalgamation-3280000.zipをダウンロード
DLLファイル(sqlite3.dll)は自身の端末に適したものをダウンロードします。
僕の場合はWindows7の64bitなので、sqlite-dll-win64-x64-3280000.zipをダウンロードしました。
sqlite.png

※sqlite3.hのみでなく、sqlite3.dllが無いと、sqlite3-1.4.1をインストールする際にエラーになるので、sqlite3.dllファイルのダウンロードも必須となります。

②ダウンロードしたzipファイルを解凍し、対象ファイルを以下ディレクトリに配置します。
なお、解凍すると何個かファイルが格納されてますが、必要なのはsqlite3.h、sqlite3.dllのみですので、その2ファイルを以下のディレクトリに格納します。
・sqlite3.h
任意のディレクトリを作成し、格納します。
僕の場合は、C:/sqlite3/配下に格納しました。
・sqlite3.dll
Rubyのbinディレクトリ配下に配置します。
僕の場合は、C:\Ruby25-x64\binでした。

③以下のコマンドを実行し、sqlite3-1.4.1をインストールします。
この際、--with-sqlite3-includeには②でsqlite3.hを格納した絶対パスを指定し、--with-sqlite3-libにはsqlite3.dllを格納した絶対パスを指定します。

実行コマンド
D:\Rails>gem install sqlite3 --platform=ruby -- --with-sqlite3-include=C:/sqlite3 --with-sqlite3-lib=C:/Ruby25-x64/bin
レスポンス
D:\Rails>gem install sqlite3 --platform=ruby -- --with-sqlite3-include=C:/sqlite
  3 --with-sqlite3-lib=C:/Ruby25-x64/bin
  Temporarily enhancing PATH for MSYS/MINGW...
  Installing required msys2 packages: mingw-w64-x86_64-sqlite3
  警告: 公開キーリングが見つかりません。'pacman-key --init' を実行済みですか?
  エラー: mingw32: キー "AD351C50AE085775EB59333B5F92EFC1A47D45A1" は不明です
  エラー: キー "AD351C50AE085775EB59333B5F92EFC1A47D45A1" をリモートで検索できませ
  んでした
  エラー: mingw64: キー "AD351C50AE085775EB59333B5F92EFC1A47D45A1" は不明です
  エラー: キー "AD351C50AE085775EB59333B5F92EFC1A47D45A1" をリモートで検索できませ
  んでした
  エラー: msys: キー "AD351C50AE085775EB59333B5F92EFC1A47D45A1" は不明です
  エラー: キー "AD351C50AE085775EB59333B5F92EFC1A47D45A1" をリモートで検索できませ
  んでした
  エラー: データベース 'mingw32' は無効です (無効または破損したデータベース (PGP
  ))
  エラー: データベース 'mingw64' は無効です (無効または破損したデータベース (PGP
  ))
  エラー: データベース 'msys' は無効です (無効または破損したデータベース (PGP ))

  pacman failed with the following output:
  Building native extensions with: '--with-sqlite3-include=C:/sqlite3 --with-sqlit
  e3-lib=C:/Ruby25-x64/bin'
  This could take a while...
  Successfully installed sqlite3-1.4.1
  Parsing documentation for sqlite3-1.4.1
  Installing ri documentation for sqlite3-1.4.1
  Done installing documentation for sqlite3 after 1 seconds
  1 gem installed

sqlite3-1.4.1が無事インストールされました。

※公開キーリングでエラーがでてますが、申し訳ありません:sob:こちらは現状理解できておりません。解決次第改めて記事にしたいと思います。

④他に依存しているgemがあるかもしれないので、bundle installを実行します。

レスポンス
D:\Rails\ryo_sample>bundle install
Fetching gem metadata from https://rubygems.org/.............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies.....
Using rake 12.3.2
-----------------------------------------------------------------
Bundle complete! 16 Gemfile dependencies, 76 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

特に追加でインストールされたものはなく、正常終了しました。

ここまでで、sqlite3-1.4.1のビルドに失敗するというエラーに関しては解決ですが、
この後、DBの作成、Webサーバの起動、初期画面の表示までできるかも試します。

⑤以下のコマンドを実行してDBを作成します。

実行コマンド
D:\Rails\ryo_sample>bundle exec rake db:create
レスポンス
D:\Rails\ryo_sample>bundle exec rake db:create
Created database 'db/development.sqlite3'
Created database 'db/test.sqlite3'

無事に作成されました:laughing:

⑥以下のコマンドを実行してWebサーバを起動します。

実行コマンド
D:\Rails\ryo_sample>rails s
レスポンス
D:\Rails\ryo_sample>rails s
=> Booting Puma
=> Rails 5.2.3 application starting in development
=> Run `rails server -h` for more startup options
*** SIGUSR2 not implemented, signal based restart unavailable!
*** SIGUSR1 not implemented, signal based restart unavailable!
*** SIGHUP not implemented, signal based logs reopening unavailable!
Puma starting in single mode...
* Version 3.12.1 (ruby 2.5.5-p157), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop

レスポンスがこの状態になったら、初期画面にアクセスしてみましょう。
以下の平和な画面が表示されれば成功です!わーい!:family_wwgb:
ruby画面.png

拙い文章で申し訳ないですが、誰かの参考になれば幸いです:grinning:

参考にしたサイト

【Ruby】Windows で gem install sqlite3 がエラーとなる場合の対処法
can not load such file — sqlite3/sqlite3_native が出たときは
bundle installした時のwith native extensionsってなんだ?
Rubyのgemをインストールしている際に他のライブラリのインストールを要求してくるnative extensionとは何者か

10
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
2