NO IMAGE

【Rails】はじめてのサーバを立てるまでのエラー&対応記録

NO IMAGE

以前の記事でUbuntu20.04にてrubyをインストールしましたが、今回はその環境でrailsアプリケーションサーバを立ち上げるところまでやってみたいと思います。railsの公式チュートリアルを基にした作業記録ですが、出くわしたエラーの対応も含めて書いています。今からrailsをやろう、楽しもうという同志の参考になればと。

下準備

rails & bundlerをバージョン指定して導入します。

gem install rails -v 6.0.3
gem install bundler -v 2.2.17

yarnの導入。当方はUbuntu上でやってみたいので、aptを利用してインストールしました。これで起こるエラーを僕達はまだ知らない。

sudo apt -y update
sudo apt -y upgrade
sudo apt -y install yarn

railsチュートリアル用のディレクトリを切ったら・・

mkdir ~/rails-tut
cd ~/rails-tut

rails new して新たなrailsアプリケーションを作成。

rails _6.0.3_ new hello_app

すると以下のような結果になりました。どうやらnodejsがないですよと言われているようです。

Bundle complete! 17 Gemfile dependencies, 75 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
         run  bundle binstubs bundler
         run  bundle exec spring binstub --all
* bin/rake: Spring inserted
* bin/rails: Spring inserted
       rails  webpacker:install
sh: 1: node: not found
sh: 1: nodejs: not found
Node.js not installed. Please download and install Node.js <https://nodejs.org/en/download/>

漢は黙ってsudo apt install。

sudo apt -y install nodejs

bundle install でちょいコケ

bundle install するところでちょっとコケました。

$ bundle _2.2.17_ install
Calling `DidYouMean::SPELL_CHECKERS.merge!(error_name => spell_checker)' has been deprecated. Please call `DidYouMean.correct_error(error_name, spell_checker)' instead.
Warning: the running version of Bundler (2.2.17) is older than the version that created the lockfile (2.3.4). We suggest you to upgrade to the version that created the lockfile by running `gem install bundler:2.3.4`.
Fetching gem metadata from <https://rubygems.org/>...........
You have requested:
  listen = 3.1.5

The bundle currently has listen locked at 3.7.1.
Try running `bundle update listen`

If you are updating multiple gems in your Gemfile at once,
try passing them all to `bundle update`

$ bundle update listen
Fetching gem metadata from <https://rubygems.org/>..........
Resolving dependencies...
Bundler found conflicting requirements for the Ruby version:
  In Gemfile:
    Ruby

    bootsnap (= 1.4.5) was resolved to 1.4.5, which depends on
      Ruby (>= 2.0.0)

    capybara (= 3.28.0) was resolved to 3.28.0, which depends on
      Ruby (>= 2.4.0)

    listen (= 3.1.5) was resolved to 3.1.5, which depends on
      Ruby (>= 2.2.3, ~> 2.2)

    puma (= 4.3.6) was resolved to 4.3.6, which depends on
      Ruby (>= 2.2)

    selenium-webdriver (= 3.142.4) was resolved to 3.142.4, which depends on
      Ruby (>= 2.3)

$ ruby -v
ruby 3.0.3p157 (2021-11-24 revision 3fb7d2cadc) [x86_64-linux]

調べてみると、Ruby ~> 2.2 の意味は「2.2以上、3.0未満」とのこと。なるほど。

いまのrubyはrbenv経由でインストールしたものなので、利用可能なバージョンを改めて確認します。

$ rbenv install -l
2.6.9
2.7.5
3.0.3
jruby-9.3.2.0
mruby-3.0.0
rbx-5.0
truffleruby-21.3.0
truffleruby+graalvm-21.3.0

2.7.5あたりが良さそう。インストールします。

$ rbenv install 2.7.5

ローカルにバージョン変更&確認。

$ rbenv local 2.7.5
$ rbenv version
2.7.5 (set by ~/rails-tut/hello_app/.ruby-version)

これで良さげです。再度bundle installしてみます。一回目は失敗して、エラーメッセージの指示どおりbundle updateやらなんやかんやしました。

$ bundle _2.2.17_ install
Traceback (most recent call last):
	2: from ~/.rbenv/versions/2.7.5/bin/bundle:23:in `<main>'
	1: from ~/.rbenv/versions/2.7.5/lib/ruby/2.7.0/rubygems.rb:296:in `activate_bin_path'
