Which ruby version am I using? - ruby

I'm not sure what Ruby version is being used on my device (macOS) by default.
ruby -v outputs ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20]
brew list ruby --versions outputs ruby 3.0.2
rbenv versions outputs
system
* 2.7.4 (set by /Users/nlakritz/.rbenv/version)

If ruby -v shows 2.6.3, then you are using the ruby which ships with the system by default. You can confirm this in several ways. Running the shell command which ruby will show /usr/bin/ruby. Checking your PATH will show /usr/bin earlier than the location of the homebrew or rbenv installations.
If you wish to run one of the others, you can put it earlier on the PATH or invoke it explicitly by giving the fully qualified name such as /usr/local/opt/ruby/bin/ruby myscript.rb. Another alternative which avoids twiddling the PATH variable is to use a shebang line at the beginning of different scripts pointing explicitly to the version to use with that script.

It depends what you mean by 'default'. If you're running a file with ruby filename then the version you are using is 2.6 (because you're running the same Ruby as ruby --version).
If you're running a file directly (eg. ./filename.rb) then it depends on the shebang line in the file. For example, the file could start with the line #!/usr/bin/ruby, so to check which version of ruby that file is running on, you could call /usr/bin/ruby --version.
Hopefully that helps, but I'm not 100% sure I understood what you're asking - so let me know :)

Try this:
$ which ruby
/usr/bin/ruby
$ irb
irb(main):001:0> RUBY_VERSION
=> "2.7.0"
So, rbenv is ignored, I think you're missing this in your path:
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
ruby -v # should say 2.7 now
And if it says 2.7, you can add the export and eval commands at the bottom of your ~/.bashrc to load it every time you open a shell, thus kicking off rbenv override.
I would bet 2.6 for you, ie ruby -v.

Related

Your Ruby version is 3.1.2, but your Gemfile specified 2.6.8

I'm getting this error message when I try to do a pod install on Mac on a Turbo Module in React Native. I believe it's because of my version of Xcode installed.
> RCT_NEW_ARCH_ENABLED=1 bundle exec pod install
Your Ruby version is 3.1.2, but your Gemfile specified 2.6.8
When I run ruby -v it returns 2.6.8.
> ruby -v
ruby 2.6.8p205 (2021-07-07 revision 67951) [universal.arm64e-darwin21]
I'm not sure what to do to fix this or where it's finding 3.1.2.
2.6.8 is the builtin ruby in macOS Monterey, which can be found at /usr/bin/ruby. If bundle is finding 3.1.2 then you have another version of ruby installed (possibly via homebrew?). You can see all versions of ruby on your current PATH using the command which -a. The order of results is the order in which they are found on the PATH, as you can confirm by typing echo $PATH. Execution is on a first-come-first-served basis. On my machine the which command produces:
% which -a ruby
/opt/homebrew/opt/ruby/bin/ruby
/usr/bin/ruby
showing that I have a homebrew copy of ruby first on my PATH, followed by the macOS copy.
Similarly, you can check which installation of bundle is being used:
% which -a bundle
/opt/homebrew/opt/ruby/bin/bundle
/opt/homebrew/lib/ruby/gems/3.1.0/bin/bundle
/usr/bin/bundle
You can override the PATH ordering by editing .zshrc (long-term change); by explicitly setting it to a new set of path values in the current terminal (one-time change for the active terminal window); or by typing the fully qualified path name of the version of the command you wish to run (ex: /usr/bin/bundle ... to use the system version).

How can I figure out the installed ruby version?

I need to test for the correct Ruby version before running the script. In project we have file .ruby_version that has number of required version. For example .ruby_version file has:
2.5.5
I could get current user version of Ruby in user using ruby --version, but this command return more information that i needed. For example:
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-darwin19]
So my check doesn't work:
required_ruby=$(cat .ruby-version)
current_ruby=`ruby --version`
if [ "$required_ruby" != "$current_ruby" ]; then
echo "This project uses Ruby version $required_ruby, try to checkout or install it"
fi
Any idea how to fix it?
There are two special constants RUBY_VERSION and RUBY_PATCH_LEVEL available in the Ruby interpreter, so you can call the ruby command and let it give your desired information.
$ /usr/bin/ruby -e 'puts RUBY_VERSION'
2.5.5

