OSX: Permissions issue after installing Ruby Gem or Node modules - ruby

I've recently started having an issue installing any Ruby gem or node modules on my local dev environment when it comes to installing anything via the terminal. It was working normally for some time.
sudo gem install compass
This appears to install correctly however when i run
compass -v
I get
/usr/bin/compass: Permission denied
When I browse to /usr/bin/compass and "Get Info" on compass it says system - Read & Write, everyone - No Access as opposed to everyone - read only or...
-rwx------ 1 root wheel 512 Jul 27 14:46 compass
For some reason everything is getting written with incorrect permissions.
Same goes for Node.js modules.
sudo npm install gulp
Appears to work but I get the same permissions issue when I try to use it
/usr/local/bin/gulp: Permission denied
I've exhausted my search and hope someone can help provide some insight on how I can permanently fix this issue

This is happening because you're installing gems with sudo and, as such, root is taking possession of everything. So, instead of using sudo I'd advise using RVM. RVM provides you with an easy way to manage multiple versions of Ruby, each of which gets their own home for gems in ~/.rvm which avoids the permission difficulties you're experiencing. Additionally, it's possible (but not recommended) to do a global RVM install that works for all users.
EDIT: As the Tin Man mentioned in response to me, a global RVM install is not only not recommended in a general sense, but the RVM authors themselves advise against it. Caveat emptor and such. You should find the single-user version more than adequate in any case, though.

Related

How to fix gem permission error after installing rbenv?

I was trying to install Sinatra and I kept on getting this error: You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory
Thanks to "Installing gem or updating RubyGems fails with permissions error" I installed rbenv. I followed the guide and set my global ruby version, etc. but I continue to get the same error when I try to install gems. When I run gem environment the installation directory for RubyGems is still the system Ruby directory.
Should I update GEM_PATH? Since the rbenv guide doesn't mention anything about that, something makes me think that there is still a problem with my rbenv installation.
Can someone please help me figure this out?
You're not showing us the commands you're using but it smells like you're using sudo to install Sinatra. Don't do that with rbenv or RVM managed Rubies.
Just as in the linked question, using rbenv or RVM allows you have one or more Rubies in your user-space where you can modify them all you want. That means you don't need to use sudo, just use gem install ....
An alternate problem you could be having is you set your global Ruby to be system, which is the version installed by Apple for their use, and which you don't want to modify unless you understand why it's there and what they're using it for. IF you have to change it then sudo would be appropriate but, with rbenv or RVM managing Rubies in your user-space there's really no reason to.
Do NOT use chmod to change the ownership of the vendor installed gems; Again, that's for Apple's use so have fun with the local Rubies instead and leave Apple's alone.
In this case, I quit Terminal and upon reopening, things were working correctly. Probably a good thing to try if you're stuck and are sure you've followed instructions correctly. It's not explicitly mentioned in the material I read but I believe is a good practice in general.

"Don't run bundler as root" - what is the exact difference made by using root?

If you run ruby bundler from the command line while logged in as root, you get the following warning:
Don't run Bundler as root. Bundler can ask for sudo if it is needed,
and installing your bundle as root will break this application for all
non-root users on this machine.
What is this exact difference that running bundler as root makes to the gems it installs?
Is it to do with the permissions of the actual files that it installs for each gem? Will Ruby try to access the gem files as a non-root user (and if so, what user / group would Ruby use and how would I find out)?
What would be the symptoms of an application that is broken due to bundler being used as root?
My specific reason for asking is because I'm trying to use bundler on a very basic Centos VPS where I have no need to set up any non-root users. I'm having other problems with gems installed via bundler (Error: file to import not found or unreadable: gemname despite the gem in question being present in gem list), and I'm wondering if installing the gems via bundler as root might have made the files unreadable to Ruby.
I want to work out if I do need to set up a non-root user account purely for running bundler, and if I do, what groups and privileges this user will need to allow Ruby to run the gems bundler installs.
Or can I just chown or chgrp the gem folders? If so, does it depend on anything to do with how Ruby is installed? (I used RVM and my gems end up in /usr/local/rvm/gems/ which is owned by root in group rvm) This loosely related question's answer implies that unspecified aspects of how Ruby is installed influence bundler's permissions requirements.
Researching the "Don't run bundler as root" message only comes up with an unanswered question and complaints that this warning is apparently "like it saying to go to sleep at 8PM" (link contains NSFW language).
So I had to dig into the git log history of bundler's repo, because GitHub doesn't allow search in git commits messages anymore.
The commit c1b3fd165b2ec97fb254a76eaa3900bc4857a357 says :
Print warning when bundler is run by root. When a user runs bundle install with sudo bundler will print a warning, letting
them know of potential consequences.
closes #2936
Reading this issue, you understand the real reason you should not use the root user:
Running sudo bundle install can cause huge and cascading problems for
users trying to install gems on OS X into the system gems. We should
print a warning and explain that Bundler will prompt for sudo if it's
needed. We should also warn people that sudo bundle will break git
gems, because they have to be writable by the user that Bundler runs
as.

