Ruby on Rails

作成日 : 2019-04-29
最終更新日 :

インストール

Ruby on Rails は Ruby で作成されたオープンソースのWebアプリケーションフレームワークである。 日本では Rails と、外国では RoR と略されることも多い。本ページでは日本流で行く。

Windows 10 に入れてみる

まず Ruby のインストール

2019-04-27 に Ruby から入れてみた。その日の日記である。

https://rubyinstaller.org/downloads/ に行く。

RubyInstaller は WITH DEVKIT と WITHOUT DEVKIT とある。Rails では WITH DEVKIT を選ぶ。 ちなみに選ぶべきバージョンは => で指示されているものだろう。私が使っている Windows 10 (Home) では、 Ruby+Devkit 2.5.5.1(x64) が指示されているので、このバージョンをインストールした。

インストーラーを起動すると、英語で[Setup - Ruby2.5.5-1-x64 with MSYS2] という画面になる。使用許諾契約書の同意(に相当する英語バージョン、以下同様)のダイアログウィンドウのラジオボタンでは、I Agree を選択して次に進む。 次に、インストール先とオプションの設定では、ラジオボタンが 3 個出てくるが、 Tcl/Tk ……の選択肢はなく、かわりに MSYS2 のなんたらかんたら、 がある。デフォルトでラジオボタン 2 個がチェックされているのでそれでよいものとして次に進む。 ちなみに、MSYS2 とは Windows 上で動作する Unix シェルやコマンド群などを統合したパッケージである。Rails では必須である。

その後インストール作業が継続する。私のコンピュータでは 20 分かかった。SSD だが CPU は遅い。

インストールが完了した旨のダイアログが出てくるので[Finish]ボタンをクリックすると、次の DOS 窓が出てくる。

(前略)
   1 - MSYS2 base installation
   2 - MSYS2 repository update
   3 - MSYS2 and MINGW development toolchain
Which components shall be installed? If unsure press Enter [1,2,3] 

最後の文は、「わからなければ(単に)Enter を押せ」だから、そのようにする。
> sh -lc
という表示が自動的になされ、その後もかなりの行が同じく自動的に出るが、うまくやっているものと信じる。最後にまた上記の画面が出たので、 Ctrl-C で DOS 窓を閉じる。

では、Ruby が正しくインストールされているか、確かめよう。以下、> はプロンプトで、下線部はキーボード入力部である。

> ruby --version
ruby 2.5.5p157 (2019-03-15 revision 67260) [x64-mingw32]

正しくインストールされている。

SQLite

データベースマネジメントシステム (DBMS) の利用はほぼ必須である。Rails のデフォルトは SQLite である。 これは別口からインストールしなければならない。

こちらは別にインストールしているので飛ばす。

Node.js

Node.js は JavaScript のサーバー用ライブラリ。サーバー用ではあるが、いろいろな便利なコマンドがあり、 やはり Rails を利用するためには入れておかなければならない。

Node.js は、私の PC にはインストールされていない( node コマンドが「見つからない」と出てくるため)。 最新安定版を入手する。ここでは、LTS 推奨版の 64-bit .msi を選ぶ。 最新のバージョン: 10.15.3 (同梱 npm 6.4.1)とある。

デフォルトのインストールフォルダはC:\Program Files\nodejs\である。このスペースがあるフォルダは気に食わないが、仕方がない。 そのまま進める。

インストール完了後、正しくインストールされているか調べる。

> node
v10.15.3

安心した。

なお、Node.js は他のアプリでも当然のようにインストールされているべきライブラリである。 たとえば、Puppeteer などがある。Puppeteer 入門にも Node.js が言及されている。

Rails のインストール

さて Rails のインストールには gem コマンドを使う。その前に、gem コマンドが使えることを確認しておく。

> gem --version
2.7.6.2

gem がインストールされていることがわかった。では Rails をインストールする。

> gem install rails
Fetching: concurrent-ruby-1.1.5.gem (100%)
Successfully installed concurrent-ruby-1.1.5
Fetching: i18n-1.6.0.gem (100%)

HEADS UP! i18n 1.1 changed fallbacks to exclude default locale.
But that may break your application.

Please check your Rails app for 'config.i18n.fallbacks = true'.
If you're using I18n (>= 1.1.0) and Rails (< 5.2.2), this should be
'config.i18n.fallbacks = [I18n.default_locale]'.
If not, fallbacks will be broken in your app by I18n 1.1.x.

