#navi(../)
* RubyにSQLiteのモジュールをインストールする [#p19dfe4b]
RubyでSQLite3を使用するには、sqlite3のモジュールをインストールする必要があります。~
以下の操作によりRubyでsqlite3を操作するためのモジュールをインストールすることができます。~
動作確認環境としてUbuntuを利用しました。~
SQLite3は既にインストール済みであることを前提に本資料を記述しています。
#contents
#htmlinsertpcsp(db-top.html,db-sp.html)
* 関連記事 [#qe87c71b]
-[[UbuntuにSQLite3をインストール>SQLite/UbuntuにSQLite3をインストール]]
-[[CentOSにSQLite3をインストール>SQLite/CentOSにSQLite3をインストール]]
-[[WindowsにSQLite3のインストール>SQLite/WindowsにSQLite3のインストール]]
[[LL.JUST4FUN.BIZ>http://ll.just4fun.biz/]]の記事です。
-[[UbuntuにRubyをインストール>http://ll.just4fun.biz/Ruby/%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB/Ubuntu%E3%81%ABRuby%E3%82%92%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB.html]]
* 動作確認環境 [#e766816b]
$ ruby --version
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
$ sqlite3 --version
3.7.9 2011-11-01 00:52:41 c7c6050ef060877ebe77b41d959e9df13f8c9b5e
$ lsb_release -d
Description: Ubuntu 12.04.4 LTS
* sqlite3モジュールのインストール [#wf257e24]
以下のようなサンプルスクリプトを作り実行すると以下のようにエラーメッセージが表示されます。~
sqlite3のモジュールがインストールされていないことが確認できます。
$ cat sqlite3.rb
require 'rubygems'
require 'sqlite3'
$ ruby sqlite3.rb
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- sqlite3 (LoadError)
from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from sqlite3.rb:2:in `<main>'
+モジュールをインストールするために、sqlite3-devパッケージをインストールします。~
インストールしておらず、sqlite3のモジュールをインストールしようとしたら以下のメッセージが表示されました。~
$ sudo gem install sqlite3-ruby
Building native extensions. This could take a while..
<snip>
checking for sqlite3.h... no
sqlite3.h is missing. Try 'port install sqlite3 +universal',
'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
and check your shared library search path (the
location where your sqlite3 shared library is located).
なので、devパッケージをインストールします。(CentOSの場合はyumですね。)~
ちなみにmakeコマンドも必要ですのでインストールされていない方はインストールしてください。
sudo apt-get install libsqlite3-dev
+準備ができたので、gemコマンドでsqlite3-rubyをインストールします。
Ubuntuなので、sudoをつけて以下のように実行します。
sudo gem install sqlite3-ruby
上記コマンドを実行すると以下のようにインストールできました。
$ sudo gem install sqlite3-ruby
Building native extensions. This could take a while...
Fetching: sqlite3-ruby-1.3.3.gem (100%)
#######################################################
Hello! The sqlite3-ruby gem has changed it's name to just sqlite3. Rather than
installing `sqlite3-ruby`, you should install `sqlite3`. Please update your
dependencies accordingly.
Thanks from the Ruby sqlite3 team!
<3 <3 <3 <3
#######################################################
Successfully installed sqlite3-1.3.9
Successfully installed sqlite3-ruby-1.3.3
2 gems installed
Installing ri documentation for sqlite3-1.3.9...
Installing ri documentation for sqlite3-ruby-1.3.3...
Installing RDoc documentation for sqlite3-1.3.9...
Installing RDoc documentation for sqlite3-ruby-1.3.3...
+再度以下のスクリプトを実行する
$ cat sqlite3.rb
require 'rubygems'
require 'sqlite3'
$ ruby sqlite3.rb
$ echo $?
0
問題なくrequire 'sqlite3'が動作していることが確認できました。
以上、RubyにSQLite3のモジュールをインストールする手順でした。
#htmlinsertpcsp(db-btm.html,db-sp.html)