gem install が permissions エラーでインストールできない場合の対処手順

xcodebuild コマンドを使ったビルドする際に、ログを整形してくれる xcpretty をインストールしようとした時の悪戦苦闘を記録した記事です。

似たような状況に陥った人も多いと思うので誰かの参考になれば・・・。

gem install で permissions エラーでインストールできない場合の対処手順

gem install xcpretty を実行したら下記のようなエラーが発生しました。

% gem install xcpretty
Ignoring ffi-1.13.1 because its extensions are not built. Try: gem pristine ffi --version 1.13.1
Fetching rouge-2.0.7.gem
Fetching xcpretty-0.3.0.gem
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.

sudo を付けてみましたがダメでした。

% sudo gem install xcpretty
Ignoring ffi-1.13.1 because its extensions are not built. Try: gem pristine ffi --version 1.13.1
Fetching rouge-2.0.7.gem
Fetching xcpretty-0.3.0.gem
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.

エラーの原因は、システムに予めインストールされている /usr/bin/gem のインストール先(/Library/Ruby/Gems/2.6.0)の書き込み権限がない為です。

which gem で現在の gem コマンドがシステムのディレクトリ(/usr/bin 配下にあることがわかります。

% which gem
/usr/bin/gem

gem は ruby のパッケージ管理システムです。

デフォルトではシステムの ruby が使われるため、今回は rbenv コマンドを利用してシステムとは異なるディレクトリに ruby をインストールしてそちらを使う方法を取りました。

brew でインストールするので予め update しておきます。

brew 自体のインストールがまだの方はこちらからインストールできます。

% brew update

続いて brew install で rbenvruby-build をインストールします。

% brew install rbenv ruby-build
Error: The following directories are not writable by your user:
/usr/local/lib/pkgconfig

You should change the ownership of these directories to your user.
  sudo chown -R $(whoami) /usr/local/lib/pkgconfig

And make sure that your user has write permission.
  chmod u+w /usr/local/lib/pkgconfig

いきなりエラーです。

/usr/local/lib/pkgconfig ディレクトリの権限(オーナーシップ)が実行ユーザーと異なるためのようです。

ls -l で確認するとユーザーが root グループが wheel となっていました。

ls -l /usr/local/lib/pkgconfig 
total 8
lrwxr-xr-x  1 root  wheel   10  2  1 14:40 fuse.pc -> osxfuse.pc
-rw-r--r--  1 root  wheel  258 12  5  2019 osxfuse.pc

これを chown で一般ユーザーの権限に書き換えます。

例えば一般ユーザー名が hoge でグループ名が staff であれば以下のように指定します。

% sudo chown -R hoge:staff /usr/local/lib/pkgconfig

もう一度 ls -l で書き変わっているか確認しましょう。

% ls -l /usr/local/lib/pkgconfig                        
total 8
lrwxr-xr-x  1 hoge  staff   10  2  1 14:40 fuse.pc -> osxfuse.pc
-rw-r--r--  1 hoge  staff  258 12  5  2019 osxfuse.pc

再度 brew install を実行してインストールします。

% brew install rbenv ruby-build

rbenv versions で現在システムの ruby を指していることを確認します。

% rbenv versions
* system (set by /Users/satokichi/.rbenv/version)

rbenv install -l でインストールできる ruby のバージョンを確認できます。

% rbenv install -l
2.6.7
2.7.3
3.0.1
jruby-9.2.17.0
mruby-3.0.0
rbx-5.0
truffleruby-21.1.0
truffleruby+graalvm-21.1.0

Only latest stable releases for each Ruby implementation are shown.
Use 'rbenv install --list-all / -L' to show all local versions.

今回は最新の 3.0.1 をインストールしました。

% rbenv install 3.0.1
Downloading openssl-1.1.1k.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/892a0875b9872acd04a9fde79b1f943075d5ea162415de3047c327df33fbaee5
Installing openssl-1.1.1k...
Installed openssl-1.1.1k to /Users/satokichi/.rbenv/versions/3.0.1

Downloading ruby-3.0.1.tar.gz...
-> https://cache.ruby-lang.org/pub/ruby/3.0/ruby-3.0.1.tar.gz
Installing ruby-3.0.1...
ruby-build: using readline from homebrew
Installed ruby-3.0.1 to /Users/satokichi/.rbenv/versions/3.0.1

rbenv global 3.0.1 とすると rbenv の参照先の ruby が先ほどインストールした 3.0.1 になります。

なおディレクトリ(アプリなど)ごとに ruby のバージョン指定を変えるときは rbenv local で使い分けることができるそうです。

% rbenv global 3.0.1
% rbenv versions                   
  system
* 3.0.1 (set by /Users/satokichi/.rbenv/version)

続いて、gem コマンド及び ruby コマンドのパスを変更するため .bash_profile(or .bashrc) にパスの情報を追記します。

% vim ~/.bash_profile 

[[ -d ~/.rbenv  ]] && \
  export PATH=${HOME}/.rbenv/bin:${PATH} && \
  eval "$(rbenv init -)"

source コマンドで .bash_profile(or .bashrc)を変更を反映させます。

尚、再起動すると無効になってしまうみたいなので再度 source コマンドをやる必要があります。

source ~/.bash_profile

which で gem 及び ruby のパスが変わっていることを確認しましょう。

% which gem                                                            
/Users/satokichi/.rbenv/shims/gem
% which ruby                                                           
/Users/satokichi/.rbenv/shims/ruby

最初に戻り、gem install xcpretty が成功しました!

% gem install xcpretty
Fetching xcpretty-0.3.0.gem
Fetching rouge-2.0.7.gem
Successfully installed rouge-2.0.7
Successfully installed xcpretty-0.3.0
Parsing documentation for rouge-2.0.7
Installing ri documentation for rouge-2.0.7
Parsing documentation for xcpretty-0.3.0
Installing ri documentation for xcpretty-0.3.0
Done installing documentation for rouge, xcpretty after 2 seconds
2 gems installed

Mac 買い直したりした時の自分のための記事ですが、誰かの役にも立てたら幸いです。

以上

1件のコメント

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です