2019年03月17日(土)
以下構成でHerokuへのデプロイまで行ってみました。
- Ruby 2.6.1
- Rails 5.1.6
rbenvでRubyインストール
以下が参考になりました。ありがとうございます。
http://kenzo0107.hatenablog.com/entry/2018/03/07/225128
- インストール可能なリスト
$ rbenv install --list
- インストール (2019/03/17時点で最新のver)
$ rbenv install 2.6.1
長いなー、これはトイレ行くか、コーヒーいれといてもよいかもしれん。10分くらい待った気がする
Downloading ruby-2.6.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.1.tar.bz2
Installing ruby-2.6.1...
Installed ruby-2.6.1 to /mnt/d/.rbenv/versions/2.6.1
- インストールされていることを確認 (2.5.1はすでにインストールしていた)
$ rbenv versions
* 2.5.1 (set by /mnt/d/src/ruby/.ruby-version)
2.6.1
- ディレクトリにセット
$ rbenv local 2.6.1
# 確認
$ rbenv version
2.6.1 (set by /mnt/d/src/ruby/rails-tutorial/.ruby-version)
$ ruby -v
ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-linux]
Railsインストール、ローカルで実行
- Gemfileにrails記述
$ bundler init
Writing new Gemfile to /mnt/d/src/ruby/rails-tutorial/Gemfile
- Gemfile
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem "rails","5.1.6"
- bundle installする
$ bundle install --path vendor/bundle
これまた長い、nokogiriのインストールに時間がかかっているみたい
- rails newする
$ mkdir env
$ cd env
$ rails _5.1.6_ new hello_app
bundleにinstallしたので、bundle execしないとrailsが見つからなかった。
$ bundle exec rails _5.1.6_ new hello_app --skip-bunde
nokogiriなどのinstallに時間がかかっている。なんかpassするオプションあったような気がしたので
--skip-bundle
ですでにinstallしているgemは再びinstallしないようにするはず
$ cd hello_app
$ bundle install --path vendor/bundle
不吉なエラーが、、
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
sqlite3のライブラリが足りないみたいなので、apt-getする
sqlite3.h is missing. Try 'brew install sqlite3',
'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
これでなんとかrails server立ち上げて
$ bundle exec rails s
Herokuにデプロイ
Gemfileにpostgresをセットして以下実行
$ bundle install --without production --path vendor/bundle
HerokuへデプロイのためにPostgresのGem設定を追加してbundle exec installすると、
sqlite3の時みたいなエラーが
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
--with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
これでOK
https://qiita.com/youcune/items/5b783f7fde45d0fd4b35
$ sudo apt-get install libpq-dev
- Herokuのセットアップ
$ heroku --version
heroku: command not found
$ source <(curl -sL https://cdn.learnenough.com/heroku_install)
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 25.8M 100 25.8M 0 0 6441k 0 0:00:04 0:00:04 --:--:-- 6441k
$ heroku --version
heroku/7.22.7 linux-x64 node-v11.10.1
$ heroku login
heroku: Press any key to open up the browser to login or q to exit:
Opening browser to https://cli-auth.heroku.com/auth/browser/713f3747-e5ab-43e0-a4ec-01c494b5e9ce
Logging in... done
Logged in as higekick@*******
$ heroku create
Creating app... done, ⬢ peaceful-spire-12411
https://peaceful-spire-12411.herokuapp.com/ | https://git.heroku.com/peaceful-spire-12411.git
- Herokuへプッシュ
$ git push -u heroku master
$ heroku logs --tail
2019-03-17T08:32:19.053986+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=peaceful-spire-12411.herokuapp.com request_id=e5e97945-7201-4746-99d5-7d68fc52caa0 fwd="180.27.67.36" dyno= connect= service= status=503 bytes= protocol=https
以下のような説明がTutorialの説明に出ていたけど関係ないかなと思いつつインストールしてみる
Note: Due to a temporary interaction bug between Heroku and Bundler, you may get an error message that says something like “You must use Bundler 2 or greater with this lockfile. (Bundler::LockfileError)”. The Bundler team is working on this, but in the meantime you can try this suggested workaround:
$ heroku buildpacks:set https://github.com/bundler/heroku-buildpack-bundler2
Buildpack set. Next release on peaceful-spire-12411 will use https://github.com/bundler/heroku-buildpack-bundler2.
Run git push heroku master to create a new release using this buildpack.
もう一回pushしてくれと言われるけど、何も変更してないのでpushできない
- 一旦アプリ殺す
$ heroku destroy peaceful-spire-12411 --confirm
もう一回上げなおすが、同じエラー
$ heroku create
$ git push heroku master
もう一回ログを見直す
$ heroku run rails console
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.6/lib/active_record/connection_adapters/connection_specification.rb:188:in `rescue in spec': Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). (Gem::LoadError)
pgがgemファイルに追加されていないとかいうエラーのようだ。
いや追加したよなと思ったけど、間違えてた。
productionに追加しなくては
-group :development do
+group :production do
gem 'pg', '0.20.0'
end
これでなんとかherokuへデプロイできた。
途中、Gemのインストールなどで時間待ちが発生する場合は、伊藤さんのチュートリアル解説動画を見ていると
その先の予習もできていいなと思いました。
第1章 Railsチュートリアル第3版・コーディング実況動画
https://www.youtube.com/watch?time_continue=1680&v=c4D2b5vZG2o