Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RVM: specify a ruby version to use

Tags:

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?

like image 276
Mellon Avatar asked Nov 18 '11 17:11

Mellon


People also ask

How do I use a specific version of ruby?

Selecting a version of Ruby You'll need to install bundler 1.2. x or above to use the ruby keyword and bundler 1.13. x or above to use a Ruby version specifier. You can use the ruby keyword in your app's Gemfile to specify a specific version of Ruby.

Does RVM use Ruby version?

Ruby Version Manager (RVM) is a utility that allows you to add your own personal version of Ruby to a user. It allows you to add, remove, or have multiple versions of Ruby and its libraries live in your user directory.

How do I change the default version of RVM?

Try running rvm --default 1.9. 2 instead. That works for me. Did you accidentally put the line to load rvm in your bashrc instead of your bash_profile ?


2 Answers

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):

  1. Create a file in bookstore/ directory named .rvmrc (in your favorite editor)
  2. 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)
  3. Change directory out of your bookstore directory, then change back into it.
  4. RVM should ask you if you want to trust this .rvmrc file (yes)
  5. 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.

like image 175
Brett Bender Avatar answered Sep 29 '22 05:09

Brett Bender


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.

like image 20
Dave Newton Avatar answered Sep 29 '22 07:09

Dave Newton