gem issue when using sudo - ruby

I have made a script that is already working properly in my development environment with RVM. This script reads *.eml files and parses them.
Now, my problem is this. When I transfer it to the production server, it gives a Permission denied - filename.eml (Errno::EACCES) when reading some files. When I manually read these files using sudo nano filename.eml, it is readable so I thought running my ruby script with sudo is the answer.
But when I tried running it with sudo ruby scriptname.rb, this time it gives me a cannot load such file -- mail (LoadError).
The mail gem is clearly installed and is shown both in gem list --local and sudo gem list --local.
The ruby versions are identical with or without sudo (1.9.3).
The mail gem is also accessible using sudo irb then typing require 'mail'.
Both development and production environments are on a Mac OS with RVM-managed ruby versions.
Any help will be greatly appreciated. Thanks.

maybe the gem is not readable for your server user, such as 'wwwroot', so please make sure the file is 775 mode. e.g.
cd $GEM_HOME
sudo chmod 755 -R .
then everything is readable for other group's users.

Related

OSX: Permissions issue after installing Ruby Gem or Node modules

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.

Error sudo: gem: command not found

I'm trying to install rubyrep on CentOS, and when executing sudo gem install rubyrep I get the error sudo: gem: command not found. Sudo and gem are both installed and working, just not when combined.
My setting:
[rubyrep]# which sudo
/usr/bin/sudo
[rubyrep]# which gem
/usr/local/rvm/rubies/ruby-2.1.2/bin/gem
[rubyrep]# which ruby
/usr/local/rvm/rubies/ruby-2.1.2/bin/ruby
[rubyrep]# $PATH
-bash: /usr/local/rvm/gems/ruby-2.1.2/bin:/usr/local/rvm/gems/ruby-2.1.2#global/bin:/usr/local/rvm/rubies/ruby-2.1.2/bin:/usr/local/rvm/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin: No such file or directory
Try without sudo. You should only use sudo if you did a root installation of ruby, which is not recommended.
From http://web.archive.org/web/20130405074724/http://www.kleinfelter.com/node/188:
There are hundreds of reports on the net about "sudo: gem: command not
found". Out of the dozen or so I looked at, the suggested resolution
amounted to "ensure that 'gem' is in your PATH."
Yep. It is in my path. That is to say, the following works:
gem --help
but the following fails:
sudo gem --help
Further confusing matters, gem is in the path shown by:
sudo echo $PATH
The trick is that sudo doesn't use $PATH for its path on some editions
of Linux. This behavior is considered "more secure." (See
sudo changes PATH - why? ).
Bah! It is still a pain to work around. You have to get 'gem' into
the path (not simply the $PATH!) You'll read about options you can
pass to sudo, and that is fine and dandy when you type the
command-line yourself. When someone's script invokes sudo for you,
you're stuck with the command line that it uses.
I fixed it by doing:
alias sudo='sudo env PATH=$PATH'
as suggested in sudo changes PATH - why?.
sudo env PATH=$PATH gem install rails
It works for me.
If anyone runs across this, like I did, and the without sudo isn't an option. You may want to check your sudoers for the 'secure_path'.
If this doesn't include the path of your installation/gem/ruby (which gem). Then you'll also get this error.
Fedora 23, built from source.

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.

Superuser Issue in ruby kubuntu

I have installed ruby and gems with sudo,
Whenever I run the script, I have to do it with sudo or else I will get this error:
/usr/local/rvm/rubies/ruby-1.9.3-p385/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- watir-webdriver (LoadError)
from /usr/local/rvm/rubies/ruby-1.9.3-p385/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'from Login.rb:1:in `<main>'
I am planning to use eclipse IDE, so--if I do run the script in eclipse--I get the same problem
Could someone help me in running ruby script as superuser in eclipse?
You can change the access permissions for needed gem.
sudo chmod 644 filename
To know the full path:
gem which gemname

RubyGems installation errors both when using 'sudo' and not using sudo

I have a machine that is running Ubuntu Hardy, which provides its own RubyGems package. Unfortunately that version of RubyGems (1.1.1) is too old to do anything useful with, so I decided to manually update RubyGems to the current version (1.3.6). That part went smoothly, and if I do gem -v, I get 1.3.6 which is expected. The problem is when I try to do: sudo gem install rack, it returns this error:
ERROR: While executing gem ... (Errno::EACCES)
Permission denied - /home/username/.gem
Usually when I install gems as root, it knows to install it into /usr/lib/ruby/gems, so why is it checking my home directory at all? Another quirk is when I do gem install rack (not as root), it says:
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions into the /usr/lib/ruby/gems/1.8 directory.
which is where I want it to go. I've already tried clearing source_caches, trying different versions of RubyGems (1.3.5), forcing installation into /usr/lib with -i to no avail. Any ideas on why RubyGems is so insistent on checking my /home directory when installing as root?
Sounds like it could be a path issue coupled with having multiple versions installed.
Any difference in output between:
sudo gem env
and
gem env
Try running:
gem environment
and checking the values for the GEM PATH. More info at http://docs.rubygems.org/read/chapter/10#page31
I was running into the same problem myself on Fedora 15, so I ran 'gem install' with the '--backtrace' option to see what was going on.
It turned out it was failing at /usr/lib/ruby/site_ruby/1.8/rubygems/doc_manager.rb:203 where it tried to chdir to the directory it had previously stored (the home directory of the user I was running sudo as)
I didn't extensively debug to see what the underlying cause was, just rather used a quick workaround so I could continue moving forward. The workaround was simply to cd to the root directory, eg cd /, before running the gem install command.
Hope this helps / solves your issue.
Woulda been easier to su (password) then chmod 755 /usr/lib/ruby/gems/1.8

Resources