How do I fix the "You don't have write permissions into the /usr/bin directory" error when installing Rails?
Asked Answered
V

12

207

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?

Virago answered 23/5, 2010 at 23:13 Comment(3)
I was getting same error on first command, use this command sudo gem update --system -n /usr/local/binDuodenitis
What is the -n?Zinc
Just going to put it out here that simply restarting the terminal solved this for me.Rocca
O
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

Oracular answered 27/5, 2010 at 9:52 Comment(2)
only doing that solved for me a similar problem with the same error messageArras
It seems RVM project (or link at least) is outdatedCalypso
S
660

use -n parameter to install like for cocoapods:

sudo gem install cocoapods -n /usr/local/bin
Schlock answered 25/1, 2016 at 9:59 Comment(7)
Why would we even need this?Sesqui
Fails on MacOS unless you specify -n /usr/local/bin. Thanks for the solutionPamulapan
What does -n means?Payment
according to 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/binCoarsegrained
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.Natation
thanks so much this works, but do we have to write -n /usr/local/bin for every gem installation??Eyelid
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.Impulsive
F
117

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!

Fro answered 6/9, 2018 at 2:49 Comment(3)
vim .gemrc how to write this cmdNonproductive
I am not sure this will work in Window$, but I guess you could use Notepad.Fro
Thank you, just like you I was tired of looking for this suffix everytime I needed to make an update!Interstitial
N
96
sudo gem install cocoapods --pre -n /usr/local/bin

This works for me.

Neonate answered 19/6, 2018 at 2:41 Comment(2)
I had this issue and this answer worked for me! I replaced cocoapods with the dependency that I neededPaxton
Worked for me also with macOS Big Sur 11.6.1Boson
S
39

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

This works for me.

Sebastien answered 9/12, 2017 at 20:0 Comment(0)
O
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

Oracular answered 27/5, 2010 at 9:52 Comment(2)
only doing that solved for me a similar problem with the same error messageArras
It seems RVM project (or link at least) is outdatedCalypso
E
8

On macOS High Sierra, this solved my issue:

sudo gem update --system -n /usr/local/bin/gem
Eighth answered 11/10, 2019 at 2:36 Comment(1)
Worked for me with /usr/local/bin (without /gem)Mammy
D
7

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

sudo gem install rails --pre
Dyche answered 27/5, 2010 at 10:38 Comment(1)
NO. Use rvm or rbenv ...do not just sudo.Objectivism
R
6

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

Reference RailsCast's RVM Install tutorial.

Rewarding answered 22/12, 2015 at 19:41 Comment(1)
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.Mcbryde
C
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
Cloverleaf answered 11/6, 2018 at 18:30 Comment(0)
J
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.

Juniorjuniority answered 25/7, 2021 at 10:9 Comment(0)
K
1

Following the instructions in this answer helped me:

https://mcmap.net/q/82377/-i-am-unable-to-install-rails-and-have-tried-installing-ruby-version-3-7-multiple-times

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

Knobkerrie answered 19/11, 2022 at 14:26 Comment(0)
S
0

In Linux use the following command to fix it:

bundle config path /PATH/TO/YOUR/PROJECT
Swainson answered 9/4 at 22:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.