rbenv: Surviving without gemsets
Suppose you have rails 3.1.0 installed, but you want to create a new project using rails 3.2.13 which is not installed.
Let's say you want the new project to be in ~/projects/Project2
.
gem install rails -v 3.2.13
cd ~/projects
rails _3.2.13_ new Project2
This will create the Gemfile
for you, locked to the version of rails you specified on the command-line.
I deliberately omitted the idea of keeping a separate copy of gems for the new project, because that goes against the Bundler philosophy, which is to have all gems installed in one place. When you run rails, Bundler will pick the correct gem versions automatically from that central location. That means a project can share gems instead of installing a fresh copy for itself. (Note, however that each version of ruby you install will have its own gems. This is a good thing because native extensions likely won't work across ruby versions.)
You do have to be a bit more aware, because most commands, like rake
, will load the newest version of rake
that you have installed. You'll need to run bundle exec rake ...
to make sure the correct version is loaded. Usually I'll run bundle exec
for all commands except rails
. You can create an alias to make it shorter (I use bex
). To automate this with gem executables, you can use rbenv-binstubs, but you still have to be aware that running non-gem executables like ruby
and irb
won't automatically use the Gemfile.
Sidenote: rails new
will run bundle install
, which will check for the newest version of the dependencies. If you want bundler to try to use currently installed gems that satisfy the dependency requirements, you can skip the bundle install
with rails new --skip-bundle
, then run bundle check
in the app dir.
Sidenote 2: suppose you want to use a ruby version for Project2 (e.g. 2.1.8) that's different from the default (e.g. 2.3.0). In that case, running gem install
as specified above will install the gems under 2.3.0, which is a waste of time because you'll need to install the gems again under 2.1.8. To solve that problem, you can force the commands to use the preferred version via environment variable:
RBENV_VERSION=2.1.8 gem install rails -v 3.2.13
cd ~/projects
RBENV_VERSION=2.1.8 rails _3.2.13_ new Project2
echo 2.1.8 > Project2/.ruby-version
You could use rbenv shell
to set the variable, but I only recommend that if you don't want rbenv to auto-switch based on .ruby-version
files for the duration of that shell. It's very easy to forget that you have the variable set, and when you cd to a different project, it won't be using the version you expect.
How to check which gemset rbenv is using?
do a rbenv gemset active
If you've set up rbenv, and rbenv-gemset correctly, you will see the following output:
project-name global
rbenv: how may I create a private bundle for an application?
It's been a while since this was posted, but answering it in case others have the same question.
Brief answer: Use bundler or rbenv-gemset plugin
Sharing more context on the general best practice:
rbenv is used only to pick ruby versions by setting an environment
variable
Typically, it is used along with bundler and ruby-build. Bundler is an alternative to rvm's gemsets and manages gems. ruby-build plugin helps in installing different versions of rubies and automating it.
Related Topics
Undefined Instance Method "Respond_To" in Rails 5 API Controller
Using Helpers in Rails 3 to Output HTML
Error to Installing SASS with Gem in Windows 8.1
How to Avoid Putting the Magic Encoding Comment on Top of Every Utf-8 File in Ruby 1.9
Edit Each Line in a File in Ruby
Activerecord Select Except Columns
Why Are My Bigdecimal Objects Initialized with Unexpected Rounding Errors
Suppressing the Output of a Command Run Using 'System' Method While Running It in a Ruby Script
Selenium Rc: Run Tests in Multiple Browsers Automatically
Devise: How to Override Devise Error Messages on Password Change
How to Resolve Ruby Error (Missing Psych)
I Want to Override Authenticate_User and Current_User Method of Devise Gem
Rendering a Partial from a Controller in Rails
How to Fix Hanging Popen3 in Ruby
How to Post Xml to Restful Web Service Using Net::Http::Post
Factorygirl Association Model Trouble: "Systemstackerror: Stack Level Too Deep"