11
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rbenv でRuby のバージョン管理

Last updated at Posted at 2019-04-21

状況・やりたいこと

とあるアプリをHerokuにアップしようとしたところ上手く行かなかった。

「rubyのバージョン(ruby-2.3.7)がサポート外」という記載があったのでrubyのバージョンを修正してみることに。

対象アプリはJunaraさんの農産物直売所検索アプリだったので、Rubyのバージョンを記事に提示されていた**2.6.1に「変更する」**ことを目標とした。

現状の把握

まずは、現状のバージョン確認

$ ruby --version
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]

rbenvで2.6.1に切り替える

バージョン切り替え前半

rbenvでインストール可能なrubyのバージョンを確認

$ rbenv install -l

→2.6.1が見当たらなかったので、brewアプデ。

$ brew update
$ brew upgrade rbenv ruby-build

改めてリスト表示。

$ rbenv install -l
  (省略)
  2.6.0-rc1
  2.6.0-rc2
  2.6.0
  2.6.1
  2.6.2
  2.7.0-dev
  (省略)

→出てきた!!!
rbenvでインストールしてみる。

$ rbenv install 2.6.1

$ ruby —version
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]

→インストールしてみたが、バージョン2.6.1と出てこない。。(?)

$ rbenv versions
rbenv: version `ruby-2.3.7' is not installed (set by /Users/sayjoy/Desktop/free/tr_fuchu-nosanbutsu/.ruby-version)
  system
  2.4.1
  2.5.1
  2.6.1

→versions確認してみると、どこにも*が入っていない。なぜ?

ローカル環境に?2.6.1を適用?してみる。

$ rbenv local 2.6.1

ここで改めてversions。

$ rbenv versions
  system
  2.4.1
  2.5.1
* 2.6.1 (set by /Users/sayjoy/Desktop/free/tr_fuchu-nosanbutsu/.ruby-version)

きた!っぽい。

rails s 起動確認

ここでサーバー起動してみるが、下記エラー。

$ rails s
Could not find activerecord-import-1.0.1 in any of the sources
Run `bundle install` to install missing gems.`

とりあえず下記コマンドを実行。

$ bundle install

再度rails sしてみるも、今度はmigrationができてない感じのエラー。
次はマイグレーションの実行。

$ bundle exec rake db:migrate

加えて、csvデータのインポートまで実行して、一旦起動は成功。

ただし、ruby —versionの結果は変わらず。
この時点で2.6.1には変更できていない状況。

バージョン切り替え後半

バージョン切り替えたのに上手く反映されない。。
rbenvでバージョンがうまく切り替わらなかった時にやったことを参考にした。
原因は、環境変数(PATH)の設定ミスだということがわかった。「PATHを通す」というやつ。。
色々調べながら作業した内容は以下の通り。

Ruby がどのパスから呼ばれているか確認。

$ which ruby
/usr/bin/ruby

→状況としては、rbenv で入れたRuby じゃなくてシステムのデフォルトRuby が使われてる状態らしい。
rbenv のRuby が使われている場合、下記出力になるとのこと。
/Users/ユーザー名/.rbenv/shims/ruby
※パスが.rbenv配下のrubyを向いていればOK

PATHの確認。

$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

PATHの編集。

$ open ~/.bash_profile

このコマンドを実行すると.bash_profile ファイルがでテキストエディットで開き、編集が可能となる。

下記2つの記述があればOKらしく、①が不足していたので追記した。
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

.bash_profileに追記した追記した内容を反映させる。

$ source ~/.bash_profile

改めて、PATHの確認。(echo $PATHで確認しても同じ)

$ printenv PATH
/Users/sayjoy/.rbenv/shims:/Users/sayjoy/.rbenv/bin:/Users/sayjoy/.nodebrew/current/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

Rubyがどこから呼ばれているか、バージョンについて改めて確認。

$ which ruby
/Users/sayjoy/.rbenv/shims/ruby

→いい感じ!

$ ruby --version
ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin18]

→いい感じ!

バージョン切り替えに成功!!!

Herokuへのアップリトライ(余談)

