Help us understand the problem. What is going on with this article?

rubyのrequire '**'で`mkdir': Permission deniedと出る時の対処法

More than 5 years have passed since last update.

rubyで、nokogiriでスクレイピングしようとしたら以下のエラーが出た。
open-uriのみ記述の場合はエラーが出ないので、どうやらnokogiriが原因っぽい。

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/fileutils.rb:245:in `mkdir': Permission denied - /Library/Ruby/Gems/2.0.0/extensions/universal-darwin-14 (Errno::EACCES)
  from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/fileutils.rb:245:in `fu_mkdir'
  from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/fileutils.rb:219:in `block (2 levels) in mkdir_p'
  from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/fileutils.rb:217:in `reverse_each'
  from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/fileutils.rb:217:in `block in mkdir_p'
  from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/fileutils.rb:203:in `each'
  from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/fileutils.rb:203:in `mkdir_p'
  from /Library/Ruby/Site/2.0.0/rubygems/ext/builder.rb:210:in `write_gem_make_out'
  from /Library/Ruby/Site/2.0.0/rubygems/ext/builder.rb:132:in `build_error'
  from /Library/Ruby/Site/2.0.0/rubygems/ext/builder.rb:171:in `rescue in build_extension'
  from /Library/Ruby/Site/2.0.0/rubygems/ext/builder.rb:156:in `build_extension'
  from /Library/Ruby/Site/2.0.0/rubygems/ext/builder.rb:198:in `block in build_extensions'
  from /Library/Ruby/Site/2.0.0/rubygems/ext/builder.rb:195:in `each'
  from /Library/Ruby/Site/2.0.0/rubygems/ext/builder.rb:195:in `build_extensions'
  from /Library/Ruby/Site/2.0.0/rubygems/specification.rb:1436:in `block in build_extensions'
  from /Library/Ruby/Site/2.0.0/rubygems/user_interaction.rb:45:in `use_ui'
  from /Library/Ruby/Site/2.0.0/rubygems/specification.rb:1434:in `build_extensions'
  from /Library/Ruby/Site/2.0.0/rubygems/stub_specification.rb:60:in `build_extensions'
  from /Library/Ruby/Site/2.0.0/rubygems/basic_specification.rb:56:in `contains_requirable_file?'
  from /Library/Ruby/Site/2.0.0/rubygems/specification.rb:925:in `block in find_inactive_by_path'
  from /Library/Ruby/Site/2.0.0/rubygems/specification.rb:924:in `each'
  from /Library/Ruby/Site/2.0.0/rubygems/specification.rb:924:in `find'
  from /Library/Ruby/Site/2.0.0/rubygems/specification.rb:924:in `find_inactive_by_path'
  from /Library/Ruby/Site/2.0.0/rubygems.rb:185:in `try_activate'
  from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:132:in `rescue in require'
  from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:144:in `require'
  from /Users/sadakoa/Desktop/main.rb:1:in `<main>'

対処

% gem update
#もし怒られたら
% sudo gem update

結果、治らない。ここで1時間ちょっと頭を悩ます。
gem installした時にrbenvにパスを通すのを忘れていたので、下記コマンドを打ってみる。

% rbenv rehash

解決した。

原因

このエラー、前も見たことがあってその時はどう解決したか覚えていないんだけど、rbenv関係のエラーだった気がする。
(間違っていたらすみません)
対処法としては

  • gemをアップデートする
  • rbenvをrehashする
  • zshでPATHが通ってるか確認する。(通ってなかったら通す)
  • いっそrbenvでrubyを入れ直す

らへんを試せば治る気がする。

sadakoa
Digital Designer at Tokyo.
http://sadakoa.org
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away