RVM: specify a ruby version to use - ruby-on-rails

I know how to use RVM, but now I have a weird problem, which I do not understand why.
Here is the simple story (I am using Ubuntu):
I have created a Rails project, the direcotry of this project is "bookstore/".
I go to project directory by cd bookstore , and type command rvm list like following:
bookstore/$ rvm list
rvm rubies
ruby-1.9.2-p136 [ i386 ]
ruby-1.8.7-p352 [ i386 ]
ruby-1.8.7-p330 [ i386 ]
ruby-1.8.6-p420 [ i386 ]
ruby-1.9.2-p290 [ i386 ]
Since I did not see the => arrow sign which is supposed to indicate the current ruby version in use, so I specify the ruby version with the following RVM command:
bookstore/$ rvm use ruby-1.9.2-p290
Using /home/usr/.rvm/gems/ruby-1.9.2-p290
Now, if I rvm list I see my project is using ruby v1.9.2 :
bookstore/$ rvm list
rvm rubies
ruby-1.9.2-p136 [ i386 ]
ruby-1.8.7-p352 [ i386 ]
ruby-1.8.7-p330 [ i386 ]
ruby-1.8.6-p420 [ i386 ]
=> ruby-1.9.2-p290 [ i386 ]
Every thing works fine at this point!
But, if now I open a new terminal window on Ubuntu, and cd to the project directory, and run the command rvm list again, I got:
bookstore/$ rvm list
rvm rubies
ruby-1.9.2-p136 [ i386 ]
ruby-1.8.7-p352 [ i386 ]
ruby-1.8.7-p330 [ i386 ]
ruby-1.8.6-p420 [ i386 ]
ruby-1.9.2-p290 [ i386 ]
Where is the => to indicate the ruby version I specified previously? Why it again needs me to specify the ruby version?
It happens always when I open a new terminal window. How to have my project "remember" the ruby version I have specified?

