I have a lot of gems (in a Rails 2.3 app) in the vendor/ directory. These are frozen in using rake. I'd like to move to Bundlr.
What's the best way of migrating these gems?
Bundler will allow you to install specific versions of your gems: http://gembundler.com/gemfile.html
While I don't know of a way to auto-generate a Gemfile from your existing frozen gems, you can definitely do a quick audit of the versions of the gems you are using and then setup a Gemfile which will bundle in the exact same gems.
Here's info on setting up Rails 2.3 with Bundler: http://gembundler.com/rails23.html
Remember to remove all of your config.gem declarations from your environments. These should all be specified in the Gemfile now in the right format for bundler.
If you want your new gems to be stored in your repository for easy access (and faster install/deployment), check out bundle package: http://gembundler.com/bundle_package.html
Related
How can I initialize/create/generate a Gemfile for Bundler using the Gems I currently have installed?
For example, if I have the rails gem and the colorize gem already installed, and I start a new rails app, how can I generate a Gemfile that already includes the rails and colorize gems, ideally with their current versions, so I don't have to type them out manually?
Appending Installed Gems to Your Gemfile
You can dump all your currently-installed gems into a Gemfile-like format with a little Ruby text munging and shell redirection. For example:
ruby -ane 'puts "gem #{39.chr}#{$F.first}#{39.chr}"' < <(gem list) >> Gemfile
You could then manually edit the Gemfile and remove the gems you don't want, or organize them into Bundler groups as needed.
Removing gems you don't want in your bundle might take longer than just typing them into the Gemfile in the first place, but your mileage may vary. At least it's nice to know it can be done!
I am new to ruby. I am trying to use AuthLogic gem in my rails application. I had installed it and added
config.gem"authlogic"
in my environment.rb file and executed the command
rails generate nifty_scaffold user username:string email:stringpassword:string new
It thrown me a error "git://github.com/odorcicd/authlogic.git (at rails3) is not yet checked out. Run bundle install first." Oftenly even though i run bundle install. Why? Please any one help me.
If you are using Rails 4+ versions, you need not to include gems in environment.rb file. You should add the gems in Gemfile as follows,
gem 'authlogic'
Once you have added new gems in Gemfile in addition to the existing gems, you must run
bundle install
so that, your rails application can consider including the recently added gems (here, authlogic gem in our example) in your application. That is what the reason why you need to run bundle install more often (only when you add new gems in Gemfile)
And, please do not add gems in config/environment.rb file. That is Rails 2 way.
Using Rails:
If Bundler retrieves the proper gems (and dependencies) and locks them in the Gemfile.lock for a given project, isn't using a gemset for this same project overkill? I've been told that using gemsets is still a good practice because merely having 2 versions of the same gem in your current PATH can cause conflicts. Is this right, or do you only need one or the other: Bundler or RVM?
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 calling rake?
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 typing bundle exec. Note the bundler plugin for oh-my-zsh provides the same feature
Managing multiple Ruby versions
The ruby version manager rbenv provides similar features as well.
Yes, gemsets are overkill. Just use bundler.
RVM is still usefull for managing versions of Ruby itself - but don't use it for gemsets. Just use bundler for gem version management.
Regarding conflicts between gem versions, if you use bundle exec before each command you shouldn't have a problem - eg. bundle exec rake db:migrate or whatever.
I had Rails 2.3.5 installed, and wanted to upgrade to 2.3.10 as a stepping stone to Rails 3. I thought running gem install rails -v=2.3.10 would install 2.3.10 and keep 2.3.5 as well. But now when I do rails -v, it only lists Rails 2.3.10. How can I install different versions of Rails and keep the existing ones?
gem list rails should show you all installed versions of Rails. You can specify which one you want each project to use in the config/environment.rb file.
Alternately (or "additionally"), look in to RVM (particularly the "gemset" function) for maintaining separate gem sets for each project.
Updated May 2017 Instead of RVM gemsets, best practice for managing gems in Rails projects (including the Rails gem itself) is to use Bundler. Bundler's Gemfile will list all the gems your project uses, and allows you to "pin" versions, so by changing the version pin for Rails and running bundle you can update your project to the new version.
<sarcasm>Now that I've said that, though, Bundler is probably on the way out to be replaced by something else. </sarcasm>
You still have both versions, as the other answers have mentioned. However, you don't want to call rails newapp and then change the config/environment.rb file. This will cause problems for any files that have changed between versions. Instead, create a new 2.3.5 app this way:
rails _2.3.5_ newapp
And you'll run the exact version of rails you want, to create the file structure correctly. I don't know why this isn't documented better.
To answer your question, you can install many versions of the rails gem without conflict. However, each project is created using a specific version. To install a new version of the rails gem as follow;
Change the version 3.2.18 with any version you like (see link below for all available versions).
gem install rails --version=3.2.18
To install the latest version
gem install rails
To check all the rails version available, check out this link
Here is a link to all the version of rails
You might consider updating your gem software by this command prior to loading new gems.
gem update --system
As per #pjmorse, list the version installed with this command
gem list rails
Hope that helps
You can define the Rails version of an application in config/enviroment.rb.
You can vendor the version of rails you want into your vendor/rails folder. At the command line just run rake `rake rails:freeze:edge RELEASE=2.2.2'. You don't need any version of rails installed for this to work it will take the source and build it from the remote source in your vendor directory.
rake rails:freeze:edge RELEASE=2.2.1
rake rails:freeze:edge RELEASE=2.2.2
rake rails:freeze:edge RELEASE=2.2.3
rake rails:freeze:edge RELEASE=2.2.4
rake rails:freeze:edge RELEASE=2.2.5
rake rails:freeze:edge RELEASE=2.2.6
rake rails:freeze:edge RELEASE=2.2.7
rake rails:freeze:edge RELEASE=2.2.8
Our project is Rails 2.2.2, maybe it can't use Bundler? (or maybe for some other reasons, Bundler cannot be used)
Then in that case, what is the most preferred way of freezing the gems into the project source tree?
Some that I know of are:
rake gems:freeze
needs gemsonrails and it doesn't work with the current gem 1.3.7
rake gems:unpack
will not freeze the depended gems. have to add it one by one manually
script/plugin install
need to install the depended gems one by one as well
you can unpack the gems into your vendor directory. once they are on the server just run rake gems:unpack and that will build them like how plugins are built or at least put into the file structure.
I've done this not for dependency and upgrade issues but for shared hosts, hosts without gem support, and actually to modify gems that need a one liner tweek.
we use rake gems:unpack and rake gems:unpack:dependencies.