pyenvを使ってインストールする際のPermissionError

pyenv install をした際に以下の様なエラーが出ました。

エラー内容

homebrewで入れたpythonが入っている時

$ pyenv install 3.6.4
WARNING: Please make sure you remove any previous custom paths from your /Users/username/.pydistutils.cfg file.
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.6.4.tar.xz...
-> https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
Installing Python-3.6.4...
python-build: use readline from homebrew

BUILD FAILED (OS X 10.12.6 using python-build 1.2.2)

Inspect or clean up the working tree at /var/folders/12/hr21gzw96h1cw0bwdq2fm80c0000gp/T/python-build.20180311220539.4803
Results logged to /var/folders/12/hr21gzw96h1cw0bwdq2fm80c0000gp/T/python-build.20180311220539.4803.log

Last 10 log lines:
  File "/var/folders/12/hr21gzw96h1cw0bwdq2fm80c0000gp/T/tmpbkgjxirq/pip-9.0.1-py2.py3-none-any.whl/pip/wheel.py", line 287, in clobber
    ensure_dir(dest)  # common for the 'include' path
  File "/var/folders/12/hr21gzw96h1cw0bwdq2fm80c0000gp/T/tmpbkgjxirq/pip-9.0.1-py2.py3-none-any.whl/pip/utils/__init__.py", line 83, in ensure_dir
    os.makedirs(path)
  File "/private/var/folders/12/hr21gzw96h1cw0bwdq2fm80c0000gp/T/python-build.20180311220539.4803/Python-3.6.4/Lib/os.py", line 210, in makedirs
    makedirs(head, mode, exist_ok)
  File "/private/var/folders/12/hr21gzw96h1cw0bwdq2fm80c0000gp/T/python-build.20180311220539.4803/Python-3.6.4/Lib/os.py", line 220, in makedirs
    mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/lib/python3.6'
make: *** [install] Error 2

homebrewで入れたpythonを削除後

$ pyenv install 3.6.4
WARNING: Please make sure you remove any previous custom paths from your /Users/username/.pydistutils.cfg file.
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.6.4.tar.xz...
-> https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
Installing Python-3.6.4...
python-build: use readline from homebrew

BUILD FAILED (OS X 10.12.6 using python-build 1.2.2)

Inspect or clean up the working tree at /var/folders/12/hr21gzw96h1cw0bwdq2fm80c0000gp/T/python-build.20180311230122.32973
Results logged to /var/folders/12/hr21gzw96h1cw0bwdq2fm80c0000gp/T/python-build.20180311230122.32973.log

Last 10 log lines:
  File "/var/folders/12/hr21gzw96h1cw0bwdq2fm80c0000gp/T/tmptf03qo4n/pip-9.0.1-py2.py3-none-any.whl/pip/_vendor/distlib/scripts.py", line 372, in make
    self._make_script(entry, filenames, options=options)
  File "/var/folders/12/hr21gzw96h1cw0bwdq2fm80c0000gp/T/tmptf03qo4n/pip-9.0.1-py2.py3-none-any.whl/pip/_vendor/distlib/scripts.py", line 276, in _make_script
    self._write_script(scriptnames, shebang, script, filenames, ext)
  File "/var/folders/12/hr21gzw96h1cw0bwdq2fm80c0000gp/T/tmptf03qo4n/pip-9.0.1-py2.py3-none-any.whl/pip/_vendor/distlib/scripts.py", line 250, in _write_script
    self._fileop.write_binary_file(outname, script_bytes)
  File "/var/folders/12/hr21gzw96h1cw0bwdq2fm80c0000gp/T/tmptf03qo4n/pip-9.0.1-py2.py3-none-any.whl/pip/_vendor/distlib/util.py", line 407, in write_binary_file
    with open(path, 'wb') as f:
PermissionError: [Errno 1] Operation not permitted: '/bin/easy_install-3.6'
make: *** [install] Error 2

解決方法

エラーにも書いてあったのですが、 ~/.pydistutils.cfg

[install]
prefix=

と記入してあったので消したら無事インストールできる様になりました。

PREFIXが空欄になって /bin などにインストールしようとしていたのかと思います(おそらく ここ あたり??)

参照

https://stackoverflow.com/questions/27465413/python-pip-tries-to-install-to-bin-directory