LoginSignup
2
0

Rails 6.0.3でRuby 2.6.6の環境構築

Last updated at Posted at 2024-04-05

 Ruby on Railsなるものを勉強してみようと『パーフェクトRuby on Rails【増補改訂版】』に手を出しました。

 数年前の書籍だから完全に同じ環境を構築する必要も無さそうだけど、出来るだけ同じ環境で勉強してみることにしました。意外と手間が掛かったので記録しておきます。

 書籍の「Appendix Railsの開発環境構築」に関しては特に問題は無かったので省略して、Railsのversion 6.0.3をインストールする部分を説明していきます。

 まず普通にgem install rails -v 6.0.3をすると下記のようにnokogiriライブラリに関するエラーが出ます。

$ gem install rails -v 6.0.3
(中略)
Successfully installed activesupport-6.0.3
Building native extensions. This could take a while...
/home/user/.rbenv/versions/2.6.6/lib/ruby/2.6.0/rubygems/ext/builder.rb:76: warning: Insecure world writable dir /mnt/c in PATH, mode 040777
Successfully installed racc-1.7.3
ERROR:  Error installing rails:
        The last version of nokogiri (>= 1.8.5) to support your Ruby & RubyGems was 1.13.10. Try installing it with `gem install nokogiri -v 1.13.10` and then running the current command again
        nokogiri requires Ruby version >= 3.0, < 3.4.dev. The current ruby version is 2.6.6.146.

 nokogiriライブラリのバージョン1.8.5以上の中で、君のRuby環境で対応している最新は1.13.10ですよ、と言ってくれています。また、インストールしようとしていたnokogiriはRuby 3.0以上が必要だけど、君のRubyは2.6.6だよね、とも言ってくれています。

 RubyGems.orgで確認すると、確かにnokogiriの1.13.10はRubyの2.6以上、3.2.dev未満に対応しているようです。

 大人しく最新のRubyを使って、最新のRails環境を構築するのがいいんでしょうけれど。ここでは、Ruby 2.6.6に対応しているnokogiriくんを用意しましょう。

$ gem install nokogiri -v 1.13.10
Fetching nokogiri-1.13.10-x86_64-linux.gem
/home/user/.rbenv/rbenv.d/exec/gem-rehash/rubygems_plugin.rb:6: warning: Insecure world writable dir /mnt/c in PATH, mode 040777
Successfully installed nokogiri-1.13.10-x86_64-linux
Parsing documentation for nokogiri-1.13.10-x86_64-linux
Installing ri documentation for nokogiri-1.13.10-x86_64-linux
Done installing documentation for nokogiri after 2 seconds
1 gem installed

さて、これで再度gem install rails -v 6.0.3を実行すると、またエラーが発生します。

$ gem install rails -v 6.0.3
ERROR:  Error installing rails:
        There are no versions of nokogiri (~> 1.14) compatible with your Ruby & RubyGems. Maybe try installing an older version of the gem you're looking for?
            nokogiri requires Ruby version >= 3.0, < 3.4.dev. The current ruby version is 2.6.6.146.

 nokogiriの1.14以上のバージョンがないよ、インストールしようとしているgemの古いバージョンを探してみたらいいんじゃないか、と言ってくれているようです。これに関しては下記の記事が詳しいです。

 gemの依存関係を調べてみたら「rails-html-sanitizer」くんのバージョンが原因らしく、これのv1.6.0がnokogiriの1.14以上を必要としているからみたいですね。というわけで、これの前のバージョン1.5.0をインストールしておきます。

$ gem install rails-html-sanitizer -v 1.5.0
Fetching rails-html-sanitizer-1.5.0.gem
Successfully installed crass-1.0.6
Successfully installed loofah-2.22.0
Successfully installed rails-html-sanitizer-1.5.0
Parsing documentation for crass-1.0.6
Installing ri documentation for crass-1.0.6
Parsing documentation for loofah-2.22.0
Installing ri documentation for loofah-2.22.0
Parsing documentation for rails-html-sanitizer-1.5.0
Installing ri documentation for rails-html-sanitizer-1.5.0
Done installing documentation for crass, loofah, rails-html-sanitizer after 0 seconds
3 gems installed

さて、これで再度gem install rails -v 6.0.3を実行すると、またエラーが発生します。

$ gem install rails -v 6.0.3
(中略)
Successfully installed net-pop-0.1.2
ERROR:  Error installing rails:
        The last version of net-imap (>= 0) to support your Ruby & RubyGems was 0.3.7. Try installing it with `gem install net-imap -v 0.3.7` and then running the current command again
        net-imap requires Ruby version >= 2.7.3. The current ruby version is 2.6.6.146.

 net-imapライブラリのバージョン0以上で君のRuby環境で対応している最新は0.3.7ですよ、と言ってくれています。インストールしようとしていたnet-imapはRuby 2.7.3以上が必要だけど、君のRubyは2.6.6だよね、とも言ってくれています。

