Sumak
Sumak

Reputation: 1061

Find where a specific Ruby version is installed

On Mac, I used to have rbenv as Ruby version manager but recently switched to asdf. I want to create a Ruby on Rails 7.0.4 but it uses an outdated version and I'd like to locate it.

$ rails new app
    "Rails 7 requires Ruby 2.7.0 or newer.

    You're running
      ruby 2.6.8p205 (2021-07-07 revision 67951) [universal.arm64e-darwin"


$ which ruby           
/Users/kawsay/.asdf/shims/ruby

$ ruby -v
ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c5) [arm64-darwin21]

$ irb
irb(main):001:0> RUBY_VERSION
=> "3.0.5"

$ asdf list ruby        
  2.7.4
 *3.0.5

$ brew info ruby      
==> ruby: stable 3.2.0 (bottled), HEAD [keg-only]

$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 3.2.33
  - RUBY VERSION: 3.0.5 (2022-11-24 patchlevel 211) [arm64-darwin21]
  - INSTALLATION DIRECTORY: /Users/kawsay/.gem
  - USER INSTALLATION DIRECTORY: /Users/kawsay/.gem/ruby/3.0.0
  - RUBY EXECUTABLE: /Users/kawsay/.asdf/installs/ruby/3.0.5/bin/ruby
  - GIT EXECUTABLE: /usr/bin/git
  - EXECUTABLE DIRECTORY: /Users/kawsay/.gem/bin
  - SPEC CACHE DIRECTORY: /Users/kawsay/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /Users/kawsay/.asdf/installs/ruby/3.0.5/etc
  - RUBYGEMS PLATFORMS:
     - ruby
     - arm64-darwin-21
  - GEM PATHS:
     - /Users/kawsay/.gem
     - /Users/kawsay/.gem/ruby/3.0.0
     - /Users/kawsay/.asdf/installs/ruby/3.0.5/lib/ruby/gems/3.0.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /Users/kawsay/.asdf/installs/ruby/3.0.5/bin
     - /Users/kawsay/.asdf/shims
     - /opt/homebrew/opt/asdf/libexec/bin
     - /Users/kawsay/.cargo/bin
     - /opt/homebrew/opt/[email protected]/bin
     - /opt/homebrew/opt/[email protected]/bin/python3
     - /opt/homebrew/bin
     - /opt/homebrew/sbin
     - /usr/local/bin
     - /usr/bin
     - /bin
     - /usr/sbin
     - /sbin

$ which rbenv
rbenv not found

$ which rvm
rbenv not found

How can I find where ruby 2.6.8p205 is installed ?

Upvotes: 1

Views: 1589

Answers (1)

anothermh
anothermh

Reputation: 10564

You have an issue where you have system Ruby, brew Ruby, asdf Ruby, and rbenv Ruby. In short, your system is confused about where to find things.

The first step is to remove brew's Ruby installation:

brew uninstall ruby

The second step is to remove rbenv completely. (or use the official docs)

The third step is to make sure that system Ruby does not have the Rails gem installed. Start a shell without loading your profile so that you can bypass asdf and any other configuration:

env -i bash --norc --noprofile

Then confirm it's pointing at system Ruby: (your version will differ slightly depending on your version of macOS)

$ ruby -v
ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.arm64e-darwin22]

Then uninstall the Rails gem:

gem uninstall rails

But while you're at it you might want to remove all the gems you installed using system Ruby: (ignore any errors here)

gem uninstall -aIx

Now close that shell session and switch back to your default shell. Make sure that your shell profile is cleared of anything related to rbenv and that you have no GEM_* environment variables being set. (assuming you use ZSH, check every ~/.zsh* file and ~/.gemrc) Then start a new shell session to load the cleaned profiles.

Then make sure asdf has a properly groomed environment:

asdf reshim

Now when you run gem env you should see output like this:

RubyGems Environment:
  - RUBYGEMS VERSION: 3.4.1
  - RUBY VERSION: 3.2.0 (2022-12-25 patchlevel 0) [arm64-darwin22]
  - INSTALLATION DIRECTORY: /Users/foo/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0
  - USER INSTALLATION DIRECTORY: /Users/foo/.gem/ruby/3.2.0
  - RUBY EXECUTABLE: /Users/foo/.asdf/installs/ruby/3.2.0/bin/ruby
  - GIT EXECUTABLE: /opt/homebrew/bin/git
  - EXECUTABLE DIRECTORY: /Users/foo/.asdf/installs/ruby/3.2.0/bin
  - SPEC CACHE DIRECTORY: /Users/foo/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /Users/foo/.asdf/installs/ruby/3.2.0/etc
  - RUBYGEMS PLATFORMS:
     - ruby
     - arm64-darwin-22
  - GEM PATHS:
     - /Users/foo/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0
     - /Users/foo/.gem/ruby/3.2.0

Note the difference between INSTALLATION DIRECTORY and EXECUTABLE DIRECTORY in this output and yours. You need to have output that looks like this, otherwise that means that asdf is not installed properly and you should restart the installation of asdf from the beginning.

Afterwards, re-run gem install rails and you should be able to complete rails new app successfully.

If not, there is a shortcut to run commands with asdf for any given command:

asdf exec gem install rails
asdf exec rails new app
asdf exec rails server

This should bypass everything else and use asdf exclusively.

Upvotes: 2

Related Questions