Dave is right, you should set a default. But also, look into defining an .rvmrc file on a per-project or per-machine basis. I use project-specific rvmrc files, so I can use different rubies and gemsets for each project, and changing into the directory automatically switches to that project's ruby/gemset.
For example, my rvmrc for company site project:
brett#bender:~/Sites/bearded/bearded(master)$ cat .rvmrc
rvm 1.9.3#bearded
Edit: For explicitness' sake, to solve your problem using an rvmrc file, do the following (assuming you already installed the ruby version you want and created a gemset for this project's gems):
Create a file in bookstore/ directory named .rvmrc (in your favorite editor)
Add rvm ruby-1.9.2-p290 to the file and save it (you can use rvm ruby-1.9.2-p290#gemset_name if you have a gemset you want to default to)
Change directory out of your bookstore directory, then change back into it.
RVM should ask you if you want to trust this .rvmrc file (yes)
RVM should have automatically switched your active ruby and gemset to the ones specified in your rvmrc file for that project.
Also note that if your RVM is older than version 1.8.0 you will need to turn on rvmrc file support (versions 1.8.0+ have it turned on by default). The link at the top of my question contains instructions if you're so inclined.

You need to set the default.
rvm --default 1.9.2-p290 # Or whichever.
A new shell is a new environment; it will not (normally) inherit from already-opened terminals
For per-project settings, use a .rvmrc file in the root of your project, for example:
rvm --create gemset use 1.9.2-p0#my_project
The --create will create the gemset if it does not already exist, handy if you or others work on the same project across machines.

Related

Where is ruby stored on mac?

I am looking for a path of where ruby gets stored on mac?
I installed macruby on my mac and I am trying to see where it got installed. I want to include the path of macruby in my ruby script. When I do rvm list, I get:
=> macruby-0.12 [ i686 ]
* ruby-1.9.3-p545 [ x86_64 ]
ruby-2.1.1 [ x86_64 ]
ruby-2.1.2 [ x86_64 ]
ruby-2.1.2-version [ x86_64 ]
I use #!/usr/local/bin/macruby in my ruby script, but looks like macruby is not present in that location. When I execute "whereis ruby" command on I get back "/usr/bin/ruby", but when I execute "whereis ruby-1.9.3-p545" or "whereis macruby-0.12" I get nothing.
How do I find the path of where does all these ruby versions lie?
You can get the path to the local executable with
$ which ruby
the rvm rubies are in your rvm directory, which I believe is ~/.rvm/rubies (I use rbenv so not sure about the details)
The which command should pickup whatever shims/other magic RVM is using to set your shell ruby
You can find your answer by running one of the following commands:
which ruby
whereis ruby
type -P ruby

"which ruby" different from rvm default

I recently installed Ruby 2.1.1 with RVM, and I set that to the default. In my root directory it correctly sets current to ruby-2.1.1:
$ rvm list
rvm rubies
ruby-1.9.3-p448 [ x86_64 ]
ruby-1.9.3-p545 [ x86_64 ]
=* ruby-2.1.1 [ x86_64 ]
# => - current
# =* - current && default
# * - default
$ which ruby
$ /Users/mynamehere/.rvm/rubies/ruby-2.1.1/bin/ruby
But when I move into my rails project, current is set back to 1.9.3, even though default is 2.1.1:
$ rvm list
rvm rubies
ruby-1.9.3-p448 [ x86_64 ]
=> ruby-1.9.3-p545 [ x86_64 ]
* ruby-2.1.1 [ x86_64 ]
# => - current
# =* - current && default
# * - default
$ which ruby
$ /Users/mynamehere/.rvm/rubies/ruby-1.9.3-p545/bin/ruby
Can someone tell me why the current ruby version changes depending on my directory, and how I can set 2.1.1 to be the default? I've tried doing "$ rvm --default use 2.1.1" in my project directory, but that doesn't fix the issue.
Thanks!
You probably have a .ruby-version file in your Rails project folder which establishes the default for that project. Check for that and, if you have it, you can remove it or change it's contents to the version you want to use for that particular Rails project.
It's discussed (rather tersely) in the rvm website under Typical RVM Project Workflow.
You can also define your gemset specific to the project as well. It's a cool feature if you have multiple projects that require different versions.

RVM doesn't seem to save installed gems

I have been using RVM for almost a year now without a problem, but suddenly it started to behave odd a day ago. First of all I wasn't able to enter the Rails console or server - it just told me that it couldn't be found anywhere. After running bundle install it fetched all Gems again as if none of them had been installed previously. This worked until I restarted my machine - after that I had to bundle again.
I am not aware of changing anything in the system that could trigger this kind of behaviour. Did anybody experience something like it before?
I am running the latest stable RVM release on Ubuntu 13.10
Try the following:
rvm list
You should see something like this:
ruby-2.0.0-p0 [ x86_64 ]
=* ruby-2.1.0-preview1 [ x86_64 ]
The =* indicates ruby-2.1.0-preview1 is my current and default ruby version.
Try switching to another ruby version, in my case:
rvm use ruby-2.0.0-p0
the rvm list command should now yield ( => means current, * default):
=> ruby-2.0.0-p0 [ x86_64 ]
* ruby-2.1.0-preview1 [ x86_64 ]
What's happening with you is probably that rvm is switching versions when you reboot. You might want to set the version you want to use as default, in my case:
rvm use ruby-2.0.0-p0 --default
rvm list
=* ruby-2.0.0-p0 [ x86_64 ]
ruby-2.1.0-preview1 [ x86_64 ]

RVM,Gemset, Bash on MacOSX - what have I done during my upgrade?

Every time I open a new terminal in Mac OSX Lion, rails isn't loaded. This is after I upgraded to ruby 1.9.3 and rails 3.2 - so I was doing a lot of configuring but not sure what I've screwed up. It used to work fine.
If I load a new terminal on startup, and type:
rvm
that works.
If I type
ruby -v
I get the correct (1.9.3)
but if I type rails -v, I get an error saying:
/Users/userishere/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in `to_specs': Could not find rails (>= 0) amongst [rake-0.9.2.2, rake-0.9.2] (Gem::LoadError)
from /Users/userishere/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:256:in `to_spec'
from /Users/userishere/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems.rb:1208:in `gem'
from /usr/bin/rails:18:in `<main>'
If I type:
rvm use 1.9.3p0#rails32 --default
That makes rails work as normal, but only in that terminal window -even a new tab in terminal, rails gives errors - how do I get it to accept that gemset/config as the global default whenever I start up? I have:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
in my .bash_profile.
What have I done to my config / missed in my upgrade? It seems like something simple, but I'm not spotting it.
I just switched my own default rvm by using the command rvm 1.9.2-p290#mybranch --default (not that I didn't have 'use' in the command)
Then, if you open a new terminal window or tab and type rvm list you should see a => next to the ruby version it's using. I see this:
ree-1.8.7-2010.02 [ x86_64 ]
ruby-1.8.7-p302 [ x86_64 ]
ruby-1.9.1-p378 [ x86_64 ]
ruby-1.9.2-p0 [ x86_64 ]
ruby-1.9.2-p136 [ x86_64 ]
ruby-1.9.2-p180 [ x86_64 ]
=> ruby-1.9.2-p290 [ x86_64 ]
I like to see which rvm version and gemset I'm using next to my command prompt. In your .bash_profile right before the line you listed that loads the RVM function, add this:
PS1="\$(~/.rvm/bin/rvm-prompt i v g s) $PS1"
Which prepends this to my prompt: ruby-1.9.2#myapp
rvm 1.9.3-p0#rails32 --default only set it working for the current terminal window, once I added another or exited and came back it was back to not working.
I fixed this by adding a .rvmrc file to my app directory. It now loads the gemset & ruby version correctly.
As per:
http://seanbehan.com/mac-os-x/installing-and-using-rvm-on-mac-os-x-creating-gemsets-and-reverting-to-original-environment/

Cannot install RVM . Permission denied in /usr/local/rvm

Based on my previous thread : RVM installed by Ruby not working? where i had installed RVM using the root user, I then had to entirely remove the RVM install and now i am installing as a user.
So i did :
Create a new user by doing : useradd newuser
Follow the instructions on the RVM website and execute the command : bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
Now, i get the error : mkdir: cannot create directory `/usr/local/rvm': Permission denied
The new user i created does not have access to this directory. I manually tried creating the folder but the same error. Please help.
EDIT : The original problem occured because i did not restart the terminal and it was still using the old settings.
Now, I got a new problem : After installing RVM, i cannot run it and it gives me an error : rvm command not found.
Here is the output of my ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session.
And here is output from ~/.bashrc file
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific aliases and functions
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session.
mkdir: cannot create directory `/usr/local/rvm': Permission denied
If you've run the rvm installer as root previously, remove /usr/local/rvm and /etc/rvmrc.
RVM is easy to install, but you are making it harder by trying to mix and match installation types. You do NOT need to create a new user. When run, RVM will create a directory in your home directory: ~/.rvm, and install everything inside it. That means you will have all the correct permissions. You do NOT need to be running as root, you do NOT need to use sudo. I'd recommend closing all your command-lines and open one fresh and start at your home directory. If you are running as root, log out, and log back in to your normal account. For a single-user install you do NOT need to be root.
For a single user, using RVM as their Ruby sandbox, use the single-user installation docs. Follow ALL the instructions on that page, INCLUDING the "Post Install" section.
Close your terminal window, and reopen it. If you have correctly followed the instructions above, typing rvm info should spit out a template of what is to come once you install a Ruby instance. If you see nothing output, or get an error, then retrace your steps in the "Post Install" section, and go through the "Troubleshooting" section. Most of the problems people have occur because they didn't bother to read the directions.
Once RVM is installed, type rvm notes and read what dependencies you need to install. If you do not add those files your Rubies installed will be missing functionality. They will work, but some of the creature comforts you'll hear about won't work and you will wonder why.
After installing the dependencies you should be in good shape to install Rubies. Type rvm list known for all the Rubies RVM can install. If you want 1.8.7 type rvm install 1.8.7, and, similarly, rvm install 1.9.2 for Ruby 1.9.2. If you want a particular revision you can add that, based on the ones in the list.
It's important to periodically update RVM using rvm get head. That will add features, fix bugs, and tell RVM about new versions of Ruby it can install if you request.
After installing a Ruby, type rvm list and it should show up in the list, looking something like this:
rvm rubies
ruby-1.8.7-p334 [ x86_64 ]
ruby-1.9.2-p180 [ x86_64 ]
Type rvm use 1.9.2 --default to set a default Ruby that will be sticky between logins. Use the version of whatever Ruby you want to default to if 1.9.2 doesn't float your boat. Once you've defined a default it should look something like:
rvm rubies
ruby-1.8.7-p334 [ x86_64 ]
=> ruby-1.9.2-p180 [ x86_64 ]
Before you begin installing gems into a RVM-managed Ruby, read "RVM and RubyGems ", in particular the part that says "DO NOT use sudo... ". I repeat. Do NOT use sudo to install any gems, in spite of what some blog or web page says. RVM's author knows better when it comes to working with RVM controlled Rubies. That is another mistake people use with RVM, again as a result of not reading the directions.
On Mac OS, you'll need the latest version of XCode for your OS. Do NOT use the XCode that came with Snow Leopard on the DVD. It is buggy. Download and install a new version from Apple's Developer site. It's a free download requiring a free registration. It's a big file, approximately 8GB, so you'll want to start it and walk away. Install XCode, and you should be ready to have RVM install Rubies.
Finally, RVM installs easily, as will the Rubies you ask it to install. I have it on about four or five different machines and VMs on Mac OS, Ubuntu and CentOS. It takes me about a minute to install it and another minute to configure it and start installing a new Ruby. It really is that easy.
I had the original issue reported in this question, "mkdir: cannot create directory `/usr/local/rvm': Permission denied" when trying to install rvm.
This is my scenario and how I solved it - maybe this will help others with this same issue.
I have Ubuntu 11.04 installed on a laptop, I only have 1 user, the one I created at install time, named nathan. When I would try to install rvm as nathan, the rvm installer saw me as root and kept trying to install rvm globally, but since I wasn't really root, it couldn't get access to create directories in /usr/local/rvm.
I'm far from an expert with Ubuntu, so I'm sure there are easier/better ways to accomplish the things I did (and I would love to learn about them), but this worked for me:
I created a new user called rubydev
I logged in as rubydev, opened a terminal and typed:
rubydev~$ bash < <(curl -B http://rvm.beginrescueend.com/install/rvm)
rvm installed correctly and I logged out of rubydev
Signed back in as nathan, opened a terminal and typed "su" (you could do all this with sudo, I am lazy)
After successfully getting root, I typed the following commands:
root: /home/nathan# cp -R /home/rubydev/.rvm .
root: /home/nathan# chown -R nathan .rvm
root: /home/nathan# chgrp -R nathan .rvm
root: /home/nathan# exit
nathan~$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile
nathan~$ echo 'export rvm_path="/home/nathan/.rvm"' > ~/.rvmrc
nathan~$ source .bash_profile
At this point, rvm was correctly installed under my home directory. To verify I typed:
nathan~$ type rvm | head -1
rvm is a function (if you don't get this response, something else is wrong)
Read the notes and installed any dependencies
nathan~$ rvm notes
I installed some rubies
nathan~$ rvm install 1.8.7-head
nathan~$ rvm install 1.9.2-head
Verified install
nathan~$ rvm list
rvm rubies
ruby-1.8.7-head [x86_64]
ruby-1.9.2-head [x86_64]
nathan~$ rvm use 1.9.2
using /home/nathan/.rvm/gems/ruby-1.9.2-head
nathan~$ rvm list
rvm rubies
ruby-1.8.7-head [x86_x64]
=> ruby-1.9.2-head [x86_x64]
Finally, I edited the preferences on the terminal itself to ensure the "Run command as as login shell" under the "Title and Command" tab was checked. It seems .bash_profile isn't otherwise processed.
I removed the rubydev user I created in step 1.
With all of that, I have a working rvm under Ubuntu 11.04 using my preferred username.
I solved this by adding
export rvm_path=~/.rvm
to ~/.bash_profile
If you first installed RVM as root and then uninstalled it. And now you are trying to install it as a non sudo user and you're getting the following error:
mkdir: cannot create directory `/usr/local/rvm': Permission denied
Make sure that you have logged out of the root session before trying to install under the user.
If you installing RVM as a user then the RVM folder should be generated in your home directory:
~/.rvm
Where there should be no permissions problems at all.
I would suggest it is picking up some old config that is left over from your system installation.
Ensure there is no /etc/rvmrc or $HOME/.rvmrc file left over because it might be using previously initialised variables from these files to construct an incorrect installation path.
if we look at this section of the bash script:
if [[ ${rvm_ignore_rvmrc:-0} -eq 0 ]]; then
for file in /etc/rvmrc "$HOME/.rvmrc " ; do
if [[ -s "$file" ]] ; then
source $file
fi
done
fi
It is trying to find one of these files, if it finds one if will run it possibly initialising rvm_path which will subsequently not be set as $HOME/.rvm by this command
rvm_path="${rvm_path:-"$HOME/.rvm"}"
I had the same issue. When I tried to create a gemset I would get a permission denied error. I just forgot to run the "rvm use 1.8.7" command first. After that I was able to create and use the gemset without any problems.
In lasts versions of rvm you need remove /etc/profile.d/ also.
This happen often if you try to install as root and then try again as a regular user.
Hope this help.
Look for file rvm.sh below /etc directory (It may be in /etc, or /etc/init.d).
Also, try some grep rvm /etc -r, so you can find some files/lines which prevent you from installing rvm in your $HOME dir.
For me running
__rvm_unload
Worked first, this was due to the fact that I had rvm installed as for multiuser.
If you installed rvm as root and you are getting permission denied issues (maybe you are deploying with capistrano as a non root user) then you could try rvm fix-permissions after doing things like rvm install 2.2.2 as root and creating a gemset as root.
Scott Bartell's solution worked for me. i am on a Digital Ocean premade image, where they had already setup rvm as root.
1. deleted /etc/rvmrc .
2. added export rvm_path=~/.rvm to ~/.bash_profile
3. logged out of ssh session to virtual machine
4. logged back in and presto!
would have commented but rep is 3 points too low :)

Resources