Ruby versions differ in Terminal & bash - ruby

In Terminal, ruby -v gives me:
ruby 1.8.7 (2011-12-28 patchlevel 357) [universal-darwin11.0]
But if I type /bin/bash then ruby -v I get:
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]
I suspect this is something to do with my PATH config(s). My $PATH variable is different in both the above environments. There are other issues e.g. rvm won't run unless I go into bash mode.
For info, my ~/.bashrc contains:
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session.

Looks like "login shell" is not enabled, you need to enable it in Terminal Emulator Preferences, sometimes it is needed to use /bin/bash --login.
There are also known issues with ZSH, but it seams to be unrelated.

Try which ruby from "terminal" and "/bin/bash". Your 1.9.3 is inside your ~/.rvm path.
Type rvm info. You should get a list of the settings for RVM.
In your ~/.bashrc OR ~/.bash_profile, you should have RVM's initialization code. If you don't you didn't install RVM completely and need to finish. Read all the instructions on the RVM installation page.

This was not due to a $PATH problem. What I've learned is that RVM cannot be run unless you change your default login shell to either Bash or ZSH. Just firing up Terminal in Mac won't work. You make the global change to using Bash like this:
chsh -s /bin/bash
(swap /bin/bash for whatever your bash path is, find out using which bash).
The RVM website does say that bash>=3.2.25 is a prerequisite, but doesn't say what that is or how to check whether you have it. It also advises you to run rvm requirements to check what you need - and you can't run this unless you change your shell (all quite confusing for somebody new to this).
Thanks to the replies above for getting me there in the end.
See also: Bad: modifier error when installing RVM

Related

RVM and Gemfile - not always loading correct ruby, only when 'cd .. & cd myproject'

