Mojave+pyenvでの環境構築で詰まった話

#はじめに
今まではpyenv+anacondaをベースに色々環境作っていたのですが，webサービスも作ろうとすると競合が起こってしまったり大変だったので一から環境を作り直すことに．

Mac Book Pro
macOS Mojave (10.14.6)
pyenv (1.2.13)

#pyenvで他のバージョン入れよう
pyenvでインストールしよう

```bash
$ pyenv install 3.6.6
```


```bash
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.6.6.tar.xz...
-> https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz
Installing Python-3.6.6...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk

BUILD FAILED (OS X 10.14.6 using python-build 20180424)

Inspect or clean up the working tree at /var/folders/tp/v06qzs21451bc64bp9bsrqdr0000gq/T/python-build.20190816014251.44876
Results logged to /var/folders/tp/v06qzs21451bc64bp9bsrqdr0000gq/T/python-build.20190816014251.44876.log

Last 10 log lines:
clang -c -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include -I/usr/local/opt/openssl/include  -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include -I/usr/local/opt/openssl/include  -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes   -I. -I./Include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/usr/local/opt/openssl/include -I/Users/UserName/.pyenv/versions/3.6.6/include  -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/usr/local/opt/openssl/include -I/Users/UserName/.pyenv/versions/3.6.6/include   -DPy_BUILD_CORE \
	      -DGITVERSION="\"`LC_ALL=C `\"" \
	      -DGITTAG="\"`LC_ALL=C `\"" \
	      -DGITBRANCH="\"`LC_ALL=C `\"" \
	      -o Modules/getbuildinfo.o ./Modules/getbuildinfo.c
rm -f libpython3.6m.a
ar rc libpython3.6m.a Modules/getbuildinfo.o
/opt/local/bin/ranlib: object: libpython3.6m.a(getbuildinfo.o) malformed object (unknown load command 1)
ar: internal ranlib command failed
make: *** [libpython3.6m.a] Error 1
```

え，なんか怒られてる．．．
arってコマンドがうまくファイルを読み込めてないみたい

#調べる

[調べたところ](https://github.com/pyenv/pyenv/issues/1267)macportに入っているものたちが古かったのが原因っぽい
そこでportの中身を見ようとするとplatformが合わず動かせないと怒られる
(darwin 15のものは現在のdarwin 18では動かせません的内容)

#解決する
エラー文に従って[サイト](https://trac.macports.org/wiki/Migration)に飛び，Migrationなるものを実行

###Migration


```bash
port -qv installed > myports.txt
sudo port -f uninstall installed
sudo rm -rf /opt/local/var/macports/build/*
curl --location --remote-name https://github.com/macports/macports-contrib/raw/master/restore_ports/restore_ports.tcl
chmod +x restore_ports.tcl
sudo ./restore_ports.tcl myports.txt
```

無事アップデート完了

ちなみに，この操作でできたmyports.txtを見ると，platformが古いことが確認できる

```bash
  bzip2 @1.0.6_0 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:07:06+0900'
  cctools @895_4+llvm50 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:08:10+0900'
  gcc5 @5.5.0_1 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:10:46+0900'
  gcc_select @0.1_8 (active) platform='darwin 15' archs='noarch' date='2018-06-05T11:08:14+0900'
  gettext @0.19.8.1_0 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:07:17+0900'
  gmp @6.1.2_1 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:09:39+0900'
  isl @0.18_0 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:09:42+0900'
  ld64 @3_0 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:09:50+0900'
  ld64-latest @274.2_2+llvm50 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:09:47+0900'
  libcxx @5.0.1_2+universal (active) platform='darwin 15' archs='i386 x86_64' date='2018-06-05T11:06:50+0900'
  libedit @20170329-3.1_2 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:06:58+0900'
  libffi @3.2.1_0 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:07:00+0900'
  libgcc @7.3.0_1 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:10:04+0900'
  libgcc6 @6.4.0_2 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:10:09+0900'
  libiconv @1.15_0 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:07:08+0900'
  libmpc @1.1.0_1 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:09:58+0900'
  libxml2 @2.9.7_0 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:07:31+0900'
  llvm-5.0 @5.0.2_0 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:07:59+0900'
  llvm_select @2_0 (active) platform='darwin 15' archs='noarch' date='2018-06-05T11:07:04+0900'
  mpfr @4.0.1_0 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:09:54+0900'
  ncurses @6.1_0 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:06:52+0900'
  openssl @1.0.2o_1 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:07:38+0900'
  xar @1.6.1_0 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:07:41+0900'
  xz @5.2.4_0 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:07:26+0900'
  zlib @1.2.11_0 (active) platform='darwin 15' archs='x86_64' date='2018-06-05T11:07:28+0900'

```

###pyenvでpythonダウンロード
ようやくここまできた

```bash
$ pyenv install 3.6.6
```

```bash
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.6.6.tar.xz...
-> https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz
Installing Python-3.6.6...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.6.6 to /Users/UserName/.pyenv/versions/3.6.6
```
無事インストール完了

#終わりに

macportsとか知らない子だったな
homebrewと共存させていいものなのか悩ましい．．．
こまめにアップデートすること大事だなと痛感