I read "Relationships between Rubygems, Bundler, and RVM" before asking it again.
Well, there are many questions like this, but people who answered say they work with Rubygems, RVM and Bundler, and they have not explained how each of these work in isolation.
I am really confused with how the three work in isolation when we are installing gems. Please do not tell me how you work, which will help me, but I won't learn what is happening when we play with them.
My confusion can be broken down into these questions. Where is a gem installed when:
I just have Rubygems (without RVM or Bundler)?
Rubygems and RVM are installed?
Rubygems, RVM and Bundler are installed?
Please help me understand this stuff with either resources on the web or by your detailed answers.
To find out where gems are being installed to, run echo $GEM_HOME in a terminal.
When using RVM, gems are installed into your RVM install as it changes $GEM_HOME. Running echo $GEM_HOME now would show a path into your RVM install.
When Bundler is added to the mix, gems will either be installed in $GEM_HOME, or, if you specify a path when running bundle install will be installed to that path. To find out where a gem is through Bundler you can use bundle show gemname to get its full path.
Use gem env to list the gem paths in each context.
Without RVM gem env will report the system gem library paths.
With RVM gem env will report the RVM-managed gem library paths.
Bundler manages application dependencies and installs into the gem library in your environment. If you are using RVM + Bundler, the gems will be installed in the RVM managed gem directories. If you are using it without RVM, bundler will install gems in the system gem directories.
To find the path where a gem is installed use:
gem which gem_name
To find executables (like html2haml) use:
which executable_name
To avoid typing bundle exec html2haml which is recommended by the Bundler team, use my rubygems-bundler gem.
I'm also trying to understand how it works. The blog "Advice on using Ruby, RVM, Passenger, Rails, Bundler, … in development" helped me have a better overview.
BTW, it's a translation of a French article, the French version is better.
Related
As the question states - where does the gem install?
Is it installing within the app directory that I'm working in (i.e. user/sites/sample_app)? Or is it being installed on my computer? If the latter where exactly?
Thanks!
gem install process
first download gem and save desktop
1.next step open command prompt and set location that means c:/desktop> gem install --local "gemname"
2.next step com to rails consoler and type $bundle install --local.
3. type the gem name on gem list
I have two questions:
Where do you install your ruby?
Did you use RVM or rbenv?
Now I will explain your question using my situation as an example.
I use RVM to manage rubies on my mac os.
now the ruby install in path
/Users/pin/.rvm/rubies/ruby-2.1.1
and these will be a gems directory under .rvm path. In this directory,
/Users/pin/.rvm/gems
there are many gems group, I have a group named
ruby-2.1.1#global
which is used by the default ruby version.
This is a directory and there will be a gems directory under it.
/Users/pin/.rvm/gems/ruby-2.1.0/gems
In this directory, you will find all of the gems you installed using cmd
bundle install
If you don't use ruby version management tools like rvm or rbenv, you may find the gems
around your ruby path. If you still can't find them, you can post the details of how you
install the rubies and other system configs, so that we can discuss here.
If you are using rvm then its get installed in
/home/user/.rvm/gems/ruby-version#global/ or /home/user/.rvm/gems/ruby-version/
If you are using specific gemset for gems then
/home/user/.rvm/gems/ruby-version#gemset_name/
If you want to know where gem is installed use gem which *gem_name* e.g.:
gem which rails
If you installed your gems with bundle install use bundle show *gem name* e.g.:
bundle show rails
Gems
If you use gem install x, you're adding the gem to the local ruby version on your system. This is a system-wide installation, and will be stored in your rubylib/ruby/gems dir:
The install command downloads and installs the gem and any necessary
dependencies then builds documentation for the installed gems.
Bundler
Using the bundle install command (when you have a Gemfile & use bundler), you basically tell bundler to install the gems relative to your specific applicaiton:
Bundler makes sure that Ruby can find all of the gems in the Gemfile
(and all of their dependencies). If your app is a Rails 3 app, your
default application already has the code necessary to invoke bundler.
If it is a Rails 2.3 app, please see Setting up Bundler in Rails 2.3.
For example, if you have a Rails 3.2 app, and a Rails 4.1 app on your system, using bundler allows you to instal the dependencies (gems) for each app independently
If you use gem install x, it will install the gem for all applications, and should only be used for things like rmagick and the database connection gems
In my rails application
ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [i686-linux]
When I run
rvm gemset list
It specifies my gemset which i'm using
global
=> blackapp
now i do bundle install and gems are installed successfully.when i do gem list, it shows * LOCAL GEMS * as empty.When i run rvm gem list it shows all the gems.So what could be the reason that gem list is not working.
I think I used bundle pack and that could be the reason as it shows .
Your bundle is complete! It was installed into ./vendor/bundle
How to overcome this?
bundle install will install the gems to the vendor/cache directory within a rails app, which does not install them in the gems directory. To list the gems installed by bundler, use bundle list
use gem list --local instead of using gem list local
you also have to make sure you have selected your specific gemset you want the gems installed into. I see that you did this, but this is the number one reason for 'disappearing' gems. People forget to select the gemset prior to doing the install of gems, then end up installed in the 'default' gemset (which is used when no gemset is selected), and then they select their gemset and wonder why they're not there.
Also, do NOT use rvm to install to system. System rubies are NOT managed at ALL by RVm and the only reason why RVM supports
rvm use system
is simply to allow people to select the system ruby. RVM is not and will not be responsible for changes to the system ruby and its environment as this is usually managed by the OS's package manager. Using RVM to manage this is a Bad Idea(Tm) as a result.
I am following these instructions: https://github.com/phifty/agraph/blob/master/README.rdoc,
and there is a step to install a gem using this command:
gem install agraph
I am new to RoR, but I have RVM and Bundler. I am not sure which I should use. Should I install this using RVM or Bundler? What should be the command?
RVM is Ruby Version Manager. It lets you maintain separate installs of Ruby side-by-side without conflicts, easily. One of the requisites for doing this is keeping gems for each version of Ruby separate since the installers can "toggle" based on which version of Ruby you're running.
Gem is a command that lets you install gems. It's the core of the whole infrastructure – it doesn't call out to a lower-level "packager" to do its work.
Bundler runs "on top" of Gem. It makes managing gem versions easier, much like RVM does for Rubies (versions of Ruby). It'll automatically install gems that aren't installed, so you don't need to explicitly run gem install gem-name before running bundle (or bundle install).
Gem, Bundler, and RVM all cross paths if you start using RVM's gemsets. They allow you to further isolate and control your environment by creating a separate "gem environment" of sorts. The primary use for this is so that you can keep gems for different projects separate, which aids in managing versions. If you use Bundler, this isn't really as important or useful as in pre-Bundler days where Rails/Sinatra/etc. would require the most recent version of a gem.
Does that answer your question?
If you are using Rails 3 you should put the command in your application Gemfile
gem 'agraph'
then run bundle install. This will take care of dependency resolution and bundling the gem on your production servers when you deploy.
For RVM the only thing you might want to do is set an RVM gemset for your application. You can do this by creating a .rvmrc file in your application directory, which will automatically set the RVM ruby version and gemset when you enter into that directory. You should not manually install gems for your application using RVM.
I want to switch between rails 2.3.10 as the "active" gem for my OS, so that I can invoke it at the command line.
Is it possible to do this? I'm not using rvm. Maybe it's time to start.
I tried gem install rails --version=2.3.10, but that just makes sure that version of the gem is installed, it doesn't put it in /usr/bin/rails.
(I do already use bundler for my apps -- but haven't needed any precise control over gems at the OS level until now)
If your problem is to run binaries of a certain version, then:
rails --version # => the latest version
rails _2.3.10_ --version # => Rails 2.3.10
This pattern (gem-binary _gem-version_) works for any gem binary.
Use RVM
RVM allows you to manage different versions of Ruby and Gems. You can install a version of ruby using, for example
rvm install 1.9.2
You can then use it using:
rvm use 1.9.2
Use specific gems on a per project basis with gemsets.
If you want further namespacing you can set up gemsets; directories which will contain specific gems for a specific project.
rvm gemset create myproject
then you can use them like so:
rvm use 1.9.2#myproject
Automation
To automate the process of switching gems, pop .ruby-version and .ruby-gemset files in your project root. Pop the version of Ruby and name of the gemset you want to use inside them and RVM wil select the correct gemset when you cd into your project directory.
Installing gems into your gemset
Install your gems into your gemset in the usual way using bundler if you are using it:
bundle install
or just using the regular old:
gem install mygem
The gems will go in the right gemset.
RVM Alternatives
You might also want to check out rbenv, which does similar job.
You can use RVM
Then you can also use Bundler afterwards, which manages gem dependencies fine.
In your Gemfile
gem "rails", "2.3.10"
and in your application
require 'rubygems'
require 'bundler/setup'
and you're done.
EDIT: Just saw your RVM mention in the post. Definitely the way to go.
You're going to want to install RVM -- it's an amazing package that will let you manage different Rubys and different sets of gems on the same machine. You can switch back and forth with total ease.
Here's the installation guide: http://rvm.beginrescueend.com/rvm/install/
Once you got everything get up, you can see all of your installed rubys at the command line with with rvm list, and switch with rvm use ruby-head, for example. RVM keeps the gems on each ruby separate, which should help with your question.
Running 'sudo gem list --local' and 'gem list --local' give me differing results. My gem path is set to my home folder and only contains the gems from 'gem list --local'.
It's probably not good to have gems installed in different directories on my computer, so should I have the gem path set differently, and should I always use sudo when installing something?
my ~/.profile
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"
~/.bash_profile is empty.
You can install gems in your local environment (without sudo) with
gem install --user-install <gemname>
I recommend that so you don't mess with your system-level configuration even if it's a single-user computer.
You can check where the gems go by looking at gempaths with gem environment. In my case it's "~/.gem/ruby/1.8".
If you need some binaries from local installs added to your path, you can add something to your bashrc like:
if which ruby >/dev/null && which gem >/dev/null; then
PATH="$(ruby -r rubygems -e 'puts Gem.user_dir')/bin:$PATH"
fi
(from http://guides.rubygems.org/faqs/#user-install)
Contrary to all the other posts I suggest NOT using sudo when installing gems.
Instead I recommend you install RVM and start a happy life with portable gem homes and different version of Ruby all living under one roof.
For the uninitiated, from the documentation:
RVM is a command line tool which allows us to easily install, manage and work with multiple ruby environments and sets of gems.
The reason why installing gems with sudo is worse than just gem install is because it installs the gems for ALL USERS as root. This might be fine if you're the only person using the machine, but if you're not it can cause weirdness.
If you decide you want to blow away all your gems and start again it's much easier, and safer, to do so as a non-root user.
If you decide you want to use RVM then using sudo will cause all kinds of weirdness because each Ruby version you install through RVM has its own GEM_HOME.
Also, it's nice if you can make your development environment as close to your production environment as possible, and in production you'll most likely install gems as a non-root user.
Better yet, put --user-install in your ~/.gemrc file so you don't have to type it every time
gem: --user-install
In case you
installed ruby gems with sudo
want to install gems without sudo
don't want to install rvm/rbenv
add the following to your .bash_profile :
export GEM_HOME=/Users/‹your_user›/.gem
export PATH="$GEM_HOME/bin:$PATH"
Open a new tab in Terminal OR source ~/.bash_profile and you're good to go!
sudo gem install --no-user-install <gem-name>
will install your gem globally, i.e. it will be available to all user's contexts.
Related (for bundler users), if you want a lighter alternative to RVM which will put everything in a user-specific well known directory, I recommend using:
bundle install --path $HOME/.gem
if you want to install gems to the same place that
gem install --user-install GEMNAME
will install them, .gem/ruby/RUBYVERSION in your homedir. (See the other comment on this question about --user-install.)
This will make the gems visible to gem list, uninstallable via gem uninstall, etc. without needing sudo access. Runnable scripts installed by gem or bundler can be put into your path by adding
$HOME/.gem/ruby/RUBYVERSION/bin
to your $PATH. gem itself tells you about this if it isn't set when you do gem install --user-install.
You can install gems into a specific folder (example vendor/) in your Rails app using :
bundle install --path vendor
Installing Ruby gems on a Mac is a common source of confusion and frustration. Unfortunately, most solutions are incomplete, outdated, and provide bad advice. I'm glad the accepted answer here says to NOT use sudo, which you should never need to do, especially if you don't understand what it does. While I used RVM years ago, I would recommend chruby in 2020.
Some of the other answers here provide alternative options for installing gems, but they don't mention the limitations of those solutions. What's missing is an explanation and comparison of the various options and why you might choose one over the other. I've attempted to cover most common scenarios
in my definitive guide to installing Ruby gems on a Mac.