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

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

Related

RVM Ruby installation not successful

I'm trying to install ruby using rvm on linux debian. This is my problem:
Ruby didn't work at all in the terminal (that is, it said: ruby:command not found.
Then I followed this thread RVM ruby installation issue and it worked. However, after I closed that terminal and opened a new one, it didn't work anymore.
Do I have to change something in the autostarts? Can someone please help me!!
Check if your ~/.profile file contains:
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
and your .bash_profile file contains:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
if it have it and still don't work add this line to your ~/.bash_profile
source ~/.profile
I'm assuming you are using bash as your shell. You may want to check it in your user preferences (depend on your distro).
I assume the installation worked, what did not worked is you missed to use the installed ruby:
rvm use ruby-2.1.0
in your case it would print you a warning about not using login-shell - follow the instructions, they ae there to help you

rvm on OS 10.9 a few problems: second export path to .bash_profile, missing 2.0 path, and missing files

1)
I have this error every time I load a new bash terminal
-bash: /etc/profile.d/sm.sh: No such file or directory
-bash: /etc/profile.d/rvm.sh: No such file or directory
I saw this previously asked question with the answer to simply delete the references from .bashrc. But the answer is unaccepted and unvoted and the references to these files are not in .bashrc but in ~/etc/profile
I should note that I don't have a profile.d directory under /etc. I have files profile and profile~orig
2)
RVM added a second export path after I ran rvm get stable, but did not include a reference to Ruby 2.0.0 in either.
.bash_profile
export PATH=$PATH:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
export PATH=/usr/local/bin:/Users/mcb/.rvm/gems/ruby-1.9.3-p448/bin:/Users/mcb/.rvm/gems/ruby-1.9.3-p448#global/bin:/Users/mcb/.rvm/rubies/ruby-1.9.3-p448/bin:/Users/mcb/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
My instinct is to combine them into one, eliminate repeats, and add the references to ruby 2 manually. I also added the $PATH: myself, since that didn't work the first time either.
3)
This is clearly not right
my-macbook-pro:~ mcb$ rvm get sable --auto-dotfiles
Warning! PATH is not properly set up, '/Users/mcb/.rvm/gems/ruby-2.0.0-p247/bin' is not available,
usually this is caused by shell initialization files - check them for 'PATH=...' entries,
it might also help to re-add RVM to your dotfiles: 'rvm get stable --auto-dotfiles',
to fix temporarily in this shell session run: 'rvm use ruby-2.0.0-p247'.
cat: /Users/mcb/.rvm/help/get: No such file or directory
So, long story short I'm starting to get worried about trying to fix anything else myself without some sort of guidance for fear of just making things worse.
for 1) check /etc/profile for those two files references
for 2) remove export PATH=/usr/local/bin:/Users/mcb/.rvm/gems/ruby-1.9.3-p448/bin:/Users/mcb/.rvm/gems/ruby-1.9.3-p448#global/bin:/Users/mcb/.rvm/rubies/ruby-1.9.3-p448/bin:... from ~/.bash_profile
for 3) rvm get sable => rvm get stable
I think the problem is that you are adding ruby 1.9.3 explicitly in your PATH, and that's a job that rvm shoud do.
In my case I have no direct reference to any of the ruby installations in my PATH.
Try cleaning your path from anything ruby and rvm related, and let rvm load as a function.
This is the .bash_profile on my computer (with rvm and brew working):
export PATH=/usr/local/bin:/usr/local/sbin:$PATH:/usr/local/opt/ruby/bin
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
To check if rvm is correctly configured you can run:
type rvm | head -n 1
The result should be: "rvm is a function"
Checkout rvm official webpage for more details on how it works: https://rvm.io/rvm/install

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.

OSX Lion New bash session rvm default ruby not used

I use OSX Lion. I have installed RVM and have put this line in my .bash_profile file.
[[ -s "/Users/Anand/.rvm/scripts/rvm" ]] && source "/Users/Anand/.rvm/scripts/rvm" # This loads RVM into a shell session.
I installed ruby-1.9.2-p290 and set it as default rvm ruby with this command:
rvm use --default ruby-1.9.2-p290
And when I checked ruby-v
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.2.0]
The problem is, every time I open a new terminal window or a tab, the default ruby is not getting set. The system ruby is instead getting used. ruby -v gives this:
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin10.3.2]
This also happens with reading .rvmrc file in a ruby project. When I'm inside a project and when a new tab gets opened, it gets me into the project directory but is not setting ruby according to .rvmrc in that project. What should I do to fix this?
I got the problem. It was with $PATH. I have to put rvm related line in the .bash_profile as the last line.
Old .bash_profile
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
source $HOME/Dropbox/dotfiles/bashrc
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH
export PATH=/Library/PostgreSQL/9.0/bin:$PATH
New .bash_profile
source $HOME/Dropbox/dotfiles/bashrc
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH
export PATH=/Library/PostgreSQL/9.0/bin:$PATH
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
And it worked. New windows and new tabs open up correctly with the default ruby i.e, ruby 1.9.2
I just fixed this very same issue. Turns out it was a path issue.
try this
➜ ~ which ruby
if you are getting /usr/bin/ruby, the path to your original ruby installation is overshadowing the default install in your RVM installation.
I added the path to RVM before the path (see below) to /usr/bin/ruby and it works as expected.
I use zsh as my shell, so I had to add this manually to my .zshrc
export PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting<br />
export PATH=$PATH:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
if you use bash
Your .bashrc probably looks like this
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
change it to this:
PATH=$HOME/.rvm/bin:$PATH # Add RVM to PATH for scripting
and make sure you .bash_profile has this:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
and you should be good to go.
Of course this means that your shell is going to look in your RVM directory first when looking for any command, but thats never caused any issues for me.
Question #1:
The command as listed is, you had switched '--default' and 'use'.
rvm --default use ruby-1.9.2-p290
Check if default is actually set:
$ rvm list default
Default Ruby (for new shells)
ruby-1.9.2-p290 [ x86_64 ]
Some other troubleshooting tips in case that doesn't work. Are you sure your bash profile file is being loaded? Try just having 'source "/Users/Anand/.rvm/scripts/rvm' instead of the check, maybe something is wrong with the path? Username does that begin with capital letter?
Question #2: You must cd into the directory with the .rvmrc file explicitly for the magic to happen. I suppose starting a console in a certain location doesn't trigger rvm. I run into this when using pushd and popd to switch between folders.
Try this first if rvm --default use doesn't work:
sudo rvm alias create default 1.9.2

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