Techioz Blog

rbenv - Ruby - 複数のプロジェクト間で Ruby のバージョンを切り替える方法

概要

rbenvを使ってRubyをインストールしました。私はMacを使用しています。私のプロジェクトの 1 つでは、Ruby 2.6.3 を使用しています。私の他のプロジェクトには Ruby 2.7.4 が必要です。そこで両方のバージョンをインストールしました。

私の最初のプロジェクトでは、rbenv バージョンを実行すると、正しく表示されます。

   system
   * 2.6.3 (set by /Users/suganyas/academics/project1/.ruby-version)
    2.7.4

2 番目のプロジェクトでは、次のコマンドを使用して Ruby バージョン 2.7.4 を設定しました。

rbenv local 2.7.4

したがって、2 番目のプロジェクトから rbenv バージョンを実行すると、再び次のように表示されます。

  system
  2.6.3
  * 2.7.4 (set by /Users/suganyas/project2/ceep/.ruby-version)

しかし、バンドルインストールを行うと、次のエラーが発生します

  Your Ruby version is 2.6.3, but your Gemfile specified 2.7.4

完全なトレースはここにあります

    The git source `git://github.com/sqlninja/outdatedbrowser_rails.git` uses the `git` protocol, which transmits data without encryption. Disable this warning with `bundle config git.allow_insecure true`, or switch to the `https` protocol to keep your data secure.
   The git source `git://github.com/nhodges/phantomjs-gem.git` uses the `git` protocol, which transmits data without encryption. Disable this warning with `bundle config git.allow_insecure true`, or switch to the `https` protocol to keep your data secure.
    The git source `git://github.com/heapsource/active_model_otp.git` uses the `git` protocol, which transmits data without encryption. Disable this warning with `bundle config git.allow_insecure true`, or switch to the `https` protocol to keep your data secure.
  Following files may not be writable, so sudo is needed:
  /Library/Ruby/Gems/2.6.0
  /Library/Ruby/Gems/2.6.0/build_info
  /Library/Ruby/Gems/2.6.0/cache
  /Library/Ruby/Gems/2.6.0/doc
  /Library/Ruby/Gems/2.6.0/extensions
  /Library/Ruby/Gems/2.6.0/gems
  /Library/Ruby/Gems/2.6.0/specifications
Your Ruby version is 2.6.3, but your Gemfile specified 2.7.4

私が何を間違っているのか教えてください。

解決策

以下のコマンドを実行すると、正しい rbenv ローカル バージョンを選択することができました。

echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile
source ~/.bash_profile