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!
Related
Quick metaprogramming question. In my rails 5 project, I'd like to find the installed location of a certain gem in code (ie. not command line).
I started with Bundler.install_path.
Looking under that, I see all the versions of the various gems I have installed for this ruby (I use rbenv, but this should work for rvm too).
/home/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/mygem-7eafb06c791d/
/home/user/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/mygem-3da2bf38d01a/
...and so on
Is there a way to tell Bundler "Can you tell me the directory where the gem mygem is installed? Or if there was a hash of gem names with their directories (or something like that), that's fine too.
bonus: if there's a guide to the inner workings of bundler, that'd be cool too :)
Bundler has the show command to show the location of a gem:
bundle show mygem
rbenv-bundler is plugin that makes the rbenv shims Bundler-aware so you dont' have to type bundler exec in front of every command.
you can manage setting of the bundler with create a config file inside ~/.bundle/config (mac / linux)
BUNDLE_PATH: ~/vendor/some_path
if you need more information about config the bundler you can check this bundler.io link
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.
After uninstalling Rails 4(RC1) I still get Rails 4 apps generated with rails new.
➲ rails -v
Rails 4.0.0.rc1
➲ which rails
/Users/brandon/.rvm/gems/ruby-1.9.3-p392/bin/rails
➲ gem uninstall rails
Select gem to uninstall:
1. rails-3.2.13
2. rails-3.2.3
3. All versions
>
What's the cleanest way to fix this?
It is quite easy.
gem uninstall rails -v=4.0.0.rc1
gem uninstall railties
gem install rails -v 3.2.13
gem update --system
rails -v
By using commands above I was able to install older version of rails as needed :)
Rails does not come as an all-in-one package. You have a base Rails gem, plus it's many dependencies:
Action Mailer
Action Pack
Active Record
Active Resource
Active Support
Bundler
Railties <---- (contains generators)
Sprockets adapter for Rails
To get rid of your Rails 4 installation as a whole, you must remove all of these gems.
The easiest way to do this is to delete your entire gem folder, then reinstall whatever you need.
Try specifying your installed version:
gem uninstall rails -v=4.0.0.rc1
EDIT:
If you've already uninstalled (which you have), the following should work:
gem update --system
rails _3.2.2_ new app_name # or whatever version you're on
Fortunately, this worked as a simple fix for me:
Please note that in order to shift back to 3.2.13 (or whatever version you'd like to go back to), you must remove Railties as well as Rails.
Just do:
gem uninstall rails
Then, select the version of Rails 4 you have and delete it.
Then, do:
gem uninstall railties
And do the same thing.
When I uninstalled the Rails 4 version of railties, it told me that dependencies for a couple gems (coffee-rails and sass-rails) wouldn't be met. So I just did the same thing with both of them as I did above (such as, gem uninstall sass-rails), and deleted their Rails 4 versions as well. For example, for sass-rails, I had a version installed called sass-rails-4.0.0.rc1, so I uninstalled that version).
And that's it; the terminal will list 3.2.13 as your current Rails version, and new apps will be generated from this version as well.
TL;DR:
The simplest and safest solution to the immediate problem is
gem uninstall railties
Slightly Longer & More Complete Approach
If you want to uninstall everything that gem install rails installed, you can get a list of commands to run with this:
gem dependency rails --pipe | ruby -ne 'puts $_.gsub(/\([0-9\. <>=~,]*\)/,"")' | ruby -ne 'puts "gem uninstall #{$_}"'
Copy those and run them one-by-one, and for each one you'll be told what else depends on it, and asked if you want to go ahead with uninstallation. If you see anything in the list that is not part of rails (say you've installed something else that needs that version of active_record) then leave it, otherwise go ahead and uninstall.
The longer explanation
The version displayed is taken from the version of the railties gem, which is not uninstalled by uninstalling the rails gem.
If you open the rails executable with
vim `which rails`
(or the equivalent with the editor of your choice) you'll see the code at the bottom that decides which version of rails to use based on the version of railties:
#!/usr/bin/env ruby_noexec_wrapper
#
# This file was generated by RubyGems.
#
# The application 'railties' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
version = ">= 0"
if ARGV.first
str = ARGV.first
str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
if str =~ /\A_(.*)_\z/
version = $1
ARGV.shift
end
end
gem 'railties', version
load Gem.bin_path('railties', 'rails', version)
The simplest solution, therefore, is just to gem install railsties. There is no solution built-into RubyGems (that I can find) that will detect which other gems were installed with rails and are no longer used by anything else and uninstall them. RubyGems does not have the idea of an exclusive dependency, so even though nothing else besides rails uses railties, you're still stuck having to know that it (and several other things) are left over and must be manually uninstalled. This is not ideal, but it's what we've got right now, and it's not that bad, especially if you use the solution above to find and remove all the rails dependencies.
I am joining a Ruby project and I was given a GitHub url for the gemfile. I copied and pasted the gemfile to the root of my ruby setup.
What I wasn't sure about was what the extension of the gemfile should be. Should it be "name.rb" ?
And also what I am confused about is the command I should use. I googled around for the right syntax, but got confused what the syntax should be to use the bundler to create the application from the gemfile that I have.
Advice much appreciated!
The Gemfile should be saved without an extension.
Bundler has many different commands you can use, but it sounds like you are interested in how to get Bundler to install all of the project dependencies. From the Bundler documentation:
bundle install
Install the gems specified by the Gemfile or Gemfile.lock
Since a Gemfile is evaluated as Ruby code it is possible to save it with an extension of .rb. However, Bundler will not automatically find it if you do. If for some reason you do need to save it with a .rb extension this answer shows how it is possible.
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