10

I updated my Mac OS to 10.9 Mavericks yesterday. My current Ruby version shows as

ruby 2.0.0p247 (2013-06-27 revision 41674) [universal.x86_64-darwin13]

I got Rails installed on my machine and when I try to install gems I get the following message:

Fetching: eventmachine-1.0.3.gem (100%)
ERROR:  While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.

I am trying all this in my home directory. Any suggestions?

Aniruddha
  • 3,157
  • 4
  • 32
  • 48
  • 3
    With my "best engineering practice" hat on I agree with @Stuart Nelson. With my "if you're just learning Ruby or using it for one thing" hat on, solve this problem by using sudo. `sudo gem install eventmachine`. This will bite you if you have a few dozen separate Rails/Ruby projects from multiple sources that you're running... but if it's a simple utility here and there, ehhh... – RyanWilcox Oct 25 '13 at 01:21
  • Thanks Ryan. Just did the above command and it got installed. I know sudo gives me lots of abilities and hence many users ask to avoid it altogether. However, this is my first time and I want to try my hands on RoR, hence the effort. What should I do to not use 'sudo' every time and get a clean install without having to think of installing any useless things – Aniruddha Oct 25 '13 at 01:33
  • 3
    I would argue that rvm and rbenv are quite useful, and not useless at all. They are your best option to avoid having to use `sudo` every time, and getting yourself into a versioning nightmare down the road. – Stuart Nelson Oct 25 '13 at 01:36
  • 1
    If you're using Rails the Bundler gem should keep the gems for your different apps away from each other. I know people that use almost exclusively that. Just using that - like every Rails tutorial written after... 2011(?) will show you how - may make you happy for maybe even your first year of Rails development. (I didn't start wanting RVM until my first year / 4th or 5th client project) – RyanWilcox Oct 25 '13 at 01:52
  • @AndrewMarshall I was aware of that thread and referred it before creating this one. But all the steps mentioned in it didn't help me. Even now, I was able to install ruby and rails through RVM, but still getting permission error while installing gems. Although `sudo` is coming handy and I can get work done. I would appreciate if could point me to right direction if you think I'm missing something. I'm running all these commands in my home directory as you have edited. – Aniruddha Oct 26 '13 at 22:40
  • I installed RVM but I can't run gem install rspec in terminal. What is going on? I get an error message. – Jwan622 Oct 20 '14 at 04:20

4 Answers4

11

Use a Ruby version manager, like RVM or rbenv.

I can't speak for rbenv, but RVM prepends a Ruby version-specific bin to your PATH that you have access to, so you don't have to use sudo (which is how you could install gems in your current situation, but is highly discouraged).

rbenv similarly prepends to your $PATH, but it uses a shim.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Stuart Nelson
  • 4,202
  • 2
  • 23
  • 26
  • 1
    I tried installing rvm but I am getting into permission issues. bash: line 704: /Users/aniruddhabarapatre1/.rvm/RELEASE: Permission denied – Aniruddha Oct 25 '13 at 01:20
  • what do you get when you enter `ls -l ~/.rvm/RELEASE` on the command line? Something is funny with your permissions. – Stuart Nelson Oct 25 '13 at 01:31
  • 1
    you would get something like `-rw-r--r-- 1 aniruddhabarapatre1 staff 7 Oct 24 19:07 .rvm/RELEASE` if you had access. – Stuart Nelson Oct 25 '13 at 01:32
  • Yes. This is something I'm getting. `-rw-r--r-- 1 root staff 7 Sep 1 14:37 /Users/aniruddhabarapatre1/.rvm/RELEASE` – Aniruddha Oct 25 '13 at 01:35
  • 2
    Ah yeah. You installed rvm as root (see how it says 'root' in there), so you don't have access to it. You need to remove the `.rvm` folder with `sudo rm -rf .rvm`, and then install rvm without sudo. It will then be owned by you, and not root. – Stuart Nelson Oct 25 '13 at 01:39
  • I tried above command and I still get following message - `-rw-r--r-- 1 root staff 7 Sep 1 14:37 /Users/aniruddhabarapatre1/.rvm/RELEASE` – Aniruddha Oct 25 '13 at 01:48
  • 2
    did the above command successfully remove `.rvm`? Make sure that folder is gone, log out of your terminal, log back in, and then the rvm install command: `\curl -L https://get.rvm.io | bash -s stable` – Stuart Nelson Oct 25 '13 at 02:10
  • Thanks a ton @Stuart. This helped me a lot. I could install rvm with your instructions and now will move ahead. Anything else that I should be careful about? Although I am still getting permission issues when I'm trying to install gems – Aniruddha Oct 25 '13 at 02:29
  • 1
    The last step is to use rvm. Type `rvm install ruby`, then make sure it is set to the default. You shouldn't be required to use sudo after that. – Stuart Nelson Oct 25 '13 at 13:55
  • Should I be doing this in my home directory? I have rails and rubyv2 installed on my machine, but installing gems still needs `sudo` to be prepended to it. – Aniruddha Oct 26 '13 at 22:42
  • 5
    `rvm install ruby`; `rvm use ruby --default`; if you have more questions post a new question or read the documention: https://rvm.io/rvm/basics – Stuart Nelson Oct 26 '13 at 23:02
10

When I had this same error it was because I did not have a Ruby version manager installed, it defaulted to the Ruby version that came shipped with my computer which is owned root.

Since it is a very bad idea to use sudo to install gems, I installed RVM with Ruby 2.1.2

\curl -sSL https://get.rvm.io | bash -s stable --ruby=2.1.2

then made sure that it was being used as the default by running

rvm use ruby-2.1.2

RVM creates a new location to store your Ruby and gems with your user permissions so you don't have to use sudo or get a permissions error. The new gem location is:

~/.rvm/gems

Then carry on as you were...

gem install ....
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Kingsley Ijomah
  • 3,273
  • 33
  • 25
  • this worked - note (on osx) for some reason 'ruby --version' command showed old version (2.2.0) even though latest was installed. 'rvm list' will show versions installed and available to be switched to. After switching (rvm use ruby-2.1.2) then 'ruby --version' showed correct (2.1.2) version. – gamozzii Mar 08 '16 at 03:35
5

I do not have RVM installed. I got this error because I installed rbenv but did not run rbenv init yet.

After I inserted the contents from the command rbenv init into .bash_profile, gem install works as expected.

I also happened to set rbenv global <ruby-version> to a downloaded Ruby version first.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Jonathan Lin
  • 19,922
  • 7
  • 69
  • 65
  • If [the rbenv install directions](https://github.com/rbenv/rbenv) are followed the initialization code will be installed automatically. – the Tin Man Mar 06 '17 at 23:09
0

Just like to say to you all after wasting a good hour or more on trying to install RubyGems... to rubygems-2.4.5 'sudo' got me through! It works a treat...

  • 1
    don't do this. it would be better to give access to that directory to the user performing the install or by using rbenv or rvm – engineerDave Feb 17 '16 at 21:39
  • `sudo` can create more problems than it'll solve unless you're very familiar with how the system and Ruby, especially multiple Rubies, work together. – the Tin Man Mar 06 '17 at 23:10