For more info see:
https://github.com/svenfuchs/i18n/releases/tag/v1.1.0

Successfully installed i18n-1.6.0
Fetching: thread_safe-0.3.6.gem (100%)
Successfully installed thread_safe-0.3.6
Fetching: tzinfo-1.2.5.gem (100%)
…中略…
Parsing documentation for rails-5.2.3
Installing ri documentation for rails-5.2.3
Done installing documentation for concurrent-ruby, i18n, thread_safe, tzinfo, activesupport, rack, rack-test, crass, 
mini_portile2, nokogiri, loofah, rails-html-sanitizer, rails-dom-testing, builder, erubi, actionview, actionpack, 
activemodel, arel, activerecord, globalid, activejob, mini_mime, mail, actionmailer, nio4r, websocket-extensions, 
websocket-driver, actioncable, mimemagic, marcel, activestorage, thor, method_source, railties, bundler, sprockets, sprockets-rails, 
rails after 401 seconds
39 gems installed

新規のアプリを作成する

私は、C:\users\%my_account%\documents の配下にアプリを作成することにした。 ここで %my_account% は、Windows にある私のアカウント名である。

> cd c:\\users\%my_account%\documents
> rails new railbook
    create
    create README.md
    …中略…
Fetching sqlite3 1.4.0
Installing sqlite3 1.4.0 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.0/ext/sqlite3
C:/Ruby25-x64/bin/ruby.exe -r ./siteconf20190427-12768-jjfdbh.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
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=C:/Ruby25-x64/bin/$(RUBY_BASE_NAME)
        --with-sqlcipher
        --without-sqlcipher
        --with-sqlite3-config
        --without-sqlite3-config
        --with-pkg-config
        --without-pkg-config
        --with-sqlcipher
        --without-sqlcipher
        --with-sqlite3-dir
        --without-sqlite3-dir
        --with-sqlite3-include
        --without-sqlite3-include=${sqlite3-dir}/include
        --with-sqlite3-lib
        --without-sqlite3-lib=${sqlite3-dir}/lib

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.0/mkmf.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.0 for inspection.
Results logged to
C:/Ruby25-x64/lib/ruby/gems/2.5.0/extensions/x64-mingw32/2.5.0/sqlite3-1.4.0/gem_make.out

An error occurred while installing sqlite3 (1.4.0), and Bundler cannot continue.
Make sure that `gem install sqlite3 -v '1.4.0' --source 'https://rubygems.org/'`
succeeds before bundling.

In Gemfile:
  sqlite3

こうなった原因はうすうすわかる。私は sqlite3 を別口でもってきて配置したからだ。sqlite3 がある場所はこうなっている。

> dir c:\users\%my_account\sqlite
sqldiff.exe sqlite3.exe sqlite3_analyzer.exe

ここは Ruby とは無関係である。しかし、本書によれば、Ruby のバイナリフォルダーに配置しなければならない。 ということで、上にある sqlite3.exe を Ruby のバイナリフォルダーにコピーした。これでいいと思って再度行ってみたが、やはり同じエラーが出る。 ということは、エラーの原因は sqlite3 のパスの場所ではない、と思えてきた。そして下記のエラーを再度観察した。

An error occurred while installing sqlite3 (1.4.0), and Bundler cannot continue. Make sure that `gem install sqlite3 -v '1.4.0' --source 'https://rubygems.org/'` succeeds before bundling.

ということは、上記コマンドが成功するかを試してみればよい。

> cd c:\Ruby25-x64
> gem install sqlite3 -v '1.4.0' --source 'https://rubygems.org/'

詳細は略すがやはり失敗する。下記を参考にして、前のバージョンをインストールすることにした。
 https://teratail.com/questions/174081

> cd c:\Ruby25-x64
> gem install sqlite3 -v '1.3.13' --source 'https://rubygems.org/'
Fetching: sqlite3-1.3.13-x64-mingw32.gem (100%)
Successfully installed sqlite3-1.3.13-x64-mingw32
Parsing documentation for sqlite3-1.3.13-x64-mingw32
Installing ri documentation for sqlite3-1.3.13-x64-mingw32
Done installing documentation for sqlite3 after 12 seconds
1 gem installed