~/.rbenv/versions/2.7.5/lib/ruby/2.7.0/rubygems.rb:277:in `find_spec_for_exe': Could not find 'bundler' (2.3.4) required by your ~/rails-tut/hello_app/Gemfile.lock. (Gem::GemNotFoundException)
To update to the latest version installed on your system, run `bundle update --bundler`.
To install the missing version, run `gem install bundler:2.3.4`

$ bundle update --bundler
Warning: the running version of Bundler (2.1.4) is older than the version that created the lockfile (2.3.4). We suggest you to upgrade to the version that created the lockfile by running `gem install bundler:2.3.4`.
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for x86_64-linux, ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from <https://rubygems.org/>...........
Fetching gem metadata from <https://rubygems.org/>.
You have requested:
  listen = 3.1.5

The bundle currently has listen locked at 3.7.1.
Try running `bundle update listen`

If you are updating multiple gems in your Gemfile at once,
try passing them all to `bundle update`

$ bundle update
Warning: the running version of Bundler (2.1.4) is older than the version that created the lockfile (2.3.4). We suggest you to upgrade to the version that created the lockfile by running `gem install bundler:2.3.4`.
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for x86_64-linux, ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from <https://rubygems.org/>...........
Fetching gem metadata from <https://rubygems.org/>.
Resolving dependencies....
Fetching rake 12.3.3 (was 13.0.6)
...
Installing webpacker 4.0.7 (was 4.3.0)
Bundle updated!
Post-install message from sass:

Ruby Sass has reached end-of-life and should no longer be used.

* If you use Sass as a command-line tool, we recommend using Dart Sass, the new
  primary implementation: <https://sass-lang.com/install>

* If you use Sass as a plug-in for a Ruby web framework, we recommend using the
  sassc gem: <https://github.com/sass/sassc-ruby#readme>

* For more details, please refer to the Sass blog:
  <https://sass-lang.com/blog/posts/7828841>

$ bundle update --bundler
Warning: the running version of Bundler (2.1.4) is older than the version that created the lockfile (2.3.4). We suggest you to upgrade to the version that created the lockfile by running `gem install bundler:2.3.4`.
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby, x86_64-linux but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
...
Bundle updated!

$ bundle _2.2.17_ install
Traceback (most recent call last):
	2: from ~/.rbenv/versions/2.7.5/bin/bundle:23:in `<main>'
	1: from ~/.rbenv/versions/2.7.5/lib/ruby/2.7.0/rubygems.rb:296:in `activate_bin_path'
~/.rbenv/versions/2.7.5/lib/ruby/2.7.0/rubygems.rb:277:in `find_spec_for_exe': Could not find 'bundler' (2.3.4) required by your ~/rails-tut/hello_app/Gemfile.lock. (Gem::GemNotFoundException)
To update to the latest version installed on your system, run `bundle update --bundler`.
To install the missing version, run `gem install bundler:2.3.4`

んむ、ちょい沼りかけ・・。

この記事を参考に解決。

$ BUNDLER_VERSION=2.2.17 bundle install
Using rake 12.3.3
...
Using webpacker 4.0.7
Bundle complete! 17 Gemfile dependencies, 75 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

rails serverしたい人生だった

config/environments/development.rbに追記して・・

  # allow connections to local server
  config.hosts.clear

rails serverしてみます。

$ rails server
=> Booting Puma
=> Rails 6.0.3 application starting in development 
=> Run `rails server --help` for more startup options
RAILS_ENV=development environment is not defined in config/webpacker.yml, falling back to production environment
Exiting
Traceback (most recent call last):
        77: from bin/rails:3:in `<main>'
        76: from bin/rails:3:in `load'
...
~/.rbenv/versions/2.7.5/lib/ruby/gems/2.7.0/gems/webpacker-4.0.7/lib/webpacker/configuration.rb:91:in `rescue in load': Webpacker configuration file not found ~/rails-tut/hello_app/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_sysopen - ~/rails-tut/hello_app/config/webpacker.yml (RuntimeError)

どうやらrails webpacker:installする必要がありそう。やってみます。

$ rails webpacker:install
rails aborted!
ArgumentError: Malformed version number string 0.32+git
~/rails-tut/hello_app/bin/rails:9:in `<top (required)>'
~/rails-tut/hello_app/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => webpacker:install => webpacker:check_yarn
(See full trace by running task with --trace)

むむ、なにかバージョンの整合性がとれていなさそう。
⇒調べてみると、aptでインストールしたyarnのバージョンがダメそうでした。

一旦yarnをアンインストール。

$ sudo apt -y remove yarn

こちらの記事等を参考に、安定版を導入します。

$ sudo curl -sS <https://dl.yarnpkg.com/debian/pubkey.gpg> | sudo apt-key add -
$ echo "deb <https://dl.yarnpkg.com/debian/> stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
$ sudo apt update && sudo apt install yarn

$ yarn -v
1.22.17

これでもう一度rails webpacker:installを試してみます。

$ rails webpacker:install
RAILS_ENV=development environment is not defined in config/webpacker.yml, falling back to production environment
      create  config/webpacker.yml
Copying webpack core config
      create  config/webpack
      create  config/webpack/development.js
      create  config/webpack/environment.js
      create  config/webpack/production.js
      create  config/webpack/test.js
			...
Webpacker successfully installed 🎉 🍰

よしっ。いよいよ、rails server。

$ rails server
=> Booting Puma
=> Rails 6.0.3 application starting in development 
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 4.3.6 (ruby 2.7.5-p203), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://127.0.0.1:3000
* Listening on tcp://[::1]:3000
Use Ctrl-C to stop

サーバが立ちました!やったね。

参考