TL;DR: Every time I open a new iterm2 tab, rvm goes back to default version, it doesn't use the Gemfile ruby version
My Gemfile has
source 'https://rubygems.org'
ruby '2.0.0'
and I use rvm 1.25.14.
RVM is smart and reads the ruby version in gemfile, except for this edge case
Doing
# NOTE: iterm2
$ cd myproject
$ ruby -v
> ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-darwin12.5.0]
but cmd+t, creating a new tab, staying in that directoy,
$ pwd
>../myproject
$ ruby -v
> ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin12.3.0]
ruby 1.9.3 is my default, which is fine. What am I missing in my bash (or .zshrc ) ? to make this work?
#FILE .zshrc
#...stuff
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
Using cmd+d, splitting the terminal vertically, has the same problem. rvm goes to default, ignoring Gemfile. Thanks for helping guys !
This should be shell- and terminal-agnostic (unfortunately I can't test it on iterm2 since I don't have any Mac machine):
Add cd ${PWD} to your .zshrc and this should force RVM to load current gemset.
Just add cd . in .zlogin after the RVM script.
Use RVM's Built-In Ability to Reload Configuration Files in the Current Directory
RVM leverages the cd command, so while there may be a specific solution for your situation the more general solution is to use direnv, dotenv, or similar to ensure that you're triggering RVM properly when changing directories.
In my personal experience, one of the two placed into your project's .envrc or similar will resolve many issues, and highly recommend direnv with or without its standard library's ruby layout or use commands. For example:
read in the current directory's .ruby-* or .rvmrc files
rvm use .
Reload RVM, which will re-read various dotfiles if the relevant ~/.rvmrc variables have been exported (see dotfile settings in next section).
rvm reload
The first option is best IMHO, and seems to "just work" on all my Bourne-compatible shells, but the other should work too.
Some Key ~/.rvmrc Dotfile Settings
With either of the solutions above, you may need some of the following items set in your global ~/.rvmrc file:
export rvm_gemset_create_on_use_flag=1
export rvm_install_on_use_flag=1
export rvm_project_rvmrc_default=1
Which ones you really need will somewhat depend on how you expect RVM to behave under any given set of circumstances. However, I've found that using RVM's ability to reload its settings (rather than relying on the cd hooks, and calling that functionality directly to be much more reliable. Your mileage may vary.
See Also
https://rvm.io/workflow/rvmrc
https://rvm.io/workflow/projects
I seems that for a local open terminal rvm don't load its scripts. Add the code to the end of .bashrc, it then should:
if [ -z "$MY_RUBY_HOME" ]; then
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
fi

"RVM is not a function" error when running in ruby script and irb

I get an error when I run %x{rvm use #myapp} in ruby and irb. The error is "RVM is not a function, selecting rubies with 'rvm use ...' will not work".
Here's what I've tried:
1. the "rvm use #myapp" command works in OSX command prompt (using OSX Mavericks)
2. made sure RVM is the latest version.
3. reloaded RVM check RVM is a function in the command prompt
4. (still fails in irb and ruby's %x{})
5. According to some SO posts, I changed OSX terminal preferences from login shell to /bin/bash and /bin/bash --login. Quit, opened new terminal windows but all efforts were in vain.
6. checked .bash_profile for [[-s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
Any ideas on how I can get %x{rvm use #myapp} to work in ruby and irb?
What happens here is that the shell you had started ruby or irb with had rvm defined both as a function and added to PATH the function takes precedence in shell and it all worked fine, but when you open ruby or irb it is a new process and it inherits only environment variables which includes PATH and does not inherit functions, additionally running %x{} from ruby creates another shell process which is neither a login or interactive shell, and they respectively would make shell load ~/.bash_profile and ~/.bashrc.
Depending on what do you want to do you have few options, to execute another ruby/gem you can use rvm ... do ... for from %x{} like this:
rvm #myapp do ruby -e '...'
OR:
rvm #myapp do gem install ...
OR:
rvm #myapp do bundle install
it allows single command to run in the context of given ruby
Try this trick:
%x{bash -c 'source "$HOME/.rvm/scripts/rvm"; rvm use #myapp'}
However, you really can use rvm as you've specified because even if you've set up the rvm you then lost your session because your terminal will be closed. Try to setup your environment with session gem, and control bash session with it.
require 'session'
#myapp = 'ruby-1.8.7-p374'
bash = Session::Bash.new
stdout, stderr = bash.execute 'source "$HOME/.rvm/scripts/rvm"'
stdout, stderr = bash.execute "rvm use #{#myapp}"
puts stdout
# => Using /home/malo/.rvm/gems/ruby-1.8.7-p374

How to permanently switch ruby -v 1.8.7 to 1.9.3

By using these commands
source ~/.rvm/scripts/rvm
rvm use 1.9.3 --default
The version in current session is 1.9.3 but when I close terminal and reopen ruby version comes back to 1.8.7.
Do I need to add something to the .bash_profile ?
Edit: I found the another way is when I reopen terminal everytime just type source .bash_profile. The version then is 1.9.3. Is there anyway to execute the .bash_profile permanently ?
Yes you need to add something to your bash profile. See here:
https://rvm.io/rvm/basics/
Quote:
The rvm installation documentation instructs you to put the following line at the very end of your bash profile:
# This loads RVM into a shell session
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
Create a file named .rvmrc with the text rvm use 1.9.3 --default.
We're treading into sysadmin waters here but one possible explanation might be because of how you're logging into your shell and your OS. See the discussion of what files are loaded by your shell here and what makes up a login vs. non-login shell here.

Switching rubies in shell script

When executing a bash shell script I am using ruby 1.9.3. Then, within the script, I want to switch to JRub (I'm using rvm). I tried switching to JRuby by doing rvm use jruby within the script, but this didn't work, it said:
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for a example.
./run.sh: line 10: jruby: command not found
When I do type rvm | head -n1 at the command prompt, I get: rvm is a function. So I'm not sure of the problem. I thought it might be because I installed JRuby using sudo (sudo rvm install jruby). So I ran the shell script again using sudo. Again I received the error.
How can I switch rubies from within a bash shell script with rvm?
Thanks
I ended up adding this to the line before:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
as explained in this thread (sorry - though I was aware of this thread before, I didn't quite grasp it):
RVM doesn't switch Rubies
/complete/path/to/rvm your shell seems to have an 'rvm' builtin command defined.

TextMate, rvm and TM_RUBY

In the TextMate RVM instructions the text it says to set TM_RUBY to /Users/wayne/.rvm/bin/textmate_ruby and in the image it shows it set to rvm-auto-ruby. I decided to set it to rvm-auto-ruby thinking that it would use RVM's default Ruby version.
When running Command R in the RSpec.bundle having TM_RUBY set to rvm-auto-ruby will result in a load error. When you set it to textmate_ruby it works.
The only problem here is that TextMate doesn't always use the default version of Ruby since it's hardcoded in that file.
/Users/jspooner/.rvm/bin/textmate_ruby:
#!/usr/bin/env bash
if [[ -s "/Users/jspooner/.rvm/environments/ruby-1.9.2-head" ]] ; then
source "/Users/jspooner/.rvm/environments/ruby-1.9.2-head"
exec ruby "$#"
else
echo "ERROR: Missing RVM environment file: '/Users/jspooner/.rvm/environments/ruby-1.9.2-head'" >&2
exit 1
fi
So two questions:
What should TM_RUBY=rvm-auto-ruby actually do?
Is there a way to have TextMate use the RVM default?
Setting TM_RUBY to your-path/rvm-auto-ruby
http://rvm.io/integration/textmate/
should load whatever ruby and gemset is indicated in the .rvmrc file located in the project and if none default to rvm default. I just got this working and it is very smooth. I did need to get the latest version of rvm
rvm get head
to make it work and restart Textmate. Hope that helps.
See your other, similar, question Rspec bundle is broken in TextMate and rvm.
To help others chasing this same issue, the solution seems to be at: RVM / Textmate doesnt recognize .rvmrc Options.
Basically you replace the ~/.rvm/bin/textmate_ruby soft link with a file. This is what I did:
cd ~/.rvm/bin
mv textmate_ruby old.textmate_ruby
Create a shell script called textmate_ruby in the same directory to replace the soft-link, using the following contents:
!/usr/bin/env sh
source ~/.rvm/scripts/rvm
cd .
exec ruby "$#"
chmod +x textmate_ruby
Before doing this change I'd see my system Ruby's version (1.8.7) displayed if I did CMD+R to run the following script in TextMate:
puts RUBY_VERSION
Evaluating the script using CMD+CNTRL+SHIFT+E gave me 1.9.2.
After switching to use that script both point to Ruby 1.9.2, so at least there's some consistency now. I don't see TextMate tracking my currently set RVM Ruby version yet; Instead it's using the default version set in RVM: rvm use 1.9.2 --default. This is still a step forward because I can control which Ruby TextMate uses by adjusting my --default.
If you decide you want to revert later, just rename, or delete, the script and reverse step 2 above.

Resources