$ gem install net-imap -v 0.3.7
Fetching net-imap-0.3.7.gem
Successfully installed net-imap-0.3.7
Parsing documentation for net-imap-0.3.7
Installing ri documentation for net-imap-0.3.7
Done installing documentation for net-imap after 0 seconds
1 gem installed

 さて、これでようやくgem install rails -v 6.0.3が成功します。しかし、これがゴールではありません。Railsを始めたいのですからrails newコマンドを成功させなければなりません。

 というわけで実行してみるとsqlite3に関するエラーが発生します。

$ rails new hello_rails
(中略)
Resolving dependencies....
sqlite3-1.6.9-x86_64-linux requires ruby version >= 2.7, < 3.3.dev, which is incompatible with the current version, ruby
2.6.6p146
(後略)

 Gemfileの中を確認すると、railsは6.0.3以上6.1未満、sqlite3は1.4以上2.0以下の指定です。よって、これでbundle installするとrailsは6.0.6.1が、sqlite3は1.7.3がインストールされます。

Gemfile
(前略)
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.3'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '~> 1.4'
(後略)

 railsはインストールしたい6.0.3に固定して、sqlite3は2.6.6が対応している最新バージョンをRubyGems.orgで探して1.5.4に固定することとしました。

Gemfile
(前略)
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '6.0.3'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '1.5.4'
(後略)

 new railsをすると勝手にbundle installまでされてしまうので、--skip-bundleオプションを付けます。

 しかし、まだエラーは発生します。rails newを行うとrails webpacker:installも自動的に実行されるようなのですが、これでエラーが発生します。

$ rails new hello_rails --skip-bundle
(中略)
rails  webpacker:install
(中略)
Installing dev server for live reloading
         run  yarn add --dev webpack-dev-server from "."
yarn add v1.22.22
[1/4] Resolving packages...
[2/4] Fetching packages...
error http-proxy-middleware@2.0.6: The engine "node" is incompatible with this module. Expected version ">=12.0.0". Got "10.19.0"
error Found incompatible module.
(後略)

 nodeのバージョンが12.0.0以上が必要だけど、10.19.0が入ってるよ、と言ってくれています。nodejsのバージョン管理ソフトのnをインストールして、nodejsの12の最新版である12.22.12をインストールします。

sudo npm install -g n
sudo n 12.22.12

 ターミナルを再起動するかhash -rして、node -vでバージョンを確認します。

$ node -v
v12.22.12

 しかし、まだエラーは発生します。

$ rails new hello_rails --skip-bundle
(中略)
rails  webpacker:install
(中略)
Installing dev server for live reloading
         run  yarn add --dev webpack-dev-server from "."
yarn add v1.22.22
[1/4] Resolving packages...
[2/4] Fetching packages...
error open@10.1.0: The engine "node" is incompatible with this module. Expected version ">=18". Got "12.22.12"
error Found incompatible module.
(後略)

 なんと、nodejsのバージョンが18以上が欲しいよ、と言ってきています。もう仕方ないので最新にでも上げることにします。

$ sudo n stable
installing : node-v20.12.1
       mkdir : /usr/local/n/versions/node/20.12.1
       fetch : https://nodejs.org/dist/v20.12.1/node-v20.12.1-linux-x64.tar.xz
     copying : node/20.12.1
   installed : v20.12.1 (with npm 10.5.0)
$ node -v
v20.12.1

 まだエラーは出ます。

$ rails new hello_rails --skip-bundle
(中略)
rails  webpacker:install
(中略)
error /home/user/hello_rails6/node_modules/node-sass: Command failed.
Exit code: 1
Command: node scripts/build.js
(中略)
gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
(後略)

 node-sassというものがエラーを生じているようです。webpackerで使用しているnode-sassのバージョンに問題があるらしいです。とりあえず、2.6.6が対応している最新版に上げることにします。

Gemfile
gem 'webpacker', '5.4.4'

 これで、ようやくエラーが出なくなります。rails newコマンドでプロジェクトを作成。

rails new hello_rails --skip-bundle --skip-webpacker-install
cd hello_rails

 作成したフォルダ内のGemfileを修正して、各種コマンドを実行。

bundle install
bundle binstubs bundler
bundle exec spring binstub --all
rails webpacker:install

 これでようやくスタート地点。いやはや、環境構築って難しいですね。

2
0
0

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
2
0