やりたいこと
Ruby on Rails 第6版を進めていて環境構築周りで詰まることが多かったので、ログとしてまとめます。
※随時更新予定
Cloud9環境
初めてAWSを利用したこともあり、些細なことでハマりました。
リージョンの設定
ログインして最初の設定では、「米国東部(バージニア北部」に設定されているため、「アジアパシフィック(東京)」を選択します。
最初はこの設定に気付かず「なんだかキーボード打っても反応が遅いなぁ」と思っていました。
EC2の設定
Cloud9の環境を作成すると、EC2のインスタンスが自動で作成されます。
ですが、このEC2インスタンスの容量が10GBしかなく、チュートリアルを進めていると No space left on device
というエラーが発生します。
その場合は、AWS Cloud9 の容量不足を解決するを参照してEC2インスタンスのボリュームを増加させます。
別インスタンスでデプロイする
別リージョンでCloud9の環境を作成した際には、以下の流れでソースを取得して rails server
の実行まで行うことができます。
$ git clone https://github.com/<user_name>/hello_app.git // GitHubのソースコードをクローン
$ cd hello_app // Railsのフォルダへ移動
$ bundle install --without production // Production 環境以外の Bundle install を実行
$ npm install --global yarn // npm でグローバル環境に yarn コマンドをインストール
$ yarn install --check-files // yarn をインストール
$ rails server // 開発用サーバーを起動
rails server
を実行する際には yarn
のインストールが必須のためCloud9にインストールされている npm
経由でインストールをしています。
Herokuへのデプロイ
Stackの変更
Stackを切り替えるのは本来好ましくないため、Stackの変更は行いません。
heroku-20では、チュートリアルで利用している Ruby 2.6.3
が利用できないエラーが発生します。
heroku-18であれば、チュートリアルで利用しているバージョンのRubyが利用できるので、 heroku create
後に以下のコマンドを実行してStackを切り替えます。
Gemfileの編集
heroku create
で自動的に作成される環境はheroku-20
のStackが利用されており、以下のようなRuby 2.6.3
が利用できないエラーが発生します。
Rubyバージョンエラーログ
remote: ! The Ruby version you are trying to install does not exist on this stack.
remote: !
remote: ! You are trying to install ruby-2.6.3 on heroku-20.
remote: !
remote: ! Ruby ruby-2.6.3 is present on the following stacks:
remote: !
remote: ! - cedar-14
remote: ! - heroku-16
remote: ! - heroku-18
remote: !
remote: ! Heroku recommends you use the latest supported Ruby version listed here:
remote: ! https://devcenter.heroku.com/articles/ruby-support#supported-runtimes
remote: !
remote: ! For more information on syntax for declaring a Ruby version see:
remote: ! https://devcenter.heroku.com/articles/ruby-versions
remote: !
remote: ! Push rejected, failed to compile Ruby app.
こちらのエラーは、Gemfileにて明示的にRubyバージョンが指定されているため発生しています。
以下の手順で対応することで上記エラーへ対処可能です。
- Gemfileの
ruby '2.6.3'
となっている行を削除して上書き -
bundle install --without production
を実行 -
git commit
で変更点をGitHubにコミット -
git push heroku master
でHerokuへデプロイ
Heroku Appの紐づけ変更
チュートリアル用のプロジェクト内でheroku create
で作成したHeroku Appを削除したのち、git push heroku master
を行うと! No app specified.
というエラーが発生します。
これはPush先のAppが無いですよというエラーなので、デプロイ先のAppを以下のように明示的に指定すれば解決します。
$ heroku git:remote --app app_name
app_name
の箇所に自身のHeroku AppのApp名を入れ、コマンドを実行してください。
また、自身がいるProjectがどのHeroku Appに紐づいているかはheroku info
で確認することができます。
Heroku App 割り当て前
$ heroku info
▸ No app specified.
▸ USAGE: heroku info my-app
Heroku App 割り当て後
$ heroku info
=== app_name
app_nameの情報....
- 参考サイト
Railsのコマンド
rails consoleが起動しない
Yarn のエラーによりrails console
コマンドが起動しないエラーが発生しました。
$ rails console --sandbox
warning Integrity check: System parameters don't match
error Integrity check failed
error Found 1 errors.
========================================
Your Yarn packages are out of date!
Please run `yarn install --check-files` to update.
========================================
To disable this check, please change `check_yarn_integrity`
to `false` in your webpacker config file (config/webpacker.yml).
yarn check v1.22.10
info Visit https://yarnpkg.com/en/docs/cli/check for documentation about this command.
※エラーに記載されている通りyarn install --check-files
を実行しても、同一のエラーが発生します
node_module
のディレクトリ、yarn.lock
を削除しyarn install
する方法も実行しましたが、私の環境では動作せず。。。
最終的に以下の方法で起動するようになりました。
$ spring stop // springを止める
$ spring status // springは自動的に再起動するので、起動しているかをチェック