Rails 6 の環境セットアップ
「独習 Ruby on Rails(2019年6月19日 初版第一刷)」を一通り読みました。
本のほうでは、vagrant環境を構築して、rails 5.2.2 を導入していたのですが、せっかくなので Virtual Box 上の Ubuntu に rails 6 を一から入れ直してみようということでやってみたのですが、Rails 5 と Rails 6 で少し違いがあるみたいなので、メモとして残します。
導入環境
OS
導入済みパッケージ
「独習 Ruby on Rails」の構築手順にしたがって、導入をすすめました。Rails 5.2.2 のときは、この手順で問題なく動作していました。
事前導入パッケージ
apt-get でインストールします 。
sudo apt-get install git g++ make libreadline-dev libssl-dev zlib1g-dev nodejs
rbenv 導入
github のサイトから clone で、ホームディレクトリの .rbenv ディレクトリに展開します。
その後、~/.rbenv/bin へのパスを通し、init コマンドをシェル起動時に自動実行するように設定します。
- 独習本では、.profile に設定するように記載されていますが、.rbenv の公式では、.bashrc に書け、となっていました。 実際には、環境とその人の好みでいいような気がします。自分の場合は、.profile がなかったので、.bash_profile を使いました。
git clone https://github.com/rbenv/rbenv.git ~/.rbenv echo 'export PATH="~/.rbenv/bin:$PATH"' >> ~/.bash_profile echo 'eval "$(rbenv init -)"' >> ~/.bashrc
rbenv で ruby-build、ruby を導入
rbenv で ruby をインストールします。加えて、Rubyフレームワーク railties も。
rbenv install 2.7.1 rbenv global 2.7.1 rbenv rehash sudo apt-get install ruby-railties
bundler, rails のインストール
bundler という、gemの依存関係を自動で解決してくれる gem を先に入れておきます。続けて、rails も入れます。 何も考えずに最新版入れてます。
gem install bundler gem install rails
SQLite3 のインストール
今回は SQLite3 を使うので、下記コマンドでインストールします。
libsqlite3-dev
が無いと、rails の新規アプリケーション作成 rails new
でエラーになります。(実際入れ忘れた・・・)
sudo apt-get install sqlite3 sudo apt-get install libsqlite3-dev
ここまでで入ったパッケージのバージョン
今回いれたもののバージョンは下記の通りです。rails は 6 が入っています。
コマンド | バージョン |
---|---|
git --version | git version 2.25.1 |
rbenv -v | rbenv 1.1.2-30-gc879cb0 |
ruby -v | ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux] |
bundler -v | Bundler version 2.1.4 |
rails -v | Rails 6.0.3.2 |
sqlite3 --version | 3.31.1 2020-01-27 19:55:54 |
nodejs -v | v10.19.0 |
rails new で新規アプリケーションを作成
作業用ディレクトリを作って、その中に rails アプリケーションを作成します。
rails 5.2.2 では特に何も文句は言われないのですが、rails 6 のせいか、、、
$ rails new MyFirstApp create create README.md (中略) remove config/initializers/new_framework_defaults_6_0.rb run bundle install The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for 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...... Using rake 13.0.1 Using concurrent-ruby 1.1.6 (中略) Using webpacker 4.2.2 Bundle complete! 17 Gemfile dependencies, 74 gems now installed. Use `bundle info [gemname]` to see where a bundled gem is installed. run bundle binstubs bundler The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for 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`. run bundle exec spring binstub --all * bin/rake: Spring inserted * bin/rails: Spring inserted rails webpacker:install Yarn not installed. Please download and install Yarn from https://yarnpkg.com/lang/en/docs/install/
最後に「Yarn が入ってないから入れてね」と言われます。でもディレクトリ内にファイルは展開されてるように見えます。
しかし、このまま無視して(コラ)rails s
でサーバー起動しようとしても、大量のトレースバックと共にエラーで終了してしまいます。
$ rails s => Booting Puma => Rails 6.0.3.2 application starting in development => Run `rails server --help` for more startup options Exiting Traceback (most recent call last): 77: from bin/rails:3:in `<main>' (中略) 1: from /home/****/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/webpacker-4.2.2/lib/webpacker/configuration.rb:92:in `read' /home/****/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/webpacker-4.2.2/lib/webpacker/configuration.rb:92:in `read': No such file or directory @ rb_sysopen - /home/****/Rails_app/MyFirstApp/config/webpacker.yml (Errno::ENOENT) 76: from bin/rails:3:in `<main>' (中略) 1: from /home/****/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/webpacker-4.2.2/lib/webpacker/configuration.rb:91:in `load' /home/****/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/webpacker-4.2.2/lib/webpacker/configuration.rb:95:in `rescue in load': Webpacker configuration file not found /home/****/Rails_app/MyFirstApp/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_sysopen - /home/****/Rails_app/MyFirstApp/config/webpacker.yml (RuntimeError)
どうやら「webpacker」という gem でエラーが出ています。「configuration file not found」とか「No such file or directory」の文字が見えるので、ちゃんと入ってないってことみたいです。 'rails webpacker:install` を実行してね、とも書いてるので、やってみると、、、
$ rails webpacker:install Yarn not installed. Please download and install Yarn from https://yarnpkg.com/lang/en/docs/install/
やっぱり、Yarn が入ってないから入れてね、と言われます。
Yarn のインストール
調べてみると、Yarn もパッケージ管理ツールみたいです。
ともかく、言われた通りに上のURLにアクセスし、Yarn をインストールします。
OS は Ubuntu なので、Debian / Ubuntu のやり方で。
公式のやり方をそのまま使います。
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
※ ここで、curl が無いよ、と言われたので、apt-get で curl を入れました。
sudo apt update && sudo apt install yarn
一応、バージョンも確認しておきます。
$ yarn --version 1.22.4
もう一度、rails new で新規アプリケーションを作成
さっき作った新規アプリケーションは失敗しているので、ディレクトリごと削除して、再作成します。
$ rails new MyFirstApp create create README.md (中略) Using webpacker 4.2.2 Bundle complete! 17 Gemfile dependencies, 74 gems now installed. Use `bundle info [gemname]` to see where a bundled gem is installed. run bundle binstubs bundler The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for 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`. run bundle exec spring binstub --all * bin/rake: Spring inserted * bin/rails: Spring inserted rails webpacker:install <<<<<< さっきはここで止まった <<<<<< 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 Copying postcss.config.js to app root directory create postcss.config.js Copying babel.config.js to app root directory create babel.config.js Copying .browserslistrc to app root directory create .browserslistrc The JavaScript app source directory already exists apply /home/****/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/webpacker-4.2.2/lib/install/binstubs.rb Copying binstubs exist bin create bin/webpack create bin/webpack-dev-server append .gitignore Installing all JavaScript dependencies [4.2.2] run yarn add @rails/webpacker@4.2.2 from "." yarn add v1.22.4 info No lockfile found. [1/4] Resolving packages... info There appears to be trouble with your network connection. Retrying... warning @rails/webpacker > node-sass > request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142 warning @rails/webpacker > node-sass > node-gyp > request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142 warning @rails/webpacker > webpack > watchpack > watchpack-chokidar2 > chokidar@2.1.8: Chokidar 2 will break on node v14+. Upgrade to chokidar 3 with 15x less dependencies. warning @rails/webpacker > webpack > watchpack > watchpack-chokidar2 > chokidar > fsevents@1.2.13: fsevents 1 will break on node v14+ and could be using insecure binaries. Upgrade to fsevents 2. warning @rails/webpacker > webpack > micromatch > snapdragon > source-map-resolve > resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated warning @rails/webpacker > webpack > micromatch > snapdragon > source-map-resolve > urix@0.1.0: Please see https://github.com/lydell/urix#deprecated [2/4] Fetching packages... info fsevents@2.1.3: The platform "linux" is incompatible with this module. info "fsevents@2.1.3" is an optional dependency and failed compatibility check. Excluding it from installation. info fsevents@1.2.13: The platform "linux" is incompatible with this module. info "fsevents@1.2.13" is an optional dependency and failed compatibility check. Excluding it from installation. [3/4] Linking dependencies... [4/4] Building fresh packages... success Saved lockfile. success Saved 597 new dependencies. info Direct dependencies ├─ @rails/actioncable@6.0.3 (中略) └─ yargs-parser@13.1.2 Done in 88.04s. Installing dev server for live reloading run yarn add --dev webpack-dev-server from "." yarn add v1.22.4 [1/4] Resolving packages... [2/4] Fetching packages... info fsevents@2.1.3: The platform "linux" is incompatible with this module. info "fsevents@2.1.3" is an optional dependency and failed compatibility check. Excluding it from installation. info fsevents@1.2.13: The platform "linux" is incompatible with this module. info "fsevents@1.2.13" is an optional dependency and failed compatibility check. Excluding it from installation. [3/4] Linking dependencies... warning "webpack-dev-server > webpack-dev-middleware@3.7.2" has unmet peer dependency "webpack@^4.0.0". warning " > webpack-dev-server@3.11.0" has unmet peer dependency "webpack@^4.0.0 || ^5.0.0". [4/4] Building fresh packages... success Saved lockfile. success Saved 101 new dependencies. info Direct dependencies └─ webpack-dev-server@3.11.0 info All dependencies ├─ @types/glob@7.1.3 (中略) └─ ws@6.2.1 Done in 25.41s. Webpacker successfully installed 🎉 🍰
うまく行ったみたいです。最後、絵文字出てる・・・
実行確認
作成したアプリケーションディレクトリに移動して、rails s
コマンドで rails サーバーを起動します。
$ cd MyFirstApp $ rails s => Booting Puma => Rails 6.0.3.2 application starting in development => Run `rails server --help` for more startup options Puma starting in single mode... * Version 4.3.5 (ruby 2.7.1-p83), 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
設定何もしてないのですべてデフォルトです。ブラウザーで http://127.0.0.1:3000
にアクセスすると、「You're on Rails!」画面が表示されます。