I am trying to install ruby on rails. I'm at version 2.3.0 for ruby and gem version of 2.5.1.
When I run the command gem install rails I get an error that says:
ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /usr/local/lib/ruby/gems/2.3.0 directory.
How can I fix this?
gem install tries to put files in "system-wide" directories. These are protected, such that only the root user can write into.
You have at least three possibilities:
sudo gem install ..., this will write the gem files into /usr/local/lib/ruby/gems/2.3.0
gem install --user-install, this will install the gem "locally" in your users home directory
apt-get install ruby-rails (not sure about the package name), which will install the ruby on rails version maintained by your distributions maintainers (Canonical).
install and use rvm or rbenv (and there might be other options). Afterwards gem install will usually just work and install the gems for your user only
While the rvm setup might be a tiny bit confusing for newbies, I recommend using that approach. It will make updating and installing ruby, gems and different versions of it really easy.
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
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'm very new to Rails (and Ruby), and am having trouble installing and using gems. I'm trying to use ruby-tmdb (https://github.com/aarongough/ruby-tmdb) and there's very little documentation.
"sudo gem install ruby-tmdb" runs just fine and I can see the gem installed when I run "gem list --local"
But, when I try and run the app, I get the error "no such file to load -- ruby-tmdb".
I'm on Mac OS X Snow Leopard. Ruby 1.8.7. Rails 3.0.3. Gem 1.3.7.
Is the gem listed in your Gemfile? In Rails 3, all gem dependencies should be listed in the Gemfile, so that it is properly loaded when the app runs.
You should have something like the following line:
gem 'ruby-tmdb'
Then, run bundle install to ensure that all gem dependencies are installed, and to have Bundler save the lock file that will ensure that all copies of this application run with the same gem versions. From this point on, you will no longer have to write the require line yourself; Rails will load in all necessary gems as the environment loads.
You might get the same error even after this, but it's always worth going through the standard process to help narrow things down :)
$ sudo which gem
$ which gem
$ sudo ruby -v
$ ruby -v
Sometimes users have different gems and rubys compared to root.
A common problem is that a gem installed for ruby 1.8 by root isn't visible for the users ruby 1.9
gems for ruby 1.8 and gems for 1.9 are NOT compatible.
This probably isn't the issue but is something that may be worth considering.
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.