今日はProgateでつくったRubyをHerokuで動かすにリトライです。
Heroku公式のRubyの使い方をみて、そのとおりにやってみたのですが、エラー連発で四苦八苦しております。
$ mkdir hello-world
$ cd hello-world/
$ bundle init
出力されたgemfileを編集
$ bundle install --path vender/bundle
$ subl config.ru
中身はこれ。`run proc { [ 200,{},["Hello World!"]}`
$ git init
$ git add config.ru Gemfile Gemfile.lock
$ heroku create
$ git push heroku master
ここでエラー。原因は・・・
-HerokuのRubyは2.4.5以上。
-ローカルのRubyは2.3.7p456。アップデートしてるのに、なぜか出来ない。
ということで、次回はローカルのRubyのUpdateにチャレンジです。
簡単そうにみえて大変だー!
以下、雑多な作業ログです。
Ruby アプリをクラウドにデプロイ・運用・スケール | Heroku
https://jp.heroku.com/ruby
①bundle init でRubyプロジェクトを初期化
$ mkdir rubydays
$ cd rubydays
$ bundle init
-bash: bundle: command not found
こりゃなんだ?
rbenvとbundlerというRubyのプラグインらしい。
なぜここで必要かが分からないけどいれてみる。
bundlerのインストールの参考にさせていただきました!
https://qiita.com/Alex_mht_code/items/d2db2eba17830e36a5f1
$ brew install rbenv ruby-build
Updating Homebrew...
<中略>
$ rbenv --version
rbenv 1.1.1
$ gem install bundler
Fetching: bundler-2.0.1.gem (100%)
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory.
$ sudo gem install bundler
Password:
<中略>
$ rbenv rehash ##
$ bundler -v
Bundler version 2.0.1
$ bundler init
Writing new Gemfile to /Documents/GitHub/rubydays/Gemfile
よーしできたぞ!
②RubyのバージョンとRackの依存関係を特定
$ subl gemfile
-bash: subl: command not found
こりゃなんだ2
Rubyのエディタらしい。いれておこう。
sublについて、こちらを参考にさせていただきました。
https://qiita.com/ashdik/items/aeb50f67e43b910204cb
そしてSubline Textをインストールしようって書いてる。インストールしてみました。
https://www.sublimetext.com/
シンボリックリンクをはる。これ、単なるエディタでしたってこと???
sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl
$ subl gemfile
ジェムファイルが開いた!
Rubyとrackを指定してっと。
# frozen_string_literal: true
source "https://rubygems.org"
ruby "2.2.3"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
# gem "rails"
gem "rack"
$ bundle install --path vender/bundle
The Gemfile specifies no dependencies
Resolving dependencies...
Bundle complete! 0 Gemfile dependencies, 1 gem now installed.
Bundled gems are installed into `./vender/bundle`
③「Hello World」でリクエストに応答
$subl config.ru
run proc { [ 200,{},["Hello World!"]}
$ git init
Reinitialized existing Git repository in /Documents/GitHub/hello-world/.git/
これでいいのかな???
Githubを見に行ったけどファイルはなし。うーん。
$ git add config.ru Gemfile Gemfile.lock
Gemfile.lockってなんだろう。いま手元にはないし。
調べてみたら、なんだか大事そうなファイルです。
もういちど bundle install
$ bundle install --path vender/bundle
Your Ruby version is 2.3.7, but your Gemfile specified 2.2.3
Gemfileのバージョンが違うって?だめもとで書き換えてみる。
$ bundle install --path vender/bundle
Fetching gem metadata from https://rubygems.org/..............
Resolving dependencies...
Using bundler 2.0.1
Fetching rack 2.0.6
Installing rack 2.0.6
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Bundled gems are installed into `./vender/bundle`
$
$ ls
Gemfile Gemfile.lock config.ru vender
Gemfile.lockができた!
$ git add config.ru Gemfile Gemfile.lock
$ git commit -m “init”
[master (root-commit) 3a043e6] “init”
3 files changed, 24 insertions(+)
create mode 100644 Gemfile
create mode 100644 Gemfile.lock
create mode 100644 config.ru
これでいいのかな?
④heroku createを使ってアプリをプロビジョン
$ heroku create
Creating app... done, ⬢ pure-garden-42094
https://pure-garden-42094.herokuapp.com/ | https://git.heroku.com/pure-garden-42094.git
$ git push heroku master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 586 bytes | 293.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Ruby app detected
remote:
remote: !
remote: ! You must use Bundler 2 or greater with this lockfile.
remote: !
<中略>
To https://git.heroku.com/pure-garden-42094.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/pure-garden-42094.git'
$
エラーメッセージで検索。2019年からのバージョンエラーって!
こちらを参考にさせていただきました。
https://qiita.com/yoshijbbsk1121/items/87250501b32c6433943e
やってみる。
$ rm Gemfile.lock
$ gem install bundler -v 1.17.3
$ bundler _1.17.3_ install
$ git push heroku master
Enumerating objects: 5, done.
<中略>
remote: !
remote: ! An error occurred while installing ruby-2.3.7
remote: !
remote: ! This version of Ruby is not available on Heroku-18. The minimum supported version
remote: ! of Ruby on the Heroku-18 stack can found at:
remote: !
remote: ! https://devcenter.heroku.com/articles/ruby-support#supported-runtimes
remote: !
だめかい!
同じくHeroku-18でひっかかっていた記事。
Ruby2.4.5以降が必要らしい。
https://qiita.com/newburu/items/bb31ef2b429b5557b45a
$ rbenv install 2.4.5
バージョンアップしてgit push
$ git push heroku master
Enumerating objects: 5, done.
<中略>
remote: ! An error occurred while installing ruby-2.3.7
そういえば、Gemfileに2.3.7って書いたっけ。2.4.5に修正してみよう。・・・しかし同じくエラー。
もう一回。
$ rbenv install 2.4.5
<中略>
$ rbenv local 2.4.5
$ ruby --version
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]
$
うーん、なんでだ!?
どうやったらローカルのRubyをアップデートできるんだろう。
続きはまたこんど。
(所要時間 3時間半)