MacOSに上げてからpyenvでインストールできないときにやったこと


## tl; dr

- `ctrutil disabled`で/bin/pipを実行できるようにした
- `zlib`をインストールした
- `LDFLAGS=-L/usr/local/opt/zlib/lib CPPFLAGS=-I/usr/local/opt/zlib/include PKG_CONFIG_PATH=/usr/local/opt/zlib/lib/pkgconfig pyenv install 3.5.1`でインストール

## 発生した課題

- `pip install --upgrade pip`が使えない
- pyenvでpython3.5.1をインストールできない

## やってみたこと

`python -m pip install --upgrade --force-reinstall pip`
で強制アップデートしてみようとしたが、

```bash
Collecting pip
  Using cached pip-9.0.0-py2.py3-none-any.whl
Installing collected packages: pip
  Found existing installation: pip 7.1.2
    Uninstalling pip-7.1.2:
Exception:
Traceback (most recent call last):
  File "/Users/kotamat/.anyenv/envs/pyenv/versions/3.5.1/lib/python3.5/shutil.py", line 538, in move
    os.rename(src, real_dst)
PermissionError: [Errno 13] Permission denied: '/Users/kotamat/.anyenv/envs/pyenv/versions/3.5.1/lib/python3.5/site-packages/pip-7.1.2.dist-info/DESCRIPTION.rst' -> '/var/folders/qf/q28hqmj54hnb054fj97rwcl40000gn/T/pip-3wy6zsy1-uninstall/Users/kotamat/.anyenv/envs/pyenv/versions/3.5.1/lib/python3.5/site-packages/pip-7.1.2.dist-info/DESCRIPTION.rst'
```

とでてしまった。`sudo`とやってしまっても`/bin/pip`への権限が無いと怒られる始末。

これはmacのSIPのせいなので、下記を参考にdisabledに。
http://berukann.hatenablog.jp/entry/2015/12/30/123020

再起動して実行してみたところ、

```bash
Collecting pip
  Downloading pip-9.0.0-py2.py3-none-any.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 278kB/s
Installing collected packages: pip
  Found existing installation: pip 7.1.2
    Uninstalling pip-7.1.2:
      Successfully uninstalled pip-7.1.2
Successfully installed pip
```
とでているのに

```bash
$ pip install percol
pyenv: pip: command not found
```

pipはインストールされておらず。。

仕方ないのでpyenvでpythonを再インストールしてみようと試みるが、

```bash

Last 10 log lines:
  File "/private/var/folders/qf/q28hqmj54hnb054fj97rwcl40000gn/T/python-build.20161105190035.4540/Python-3.5.1/Lib/ensurepip/__main__.py", line 4, in <module>
    ensurepip._main()
  File "/private/var/folders/qf/q28hqmj54hnb054fj97rwcl40000gn/T/python-build.20161105190035.4540/Python-3.5.1/Lib/ensurepip/__init__.py", line 209, in _main
    default_pip=args.default_pip,
  File "/private/var/folders/qf/q28hqmj54hnb054fj97rwcl40000gn/T/python-build.20161105190035.4540/Python-3.5.1/Lib/ensurepip/__init__.py", line 116, in bootstrap
    _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
  File "/private/var/folders/qf/q28hqmj54hnb054fj97rwcl40000gn/T/python-build.20161105190035.4540/Python-3.5.1/Lib/ensurepip/__init__.py", line 40, in _run_pip
    import pip
zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] Error 1
```

と出てしまった。

zlibを入れてみると

```bash
$ brew update zlib

....

This formula was found in a tap:
homebrew/dupes/zlib
To install it, run:
  brew install homebrew/dupes/zlib
```

とのことなのでコマンド変えて再実行

```bash
$ brew install homebrew/dupes/zlib

...

This formula is keg-only, which means it was not symlinked into /usr/local.

macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/zlib/lib
    CPPFLAGS: -I/usr/local/opt/zlib/include
    PKG_CONFIG_PATH: /usr/local/opt/zlib/lib/pkgconfig
```

何やらあやしいパラメータが。。

これを指定してみる

```bash
$ LDFLAGS=-L/usr/local/opt/zlib/lib CPPFLAGS=-I/usr/local/opt/zlib/include PKG_CONFIG_PATH=/usr/local/opt/zlib/lib/pkgconfig pyenv install 3.5.1

Downloading Python-3.5.1.tgz...
-> https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz
Installing Python-3.5.1...
WARNING: The Python sqlite3 extension was not compiled. Missing the SQLite3 lib?
Installed Python-3.5.1 to /Users/kotamat/.anyenv/envs/pyenv/versions/3.5.1
```

お、インストール出来ている。

```bash
$ pip -V
pip 7.1.2 from /Users/kotamat/.anyenv/envs/pyenv/versions/3.5.1/lib/python3.5/site-packages (python 3.5)
```

pipも無事インストールできた🙌