Command Not Found Jekyll

I'm trying to create and publish a Jekyll blog using this template.
I installed Jekyll initially. Without any luck, I did resort to installing it via sudo. (And have now since uninstalled it!)
Jekyll(1.4.2, 1.0.3) and am running rvm with ruby 2.0.0p195. After installing the gem, I run jekyll serve in the folder but get the error: Zsh Command Not Found. I tried changing my path to no avail. Any advice on how to troubleshoot?
I cannot uninstall/reinstall nokogiri (see other SO articles on this) because of dependencies related to work projects.
If you're using rvm you should not install gems with sudo. Rvm is user-specific (the binaries are all in your home directory), so putting Ruby stuff in system-wide folders with sudo will not work. Just install without sudo and you should be ok.
The rvm docs tries to explain this a little bit here:
http://rvm.io/rubies/rubygems
DO NOT use sudo...
...to work with RVM gems. When you do sudo you are running commands as root, another user in another shell and hence all of the setup that RVM has done for you is ignored while the command runs under sudo (such things as GEM_HOME, etc...). So to reiterate, as soon as you 'sudo' you are running as the root system user which will clear out your environment as well as any files it creates are not able to be modified by your user and will result in strange things happening. (You will start to think that someone has a voodoo doll of your application...)

Mac terminal problems

I recently switched over from windows pc to mac for development and i´m looking for some help regarding the terminal.
When i try to install compass wich is a ruby gem, i´m met with this error:
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/1.8 directory.
Is there a way for me to execute gem installments etc without doing sudo?
I found out that this "sudo chown -R $USER /usr/local" worked for npm. Now im looking for a way to do the same with ruby and yeoman.
As the commenters have pointed out, it is recommended that you try to use rvm or its cousins, to install ruby into your home directory, this way, you dont need root to install gems by default, and this is more widely recommended.
Alternatively you could try the same chown trick here
try
sudo chown -R <username>:<username> /Library/Ruby/Gems
The second one is usually group name, it can be skipped, or you can let it stay.

What is the correct way to install brews and gems on OS X

It seems every time I want to install something locally on Snow Leopard, I run into an issue about whether to install to /usr/local or whether or not to run install commands using sudo. I've read conflicting advice on this, including on stackoverflow
Gem install errors writable and PATH (use sudo)
gem install permission problem (do not use sudo in comments)
Should I be installing brew formula and gems using sudo? How can I avoid errors and warnings when I install without sudo privileges?
e.g when running gem update:
WARNING: Installing to ~/.gem since /Library/Ruby/Gems/1.8 and
/usr/bin aren't both writable.
WARNING: You don't have /Users/luke/.gem/ruby/1.8/bin in your PATH,
gem executables will not run.
I'm not a ruby developer but want to use sass and compass, so it seems like overkill to install rvm. Will just adding ruby to my path resolve all these issues? I also saw somewhere that I should chown /usr/local. It would be good to finally get to the bottom of this.
You're doing yourself a great disservice by dismissing RVM. I don't think it is overkill at all, in the time it would have taken you to write this SO question, RVM would have installed AND you would have also finished installing Ruby (1.8 or 1.9 whichever you prefer). Then your gem warnings and sudo problems would have been non-issues. Pretty much everyone in the ruby community is using RVM these days (unless they are on windows), including, in some cases, in production.
Having said all of that, Homebrew (which you mentioned) has a wiki page that talks about sudo, installing gems to /usr/local, rubygems etc. You could do worse than follow their advice.
I prefer to use sudo, so I am sure that programs running with normal privileges can't mess with system-wide files. I also normally run as a non-administrator user for the same reason. This means that I am protected from damaging the system even accidentally, but when I decide to install something to /usr/local I have to use sudo, so I only do that when I'm sure that it won't make a mess. For stuff that I'm not sure about, or that I simply want to try, etc., I install to a prefix in my home.
Other people have a more single-user mentality, and chown /usr/local to themselves. They're ok with the risk of messing up its contents with normal usage: for instance, if they try to install something to a prefix in their home, but the configure is borked, it could install some pieces in /usr/local without so much as a warning. OTOH, when they install to /usr/local they are sure that they won't affect the rest of the system, only /usr/local and their own home.
But then, /usr/local is just an extension of their home directory, since it has the same ownership and the same privileges. They might as well install to ~/usr/ and add ~/usr/bin to their PATH, which is what I do when I want to install things for myself.
The very best way would probably be to chgrp /usr/local to some special group, and sudo to a special user in that group that can write to that directory, but not do other random sudo stuff. Maybe someday I'll get around to configuring it that way. In the meantime, I prefer to use sudo when I want to install to /usr/local. The advantage of using a package manager like homebrew is that formulas have been checked to ensure that they install cleanly, and they can easily be uninstalled.

Resources