irb interpreter error: -bash: /usr/local/bin/irb: ##HOMEBREW_CELLAR##/ruby/2.1.1_1/bin/ruby: bad interpreter: No such file or directory

I'm Mac OS X 10.9.4 user and have system ruby version 2.0.0. Started learning ruby i've decided to get new version. I've done it using rvm get stable. It works almost fine except interactive mode.
When i call irb in terminal, i receive:
-bash: /usr/local/bin/irb: ##HOMEBREW_CELLAR##/ruby/2.1.1_1/bin/ruby: bad interpreter: No such file or directory`
ruby -v prompts ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
How can i fix it?
Answer was quite easy. Here was almost the same problem.
Solution:
Open /usr/local/bin/irb and configure ruby interpreter path. Mine was ##HOMEBREW_CELLAR##/ruby/2.1.1_1/bin/ruby and i only had to change ##HOMEBREW_CELLAR## to /usr/local/Cellar/
I suppose that another possible solution would be defining ##HOMEBREW_CELLAR## var, but i'm new to ruby and hence not sure.

does rbenv set the PATH variable to point to the correct ruby?

I installed rbenv and it seems and copied 2 versions of ruby to ~/.rbenv/versions, it shows
them both correctly.
When I run
rbenv global 1.8.7-p72
It says ok and also points to it. However when I run--
ruby --version
I get ruby not found. Basically, the ruby executable is not found in the path. Can anyone help me with this problem?
My .bash_profile is
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
rbenv versions outputs --
1.8.7-p72
*1.9.3-p448 ( set by /home/user/.rbenv/version)
Also my ~/.rbenv/shims directory doesn't have a ruby executable. It has erb, gem,irb , rake, rdoc , ri and testrb. Could this be the issue?
I'm sorry I'm completely clueless
Did you execute all the step in the installation instructions https://github.com/sstephenson/rbenv? I.e. did you add the required snippets to your shell startup (.bash_profile) and so forth?
If you followed the instructions, could you provide the output of rbenv versions and which -a ruby for a start?
EDIT:
rbenv rehash seems to be what generates the shims. This must be executed every time you install a new ruby version. How did you install the rubies? With ruby-build, as recommended in the installation instructions?
When I encountered this error, all I had to do was to make sure that $PATH had shims prior to system version of ruby like so:
~/.rbenv/shims:/usr/local/bin:/usr/bin:/bin

How to use Bundler with path specified Ruby version

I am on a VM (Lucid 64b) with a system Ruby version of 1.9.3p0.
I have a Ruby script that creates a .deb file -- The script needs to use Ruby 1.8.7 which I have installed in /foo/ruby/1.8.7.
There is an existing Gemfile to be used with Bundler
I can't use RVM and I can't install gems at the system level.
My .bashrc includes (and has been sourced)
export PATH=$PATH:/foo/ruby/1.8.7/bin
but ruby -v still gives me
ruby 1.9.3p0 (2011-10-30) [x86_64-linux]
Questions
How can I change the Ruby version for my user to use Ruby 1.8.7?
I've run: bundle install --path vendor/bundle
So in that directory (actually ./vendor/bundle/ruby/1.8/cache/gems) are all the gems I need but, when I run the Ruby script it doesn't find the required gems. I run the script like so /foo/ruby/1.8.7 script_to_gen_deb_file.rb
How can I get ruby to see/use the bundled gems?
Update
I was able to solve it. I needed to use
/foo/ruby1.8.7/bundle exec /foo/ruby1.8.7/ruby script_to_gen_deb_file.rb
I had tried this before, but I got an unrelated error and believed there was an environment problem.
Change your path so the special ruby gets precedence?
export PATH=/foo/ruby/1.8.7/bin:$PATH

Resources