とりあえずこの記事で設定した目標「バージョンの切り替え」は達成。
しかし、改めてgit push heroku masterしてみても前回同様のエラーが出てしまったので、引き続き頑張る。

$ git push heroku master
Counting objects: 7020, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (5716/5716), done.
Writing objects: 100% (7020/7020), 25.98 MiB | 946.00 KiB/s, done.
Total 7020 (delta 865), reused 7013 (delta 863)
remote: Compressing source files... done.
remote: Building source:
remote:
remote:  !     Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used.
remote: 			Detected buildpacks: Ruby,Node.js
remote: 			See https://devcenter.heroku.com/articles/buildpacks#buildpack-detect-order
remote: -----> Ruby app detected
remote: -----> Compiling Ruby/Rails
remote:        Command: 'set -o pipefail; curl -L --fail --retry 5 --retry-delay 1 --connect-timeout 3 --max-time 30 https://s3-external-1.amazonaws.com/heroku-buildpack-ruby/heroku-18/ruby-2.3.7.tgz -s -o - | tar zxf - ' failed on attempt 1 of 3.
remote:        Command: 'set -o pipefail; curl -L --fail --retry 5 --retry-delay 1 --connect-timeout 3 --max-time 30 https://s3-external-1.amazonaws.com/heroku-buildpack-ruby/heroku-18/ruby-2.3.7.tgz -s -o - | tar zxf - ' failed on attempt 2 of 3.
remote:
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:  !
remote:  !     Push rejected, failed to compile Ruby app.
remote:
remote:  !     Push failed
remote: Verifying deploy...
remote:
remote: !	Push rejected to tr-fuchu-nosanbutsu.
remote:
To https://git.heroku.com/tr-fuchu-nosanbutsu.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/tr-fuchu-nosanbutsu.git'

HerokuにデプロイでRubyバージョンサポート外エラーを参考に、bundle install をやってみた。

$ bundle install
Your Ruby version is 2.6.1, but your Gemfile specified 2.3.7

→GemfileのRubyバージョンが2.3.7 のままだったので、2.6.1 に修正し、改めてbundle install。
その後改めてHerokuにアップ。

$ git push heroku master
(省略)
remote:  !
remote:  !     Failed to install gems via Bundler.
remote:  !     Detected sqlite3 gem which is not supported on Heroku:
remote:  !     https://devcenter.heroku.com/articles/sqlite3
remote:  !
remote:  !     Push rejected, failed to compile Ruby app.
remote:
remote:  !     Push failed
remote: Verifying deploy...
remote:
remote: !	Push rejected to tr-fuchu-nosanbutsu.
remote:
To https://git.heroku.com/tr-fuchu-nosanbutsu.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/tr-fuchu-nosanbutsu.git'

→恐らく、sqlite3 はHeroku でサポートされていないという意味かと。
改めてJunraさんの記事を確認すると、DBに関する記述を見落としていたことに気づき、Gemfileにpgに関する記述を追加。

Gemfile.....
group :development, :test do
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]    #この記述は元々あった。
  gem 'sqlite3', '~> 1.3.10'
end

group :production do
  gem 'pg'
end

その後、bundle install したが下記エラーが発生。

$ bundle install
(省略)
An error occurred while installing pg (1.1.4), and Bundler cannot continue.
Make sure that `gem install pg -v '1.1.4' --source 'https://rubygems.org/'` succeeds before bundling.

どうやら、PostgreSQLのインストールが必要とのことなので下記コマンドを実行し、併せてbundle install。

$ brew install postgresql
$ bundle install

上手くいったので、git にpush して、Herokuにアップ。

$ git push heroku master

→OK!!

$ heroku open

その後、上記コマンドでアプリを開いてみたが、**「We're sorry, but something went wrong.」**というエラー画面が表示された。。
アプリ起動はできなかったが、Heroku へのアップ(push)が成功したので、今日はここまで。

宿題事項

  • 「shims」ってなんだ?
  • 「bundle install」とは。Rubyのバージョンを上げたら必要だった。必要な理由がいまいちピンときていないので要整理。。
  • 「We're sorry, but something went wrong.」ってなんじゃい?
11
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?