気を取り直して再度 railbook という名前のアプリケーションを作ろうとする。
> rails new railbook
しかし、見に行くのは 1.4.0 であり失敗する。

ここで立ち止まって考えてみた。この状況が生じているのは次の理由からである。

したがって、次のどちらかを実現すれば解決すると思われる。

  1. rails new の gem への要求を、sqlite3 1.3.13 (以下)に下げる
  2. sqlite3 の gem を 1.4.0 に上げる。

https://qiita.com/flowerhill/items/bb1e99bd87b151c0129b
を見ると、rails new が sqlite3 の gem 指定として 1.3.6 以上でOK,という指定をすればよい、と読める。 しかし、gem への要求は既に 1.3.13 (以下)に下げている。

sqlite3 の gem を 1.4.0 に上げようとするとどうなるか。

> gem install sqlite3 -v 1.4.0
Temporarily enhancing PATH for MSYS/MINGW...
Installing required msys2 packages: mingw-w64-x86_64-sqlite3
警告: mingw-w64-x86_64-sqlite3-3.28.0-1 は最新です -- スキップ
Building native extensions. This could take a while...
ERROR:  Error installing sqlite3:
        ERROR: Failed to build gem native extension.

    current directory: C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/sqlite3-1.4.0/ext/sqlite3
C:/Ruby25-x64/bin/ruby.exe -r ./siteconf20190427-7476-14ml2bv.rb extconf.rb
checking for sqlite3.h... yes
checking for pthread_create() in -lpthread... yes
checking for -ldl... no
checking for dlopen()... no
missing function dlopen
*** 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
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=C:/Ruby25-x64/bin/$(RUBY_BASE_NAME)
        --with-sqlcipher
        --without-sqlcipher
        --with-sqlite3-config
        --without-sqlite3-config
        --with-pkg-config
        --without-pkg-config
        --with-sqlcipher
        --without-sqlcipher
        --with-sqlite3-dir
        --without-sqlite3-dir
        --with-sqlite3-include
        --without-sqlite3-include=${sqlite3-dir}/include
        --with-sqlite3-lib
        --without-sqlite3-lib=${sqlite3-dir}/lib
        --with-pthreadlib
        --without-pthreadlib
        --with-dllib
        --without-dllib

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.0/mkmf.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.0 for inspection.
Results logged to C:/Ruby25-x64/lib/ruby/gems/2.5.0/extensions/x64-mingw32/2.5.0/sqlite3-1.4.0/gem_make.out

つまり失敗である。どうすれば成功するか。sqlite3 とか、1.4.0 とかそれらしいキーワードを入れて検索エンジンで探したところ、 この問題に対する対処方法が掲載されている下記のサイトを見つけた。
https://github.com/sparklemotion/sqlite3-ruby/issues/259
私は Fix 2a の対処をすることにした。

> ridk exec pacman -S mingw-w64-x86_64-dlfcn
依存関係を解決しています...
衝突するパッケージがないか確認しています...

パッケージ (1) mingw-w64-x86_64-dlfcn-1.1.2-1

合計ダウンロード容量:  0.03 MiB
合計インストール容量:  0.03 MiB

:: インストールを行いますか? [Y/n] Y
:: パッケージを取得します...
 mingw-w64-x86_64-dlfcn-1.1.2-1-any              27.6 KiB  26.9M/s 00:00 [#######################################] 100%
(1/1) キーリングのキーを確認                                             [#######################################] 100%
(1/1) パッケージの整合性をチェック                                       [#######################################] 100%
(1/1) パッケージファイルのロード                                         [#######################################] 100%
(1/1) ファイルの衝突をチェック                                           [#######################################] 100%
(1/1) 空き容量を確認                                                     [#######################################] 100%
:: パッケージの変更を処理しています...
(1/1) インストール mingw-w64-x86_64-dlfcn                                [#######################################] 100%

どうやらうまくいったようだ。再度挑戦する。

> rails new railbook
    create
    create README.md
    …中略…
Fetching sqlite3 1.4.0
Installing sqlite3 1.4.0 with native extensions
Fetching turbolinks-source 5.2.0
Installing turbolinks-source 5.2.0
    …中略…
Bundle complete! 16 Gemfile dependencies, 77 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

安心した。

リンク

まりんきょ学問所Ruby の浮き輪 > Sinatra


MARUYAMA Satosi