WindowsのRuby on Railsインストール手順(Ruby 2.4.3-1、Rails 5.1.4、mysql2 0.4.10)
2018/01/10
以前、Ruby 2.0.0を使用していたのですが、パソコン初期化してRuby on Railsを再インストールしようとしたところ、いろいろ変わっていたのでメモ書き。
以前の環境構築手順はこちら。
WindowsのRuby on Rails開発環境構築手順
なお、今回のバージョンは以下のとおりです。
- Windows 10 Pro(64bit)
- Ruby 2.4.3-1(x64)
- gem 2.7.4
- Rails 5.1.4
- mysql2 0.4.10
スポンサード リンク
Rubyのインストール
まずは、RubyInstallerをダウンロードする。
RubyInstaller公式サイトより、「Ruby 2.4.3-1(x64)」をダウンロードした。
2018年1月8日時点の最新バージョンは「Ruby 2.5.0-1(x64)」だが、安定版は2.4.Xだ。
また、2.5.0を使用した場合、Railsをインストールする際にnokogiri-1.8.1がインストールできくてめんどくさかったので、2.4.Xの方が良いかと。
ダウンロードしたインストーラーを実行する。
ライセンス同意して次へ。
インストールするフォルダをC直下や適当な位置にする。
このフォルダは「C:\Program Files」などの半角空白が入るものは避けたほうがいい。
そして、Ruby実行ファイルへの環境変数PATH追加をチェックしておく。
このチェックでPATHへ追加されるのは、ユーザー環境変数のPATHに追加されてたが、後でシステム環境変数のPATHの方へ追加しておくのがいいと思う。
インストール完了画面に、「MSYS2のインストールを実行するか」のチェックがあるので、チェックオンにして終了する。
すると、コマンドプロンプトが立ち上がるので、1・2・3と順に入力してインストールしていく。
これは「ridk install」というコマンドでも開くことができる。
Ruby 2.4.0より前は「DEVELOPMENT KIT」というものが必要だったが、2.4.0以降はRubyInstallerにこの「MSYS2 toolkit」が含まれて、「DEVELOPMENT KIT」は不要みたいだ。
Rubyがインストールできたら、バージョンを確認。
1 2 |
>ruby -v ruby 2.4.3p205 (2017-12-14 revision 61247) [x64-mingw32] |
gemのアップデート
Rubyがインストールできたら、gemのアップデートを行う。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
>gem -v 2.6.14 >gem list *** LOCAL GEMS *** bigdecimal (default: 1.3.0) did_you_mean (1.1.0) io-console (default: 0.4.6) json (default: 2.0.4) minitest (5.10.1) net-telnet (0.1.1) openssl (default: 2.0.5) power_assert (0.4.1) psych (default: 2.2.2) rake (12.0.0) rdoc (default: 5.0.0) test-unit (3.2.3) xmlrpc (0.2.1) >gem update --system >gem -v 2.7.4 >gem list *** LOCAL GEMS *** bigdecimal (default: 1.3.0) bundler (default: 1.16.0) did_you_mean (1.1.0) io-console (default: 0.4.6) json (default: 2.0.4) minitest (5.10.1) net-telnet (0.1.1) openssl (default: 2.0.5) power_assert (0.4.1) psych (default: 2.2.2) rake (12.0.0) rdoc (default: 5.0.0) rubygems-update (2.7.4) test-unit (3.2.3) xmlrpc (0.2.1) |
Railsのインストール
続いて、Railsのインストール行う。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
>gem install rails Done installing documentation for concurrent-ruby, i18n, thread_safe, tzinfo, activesupport, rack, rack-test, mini_portile2, nokogiri, crass, 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, thor, method_source, railties, sprockets, sprockets-rails, rails after 55 seconds 35 gems installed >rails -v Rails 5.1.4 >gem list rails *** LOCAL GEMS *** rails (5.1.4) rails-dom-testing (2.0.3) rails-html-sanitizer (1.0.3) sprockets-rails (3.2.1) |
mysql2について
アプリケーションでmysql2を使用していたのだが、環境作るのにものすごいつまづいたので書き残しておく。
まず、アプリケーションのフォルダで「bundle install」コマンドでgemインストールを行う。
「–path vendor/bundle」で、システムのgemではなく、アプリケーション内でgemをインストールした。
何やら「MySQL Connector/C」というものを使用してビルドしろとメッセージが。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
>bundle install --path vendor/bundle … Fetching mysql2 0.4.10 (x64-mingw32) Installing mysql2 0.4.10 (x64-mingw32) … Bundle complete! 33 Gemfile dependencies, 93 gems now installed. Bundled gems are installed into `./vendor/bundle` Post-install message from mysql2: ====================================================================================================== You've installed the binary version of mysql2. It was built using MySQL Connector/C version 6.1.11. It's recommended to use the exact same version to avoid potential issues. At the time of building this gem, the necessary DLL files were retrieved from: http://cdn.mysql.com/Downloads/Connector-C/mysql-connector-c-6.1.11-win32.zip This gem *includes* vendor/libmysql.dll with redistribution notice in vendor/README. ====================================================================================================== |
とりあえず、MySQL公式サイトより、「MySQL Connector/C」をダウンロードして、適当な場所へ配置する。
以下のコマンドでbundlerのビルドオプションへ追加すると、「ユーザーディレクトリ\.bundle\config」に設定された。
「アプリケーションディレクトリ\.bundle\config」には追加されないのか?よくわからん。。
この状態で再度「bundle install」を実行するが、変化がない。
ビルドオプションが効いていない?
1 2 3 |
>bundle config build.mysql2 '--with-mysql-dir="C:\Tools\mysql-connector-c-6.1.11-winx64"' >bundle install |
ひとまず放置してマイグレーションを行ってみたが、Loadエラー。
そもそも「mysql2 0.4.10 (x64-mingw32)」ってのはまだRuby 2.4.Xに対応していないみたい。
gemのフォルダ内を見てみたが、2.3までのフォルダ・バイナリファイルはあるが、2.4のフォルダがない。
1 2 3 |
>bundle exec rake db:migrate rake aborted! LoadError: cannot load such file -- mysql2/2.4/mysql2 |
えー、めんどくさい。。
と思いつつ調べてると、mysql2 README-GitHubにWindowsに関する記載があった。
要約すると、こんな感じか。
- mysql2のgemはデフォルトでmysql.comからMySQL Connector/Cをダウンロードして使用する。ローカルのものを使用する場合は、「–with-mysql-dir=c:/mysql-connector-c-x-y-z」を付ける。(パスはスラッシュ)
- デフォルトでmysql2のgemディレクトリに「libmysql.dll」がコピーされる。それを防ぐには、「–no-vendor-libmysql」を付ける。
- 上記で防いだ場合、「libmysql.dll」は環境変数「RUBY_MYSQL2_LIBMYSQL_DLL」、mysql2のgemディレクトリ「vendor/libmysql.dll」、システム環境変数「PATH」の3つから順に探して見つかったものを使用する。
ふむふむ。
じゃあ、「bundle install」でビルドオプションが効かなかったので、「gem install」で直接インストールしてみてはどうか。
上記の通りオプションを入力し、MySQL Connector/Cの「libmysql.dll」ファイルはruby.exeと同じフォルダに配置。
Rubyインストール時には、システム環境変数PATHには登録されておらず、ユーザー環境変数PATHにしか登録されていないので注意。
1 2 3 4 5 6 7 8 9 10 11 |
>bundle exec gem install mysql2 --platform=ruby -- '--no-vendor-libmysql --with-mysql-dir="C:/Tools/mysql-connector-c-6.1.11-winx64"' Fetching: mysql2-0.4.10.gem (100%) Temporarily enhancing PATH for MSYS/MINGW... Building native extensions with: '--no-vendor-libmysql --with-mysql-dir="C:/Tools/mysql-connector-c-6.1.11-winx64"' This could take a while... Successfully installed mysql2-0.4.10 Parsing documentation for mysql2-0.4.10 Installing ri documentation for mysql2-0.4.10 Done installing documentation for mysql2 after 0 seconds 1 gem installed |
お、いけたっぽい。
さっきビルドオプションが効かなかったのは、パスの記述がスラッシュではなく¥マークだったからか…?
とりあえず「x64-mingw32」の方をアンインストール。
1 2 3 4 5 6 7 8 |
>bundle exec gem uninstall mysql2 Select gem to uninstall: 1. mysql2-0.4.10 2. mysql2-0.4.10-x64-mingw32 3. All versions > 2 Successfully uninstalled mysql2-0.4.10-x64-mingw32 |
Gemfile.lockを直接修正しておく。
1 2 3 4 5 |
>修正前 mysql2 (0.4.10-x64-mingw32) >修正後 mysql2 (0.4.10) |
再び、マイグレーションするとエラーなくなり通った。
アプリケーション動いたし、とりあえずこれでいいか。
1 2 3 |
>bundle exec rake db:migrate >rails s |
WindowsでのRuby on Railsは環境構築でつまづくこと山のごとし。
以上。