I have two Sinatra apps, each using a different set of gems, and some of them have conflicting versions, e.g. app A uses Nokogiri 1.6.3 and app B uses Nokogiri 1.5.5.
How do I manage those two environments on the same OSX machine and be able to switch between them without conflicts?
Can I use only Bundler for that, or will I also need RVM? How exactly?
Bundler Handles Multiple Gem Versions on System
Bundler can handle this use case just fine. Your explicit gem dependencies go into your Gemfile, and all gem versions within the bundle are then stored in the Gemfile.lock file.
As a general rule, your gems will be installed in GEM_HOME. Multiple versions of a gem can be installed at the same time, and require bundler/setup takes care of ensuring that the correct versions (as specified in your Gemfile.lock) are made available to your Ruby application.
However, unless you use binstubs, gemsets, or special invocations like <gem> _<version>_ [args], you will need to run bundle exec <gem> from the command line in order to use the correct version as specified by your bundle.
The best way to manage multiple applications on a system is to use a manager like RMV (Ruby Version Manager).
RVM has the concept of a gemset, which you can use to create different sets of gems for different apps. It is the solution of your problem. You will need to create different gemsets for both applications. Like this, your gem versions will not conflict.
Related
I am new to Ruby, Rails and gem management. One thing what I have noticed is that whenever I run bundle install (even within the project directory) it seems to install gems which affects other gem based Rails projects too.
For instance, assume that after running bundle install within one gem based Rail project (which installs a bunch of gems from the gemfile) I run into gem incompatibility issues. Now, these issues will manifest in all the other gem based projects too and will present themselves everytime I attempt to rackup
My questions are:
1) Is there a way to localize this damage (gem version incompatibility) to the current project and not have other projects affected?
2) Is there a good way to obtain a compatible set of gems or is the only way to look at the gem dependencies on a gem-by-gem basis (look at the tree) and figure out the compatible ones? I seem to be wasting a lot of time on this and if I fix one something else seems to break.
Please let me know what I'm missing here or point me to resources.
Thanks
It sounds like you're missing use of bundle exec
Description
This command executes the command, making all gems specified in the Gemfile(5) available to require in Ruby programs.
Essentially, if you would normally have run something like rspec spec/my_spec.rb, and you want to use the gems specified in the Gemfile(5) and installed via bundle install(1), you should run bundle exec rspec spec/my_spec.rb.
bundle install will install gems to $GEM_HOME which is shared by any apps using the same ruby and can result in multiple versions of the same gem being installed (this is expected and normal). If you don't then also use bundle exec to load only the gem versions specified in your Gemfile you can get incompatibility errors or unexpected behavior as ruby doesn't know which version of a particular gem to require.
RVM, rbenv, and other ruby version managers are useful tools for both isolating gems per project (which isn't really necessary when also using bundler) and for allowing multiple ruby versions to be installed on the same machine (which is not handled by bundler and can be very useful when developing multiple apps with different ruby version requirements).
Given we use a preferred flavour of a ruby version manager (RVM or Rbenv) the ruby gems are isolated per ruby version.
Let's say we want to work on multiple applications locally (they are completely separate applications) and we want to use the same ruby version and the same rails version how do we properly isolate them and their gems? Are gemsets the (only) answer here? I mean if I have 5 applications with the same ruby version and I keep adding gems on all 5 fronts it's just a matter of time when one of the applications is ok with the latest stable gem version while one of the apps will still need to roll on an older version of the same gem due to legacy dependency or whatnot. How do you guys avoid this timebomb?
What if we want to use the same ruby version on multiple apps but a different Rails version? Rails being 'just a gem' is the answer same as for the above?
Thanks.
I'd recommend to use:
rbenv to handle multiple ruby versions on the same machine
bundler to define the dependencies of each application
Make sure to push .ruby_version, Gemfile and Gemfile.lock to make sure every is using the appropriate version of each gem...
I know that if application uses bundler, I can easily find all the gems installed by looking at the Gemfile.
Say, I am looking at the Rails 3 application that doesn't use bundler, how do I know what gems it uses?
Thanks
If it's not using Bundler, I don't know of a definitive way to identify every gem being used. You could search the entire app tree for require statements to start with, but that's not going to show most of them. Gems also require other gems internally, and will install their own dependencies, but those gems won't be referenced directly from your app's require statements.
If the app works and the tests pass (meaning you've at least got all the required gems installed), you could approach the problem by creating a Gemfile, listing the gems you know are needed, and then running your tests (or the app itself) via bundle exec, which will ensure that only the gems listed in the Gemfile are visible. Then you'll get failures related to missing gems, and can add them to the Gemfile until it all works. Once it's working via bundle exec, you'll know that you've captured all the requirements there.
If you're using RVM, you'll probably find it helpful to create a gemset for your app, along with a .rvmrc file in the app root, to take advantage of RVM's automatic gemset switching and Bundler integration. It'll make it easier to maintain the gem state going forward.
In any case, running gem list with the app in a working state will show you all the gems that it might be using, but without being scoped to a gemset or wrapped in bundle exec, you'll also see gems that were installed for other reasons that potentially have nothing to do with your app's dependencies.
OS X 10.6 has Rails 2.2.2 installed by default.
How to upgrade to Rails 2.3.8?
Since Rails 3 is out, is it possible to have multiple versions of Rails (like 2.3.8 and 3) installed on OS X?
(For example, I could be working on two Rails projects, one is Rails 2.x and the other is Rails 3.x).
Use rvm
RVM is a command line tool which allows us to easily install, manage and work with multiple ruby environments from interpreters to sets of gems...
RVM allows users to deploy each project with its own completely self-contained and dedicated environment--from the specific version of ruby all the way down to the precise set of required gems to run the application. Having a precise set of gems also avoids the issue of version conflicts between projects, causing difficult-to-trace errors and hours of hair loss. With RVM, NO OTHER GEMS than those required are installed. This makes working with multiple complex applications where each has a long list of gem dependencies efficient. RVM allows us to easily test gem upgrades by switching to a new clean set of gems to test with while leaving our original set intact. It is flexible enough to even have a set of gems per environment or development branch--or even individual developer's taste...
I wouldn't bother with gemsets myself just do as other people have mentioned:
gem install -v=2.3.8 rails
and then to use:
rails _2.3.8_ new rails app
Note that this _versionnumber_ thing is a standard way of specifying a version of any rubygems installed executable.
Use
gem install -v=2.3.8 rails
where -v=[version number you want to install]
Then
list gem rails
In your config/environment.rb you can state which version of Rails you want to use.
To upgrade your OS version of the rails gem:
sudo gem install rails --version 2.3.8
While you're at it, you can also upgrade your OS rubygems system itself, overwriting the binary that OS X comes with. This might seem kind of hacky, but it's a stable thing that everyone does.
sudo gem install rubygems-update
sudo update_rubygems
Regarding managing rails versions in your projects, if your only needs are:
Specifying the version of rails that you want on a rails project
Specifying different versions of rails and/or other gems in each of multiple rails projects
Then rvm is overkill. The only thing you need, and what is also a complete and utter pleasure to work with, is Bundler. Bundler is like Software Update for your rails project. In fact, Rails 3 comes with Bundler by default, you just have to list the gems you need in Gemfile and it takes care of the rest. You don't need to worry about which versions of your gems are installed in the OS.
(You do however need the appropriate version of the rails gem installed in the OS in order to generate the rails project in the first place)
http://rubygems.org/
It's all you need =)
Just a bit of background, I come from a strong C#/staticly typed background. Therefore I tend to think in terms of .dlls. So if I was working in a project, I'd reference my required dlls and that would be that.
Being new to Ruby and Rails I find I might be doing something wrong. For example, I create a Rails app at home using the gems I have locally. Using a different computer (say a work computer) I attempt to work on the project only to find I'm using different versions of the gems. After carrying out a bundle install I'm back to a working project.
The issue I have with this is that my gem library becomes 'messy'. I end up with several versions of the same gem. Is this the way others work? When using a gem (from a require) will it default to the latest version? I feel as if I'm not managing the dependencies correctly, though as I've mentioned I'm new to the world of Ruby.
Should I just include my dependencies, then perform a bundle install each time I have different/missing dependencies? What happens if I wish to upgrade to a newer version of a gem? Would it be a case of updating the gemfile that bundle uses and getting on with it?
Yes, bundler is the way to go to work with dependancies with Ruby on Rails. Why ? First, because it's shipped with it (at least for version 3.0). Second, because it's simple as hell (unlike maven with Java).
A non exhaustive list of feature :
it lets you declare one or many gem repository to fetch gems from
to group your dependencies by environment (development, production...)
to specify version you'd like
and so on. For more on this, check this http://gembundler.com/rationale.html
Regarding your question : yes, bundler will take the latest version available is none is specified.
Also, I would add a disavantage : you cannot specify a gem version depending on the OS. For example, nokogiri has a linux version AND a win version.
The default behaviour when requiring a module in a gem is to assume you want the latest version of the gem if you have more than one installed. You can change this by specifying which version you want in a specific application like this:
gem "rails", "2.3.8"
Before you require anything from the gem. This ensures that this application will use the specified version of the gem, even if a newer one is installed.
You can of course clean out obsolete versions of your installed gems whenever you don't need them again, or if you use bundler consistently: just wipe everything and run bundler again to get just your required gems installed.
Another useful tool is the Ruby Version Manager (RVM), in addition to handling different versions of ruby it provides a feature called gemsets which allows you to isolate different applications or environments from eachother. That is App A can have its separate gemset with all its required gems, and App B have another gemset with only its required gems. This will reduce the clutter in your dependencies quite a bit.