How to manage multiple gemsets and ruby versions with RVM?
Here is how I like to do it...
- Install a ruby with RVM
- Switch to/use that ruby
- Create a gemset for a project
- Switch to/use that gemset
- Install gems needed
- create an alias that points to my chosen ruby & gemset
- switch to/use that new alias (again, associated w/ a project)
Do this as many times necessary for your different projects that you want to keep separate from eachother.
Example:
$ rvm install ruby-1.9.2
...
$ rvm list
rvm rubies
=> ree-1.8.7-head [ i386 ]
ruby-1.9.2-head [ i386 ]
ruby-1.9.2-preview3 [ i386 ]
$ rvm use ruby-1.9.2-preview3
info: Using ruby 1.9.2 preview3
$ rvm gemset create my_project
info: Gemset 'my_project' created.
rvm gemset use my_project
info: Now using gemset 'my_project'
$ gem install httparty
When you HTTParty, you must party hard!
Successfully installed crack-0.1.8
Successfully installed httparty-0.6.1
2 gems installed
$ rvm alias create my_project ruby-1.9.2-preview3@my_project
info: Creating alias my_project for ruby-1.9.2-preview3@my_project.
info: Recording alias my_project for ruby-1.9.2-preview3@my_project.
$ rvm use my_project
info: Using ruby 1.9.2 preview3 with gemset my_project
$ ....
Now I have an entire environment dedicated to a particular project. This is great because I can experiment with all sorts of different gems/versions without worrying about stomping all over other projects that have very specific requirements.
Good luck!
Use multiple versions of rubygems with rvm?
The correct way is to created different named (-n) installs of the ruby you want installed and name them according to the rubygem version you want such as
rvm --install use 1.9.2-nrg186 && rvm rubygems 1.8.6 && gem --list
rvm --install use 1.9.2-nrg1810 && rvm rubygems 1.8.10 && gem --list
The reason for this is that you can only have 1 version of rubygems active an any given time. This is also due to the fact that each ruby defines a dependency on a specific rubygems version that version is known or expected to work with (regardless of if it can work with another or not).
This is the expected way to handle the multiple rubygems requirement and to eliminate potential problems. See https://gist.github.com/1273035 for specifics detailing this.
Manage gemset for ruby project with rvm and bundler
My .ruby-version looks like this
ruby-2.0.0
According to the rvm docs, it should look like this:
2.0.0
The rvm scripts that employ those .
files are triggered by the cd
command--not the bundle install
command. So try cd'ing out of your project directory, then back into it. Then check the things you checked before.
I just created a new rails project:
~/rails_projects$ rails new proj1
Then I created the .ruby-version
and .ruby-gemset
files in the project directory:
~/rails_projects$ cd proj1
~/rails_projects/proj1$ echo 1.9.3-p551 > .ruby-version
~/rails_projects/proj1$ echo proj1gemset > .ruby-gemset
After cd'ing out of the project directory, then back in, the rvm scripts that ran produced some output:
~/rails_projects/proj1$ cd
~$ cd rails_projects/proj1/
ruby-1.9.3-p551 - #gemset created /Users/7stud/.rvm/gems/ruby-1.9.3-p551@proj1gemset
ruby-1.9.3-p551 - #generating proj1gemset wrappers..........
The next time you cd into the project directory, you won't see that output.
Subsequently, everything was setup correctly:
~/rails_projects/proj1$ ruby -v
ruby 1.9.3p551 (2014-11-13 revision 48407) [x86_64-darwin14.5.0]
~/rails_projects/proj1$ rvm gemset list
gemsets for ruby-1.9.3-p551 (found in /Users/7stud/.rvm/gems/ruby-1.9.3-p551)
(default)
global
=> proj1gemset
~/rails_projects/proj1$ echo $GEM_HOME
/Users/7stud/.rvm/gems/ruby-1.9.3-p551@proj1gemset
~/rails_projects/proj1$ ls ~/.rvm/gems
cache
ruby-1.9.3-p551@proj1gemset
...
...
On the other hand, if you don't cd
out of the project directory after creating the rvm .
files, and instead you run bundle install
, the gemset won't be created.
How to use RVM and different versions of rails
I also install rails version 4.0.0. But I don't know how to use different versions of rails. when i create new project it shows rails version 3.x. I want to upgrade it to version 4. How to check list of all installed rails and how to use latest one. Need Help. Thank you.
this is because you're still using the current gemset rails3tutorial2ndEd
You need to create a different gemset:
rvm gemset create <new_gemset_name>
then use it:
rvm gemset use <new_gemset_name>
and finally install a new rails version:
gem install rails -v <version_number>
only after doing these things will you be able to make a new project with a different rails version.
rvm gemsets with Bundler
It's redundant to use RVM's gemsets if you're using bundler.
Conflicts when using Bundler arise primarily for two reason:
- Using gems that require other gems without precise version specifications.
- Executable conflicts: you have both rails v3 and v4 installed, so where do we go to when calling
rails g migration
or callingrake
?
The first issue can be resolved if you're careful about specifying your gem versions more explicitly in your Gemfile.
When working within a project with a Gemfile, the second issue can be resolved by prefixing executable calls with bundle exec
, which will run the command within the context of the current bundle (e.g. bundle exec rake db:migrate
).
When you want to specify a gem version outside of a Gemfile's context (e.g. rails new fancy_app
), you can specify any gem's version by providing it as the first argument surrounded by underscores.
rake --version
rake _10.3.1_ --version
rails new rails_latest_app
rails _3.2.12_ new rails_3_app
rails _4.0.4_ new rails_4_app
RubyGems handles all of this for you by creating version-aware wrappers for any gem's executables. Just run cat `which gem_executable`
(with gem_executable
being something like rake
, rails
, foreman
, pry
, etc.) and have a look.
Stephen Ball has a good blog post about how to use Bundler instead of RVM gemsets, which explores the overlaps in further detail.
While RVM's gemsets are not necessary, RVM provides other conveniences:
- Automatically adding bundler binstubs to the
PATH
, so you can avoid typingbundle exec
. Note the bundler plugin foroh-my-zsh
provides the same feature - Managing multiple Ruby versions
The ruby version manager rbenv
provides similar features as well.
RVM: How to use gems from a different ruby?
You need to check out gemsets and export your current gems.
rvm gemset export
Read the gemset docs for more information.
RVM and automatically switching gemsets
Create a .rvmrc
file in each project/branch with contents similar to this:
rvm gemset use xxxx
Save it. Next time you cd
into that folder, you'll probably get a security prompt from RVM to make sure you want to use that .rvmrc
. After accepting, you'll see "Now using gemset 'xxxx'" every time you navigate into that folder.
Related Topics
Check Whether a String Contains One of Multiple Substrings
Imagemagick 7 with Rmagick 2.16 on MACos Sierra Can't Find Magickwand.H
Generate Unique Random String with Letters and Numbers in Lower Case
Accessing Config from Application.Rb in Controller (Rails 3)
What's the Opposite of Chr() in Ruby
How to Set Ruby's Load Path Externally
Engine's Assets with Rails 3.1
Using Htaccess Password Protection on Rails
Protecting the Content of Public/ in a Rails App
Capybara: Select an Option by Value Not Text
How to Drop to the Irb Prompt from a Running Script
How to Customize Gemfile Per Developer
Installed Rails But the Rails Command Says It's Not Installed