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.
Related
I've read all the other Stackoverflow posts on this problem but don't see any suitable solution for rbenv, so i'll post it on here. (most of the other posts suggest either using RVM, or the OP had already ran sudo gem install rails).
when i run gem install rails (without sudo), i get
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
is /Library/Ruby/Gems/2.0.0 the correct spot?
why am i getting this error? i'm on a fresh laptop that hasn't had much install.
i've got homebrew, rbenv, and ruby 2.0
rbenv should not be trying to install to your System install of Ruby. If you check the permissions on /Library/Ruby/Gems/2.0.0 you'll see it's owned by root, which means you'll need to sudo to write to it, which we're trying to avoid.
This suggests that either
There is something wrong with your rbenv installation, because it should be looking at /Users/whoami/.rbenv/versions/2.0.0. As you can see in their documentation. I'd confirm that your rbenv is operating correctly or maybe try reinstalling to make sure your rbenv installation of ruby is taking priority.
Your system ruby might be clobbering your rbenv. Have you made sure that your system ruby isn't taking precedence in your path? Try running gem list rake -d with a gem you already have installed to see where your gems are currently installed
We are deploying our apps as RPM linux packages (with all the dependencies also packaged in RPMs). It turns out that bundler is problematic in this situation and it only complicates our build process - we would like to get rid of it.
Is it possible to run Rails 3 app without it forcing Ruby to use system rubygems? How?
In the book Rails 3 Way there is a statement describing that the easiest way to remove Bundler is to delete Gemfile* files. That's it. It just works.
You could install all gems manually using gem install gemname. In your situation or if you do not have sudo rights it is perhaps recommendable to install the gem files locally in your user directory using
gem install --user-install gemname
You can also install your gems locally with bundler:
bundle install --path ~/.gem
I have a weird trouble, i've updated the .irbrc file to make better looking, for this I
installed awesome_print, wirble and looksee gems, check them by gem list - every gem is there.
And when i run rails console i got this:
Cannot find awesome_print gem. Please run 'gem install awesome_print' to install it.
Cannot find wirble. Please run 'gem install wirble' to install it.
Cannot find looksee. Please run 'gem install looksee' to install it.
I've no idea why it happen. I have rvm on my system but I don't think it causes the problem.
Thanks.
To install gems in Rails 3, add them to your Gemfile
For instance:
gem 'awesome_print'
gem 'wirble'
gem 'looksee'
Then run bundle install.
It's also a best practice to create a gemset with rvm per-project to isolate dependencies among projects.
To do this, in your Rails root directory run: rvm --create --rvmrc 1.9.2#myproject (substitute 1.9.2 with whatever version of Ruby you want to use).
After creating the gemset, rerun bundle install.
You'll notice an .rvmrc file has been created, which makes sure whenever you cd to that directory, the "myproject" gemset will automatically be used. Add this to version control so other developers get the same effect.
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.
I've got an issue installing gems on my mac (os 10.6).
I used to be able to run
gem install <gem-name-here>
but after updating something, it could be the version of gem I'm using, but it's unlikely, I now get the error:
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions into the /usr/bin directory.
On the face of it, it looks like my 'GEM_HOME' isn't set. If so, why has this been unset, and how can I change it back?
Secondly - when I run
gem list
I see all gems - including those in ~/.gem, but when I run:
gem server
I only see gems in /usr/bin... strange huh?
Any help would be great to resolve this - I dont like using sudo to install gems constantly.
Install RVM.
Profit!
It really is that simple. In addition, you will be able to install and easily switch between different Ruby versions and sets of gems with a single command. It will all be installed in ~/.rvm (by default) and you won't need to use sudo to install gems.
Have you tried doing $bundle update after installing the gems that you wanted?
Follow the instructions in this guide:
export GEM_HOME=$HOME/.gem
export PATH=$GEM_HOME/bin:$PATH
gem install <gem-name-here>
If you were already doing this, completely delete the ~/.gem directory and try again.