202

I'm trying to install Rails 3 on a brand new MacBook Pro running OS X 10.6.3, Ruby 1.8.7, and Rails 2.3.5 and I'm wondering if I've hosed myself. So far, I've run these commands:

$ gem update --system
$ gem install arel tzinfo builder memcache-client rack rack-test rack-mount erubis mail text-format thor bundler i18n
$ gem install rails --pre

However, when I run the last command, I get this error:

ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions into the /usr/bin directory.

I think it wants me to run the command with sudo so that it can write over /usr/bin/rails. But if I do that, won't I be overwriting my Rails 2.3.5 executable? I don't want to do that. Instead, I'd like to keep both versions of Rails. How do I do that?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Jim
  • 13,430
  • 26
  • 104
  • 155

11 Answers11

645

use -n parameter to install like for cocoapods:

sudo gem install cocoapods -n /usr/local/bin
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Pradeep Kachhawaha
  • 6,589
  • 2
  • 14
  • 13
  • 8
    Why would we even need this? – nenchev Oct 31 '17 at 18:54
  • 40
    Fails on MacOS unless you specify -n /usr/local/bin. Thanks for the solution – Sacky San Dec 12 '17 at 23:22
  • 6
    What does -n means? – allenlinli Apr 03 '18 at 03:20
  • 11
    according to http://guides.rubygems.org/command-reference/#gem-install, -n specifies the directory where binaries are located. I also needed `-n /usr/local/bin` on my Mac. I ran `which pod` to determine its existing location before using the -n option which was indeed /usr/local/bin – mwu Apr 03 '18 at 18:07
  • 8
    I'm sure RVM is great, but this is a better answer that directly addresses the immediate problem without sending me down a rabbit hole. – Wayne Aug 15 '18 at 15:13
  • thanks so much this works, but do we have to write -n /usr/local/bin for every gem installation?? – Kimmiekim Jan 01 '19 at 01:44
  • Can't agree with you more. I suggest we always remember the option `-n /usr/local/bin` because `/usr/bin` belongs to the system, instead of to the end user. – DawnSong Sep 27 '19 at 08:03
113

Using the -n /usr/local/bin flag does work, BUT I had to come back to this page every time I wanted to update a package again. So I figured out a permanent fix for this.

For those interested in fixing this permanently:

Create a ~/.gemrc file

vim .gemrc

With the following content:

:gemdir:
   - ~/.gem/ruby
install: -n /usr/local/bin

Now you can run your command normally without the -n flag.

Enjoy!

Francois Nadeau
  • 7,023
  • 2
  • 49
  • 58
96
sudo gem install cocoapods --pre -n /usr/local/bin

This works for me.

Erik A
  • 31,639
  • 12
  • 42
  • 67
tong Xu
  • 983
  • 6
  • 4
39

You can use sudo gem install -n /usr/local/bin cocoapods

This works for me.

iWheelBuy
  • 5,470
  • 2
  • 37
  • 71
Damini
  • 395
  • 3
  • 5
16

I'd suggest using RVM it allows you have multiple versions of Ruby/Rails installed with gem profiles and basically keep all your gems contained from one another. You may want to check out a similar post How can I install Ruby on Rails 3 on OSX

Tarellel
  • 869
  • 13
  • 17
8

On macOS High Sierra, this solved my issue:

sudo gem update --system -n /usr/local/bin/gem
Subhan
  • 1,544
  • 3
  • 25
  • 58
7

To fix your specific error you need to run that command as sudo, ie:

sudo gem install rails --pre
Andrew Nesbitt
  • 5,976
  • 1
  • 32
  • 36
6

This Error hit me after installing RVM correctly. Solution: re-boot Terminal.

Reference RailsCast's RVM Install tutorial.

Akorn
  • 102
  • 1
  • 6
  • Not exactly 7 years later, but this works also if you're working on windows bash subsystem. Just exit the bash and launch it again. Thank you. – Pedro Madrid Feb 05 '18 at 15:39
1

For me, something different worked, that I found in on this answer from a similar question. Probably won't help OP, but maybe someone like me that had a similar problem.

You should indeed use rvm, but as no one explained to you how to do this without rvm, here you go:

sudo gem install tzinfo builder memcache-client rack rack-test rack-mount \
  abstract erubis activesupport mime-types mail text-hyphen text-format   \
  thor i18n rake bundler arel railties rails --prerelease --force
Sebastian D'Agostino
  • 1,575
  • 2
  • 27
  • 44
1

Problem: Performing some operations such as bundle installation / npm install on docker container prompts a lack of permissions error and preventing us accomplish applicative tasks. By default docker containers are "unprivileged" and not allow to accomplish out of scope administrator capabilities.

ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /usr/local/bundle directory.

Solution: Use --previliged flag to enable all devices and allow the container nearly all the same access to the host as processes running outside containers on the host. Pay attention this might be a security issue under some circumstances and recommended only on test/staging/development environments.

By default, Docker containers are “unprivileged” and cannot, for example, run a Docker daemon inside a Docker container. This is because by default a container is not allowed to access any devices, but a “privileged” container is given access to all devices (see the documentation on cgroups devices).

When the operator executes docker run --privileged, Docker will enable access to all devices on the host as well as set some configuration in AppArmor or SELinux to allow the container nearly all the same access to the host as processes running outside containers on the host. Additional information about running with --privileged is available on the Docker Blog.

For additional reading about Runtime privilege and Linux capabilities read this Documentation.

avivamg
  • 12,197
  • 3
  • 67
  • 61
1

Following the instructions in this answer helped me:

https://stackoverflow.com/a/72094586/755451

Essentially I think its down to the mentioned steps:

Step 2 Add the following lines to your .zshrc file

export PATH="$HOME/.rbenv/bin:$PATH" eval "$(rbenv init - zsh)" Step 3

brew update && brew upgrade ruby-build

Bynho
  • 572
  • 6
  • 16