「R」をbrewでOSX 10.9 Mavericksにインストールしても動かない時のTips


次のエラーでRが動かない時の処方箋を紹介します。

$ R
dyld: Library not loaded: /usr/local/lib/gcc/x86_64-apple-darwin13.4.0/4.9.1/libgfortran.3.dylib
  Referenced from: /usr/local/Cellar/r/3.1.2/R.framework/Versions/3.1/Resources/lib/libR.dylib
  Reason: image not found
Trace/BPT trap: 5

原因調査

Rを動かすにはgfortranが必要で、それはgccパッケージにバンドルされているそうです。
そこでgccをインストールするのですが、そこにポイントがあります。

brew install gccを行っても、そのままではそれらは読み込まれていません。
次の通りデフォルトのgccのみとなっています。

$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix

$ which gcc
/usr/bin/gcc

次のパスにある、インストールしたパッケージが参照されていません。

$ brew list gcc
/usr/local/Cellar/gcc/4.9.2_1/bin/c++-4.9
/usr/local/Cellar/gcc/4.9.2_1/bin/cpp-4.9
/usr/local/Cellar/gcc/4.9.2_1/bin/g++-4.9
/usr/local/Cellar/gcc/4.9.2_1/bin/gcc-4.9
/usr/local/Cellar/gcc/4.9.2_1/bin/gcc-ar-4.9
/usr/local/Cellar/gcc/4.9.2_1/bin/gcc-nm-4.9
/usr/local/Cellar/gcc/4.9.2_1/bin/gcc-ranlib-4.9
/usr/local/Cellar/gcc/4.9.2_1/bin/gcov-4.9
/usr/local/Cellar/gcc/4.9.2_1/bin/gfortran
/usr/local/Cellar/gcc/4.9.2_1/bin/gfortran-4.9
/usr/local/Cellar/gcc/4.9.2_1/bin/x86_64-apple-darwin13.4.0-c++-4.9
/usr/local/Cellar/gcc/4.9.2_1/bin/x86_64-apple-darwin13.4.0-g++-4.9
/usr/local/Cellar/gcc/4.9.2_1/bin/x86_64-apple-darwin13.4.0-gcc-4.9
/usr/local/Cellar/gcc/4.9.2_1/bin/x86_64-apple-darwin13.4.0-gcc-4.9.2
/usr/local/Cellar/gcc/4.9.2_1/bin/x86_64-apple-darwin13.4.0-gcc-ar-4.9
/usr/local/Cellar/gcc/4.9.2_1/bin/x86_64-apple-darwin13.4.0-gcc-nm-4.9
/usr/local/Cellar/gcc/4.9.2_1/bin/x86_64-apple-darwin13.4.0-gcc-ranlib-4.9
/usr/local/Cellar/gcc/4.9.2_1/bin/x86_64-apple-darwin13.4.0-gfortran-4.9
/usr/local/Cellar/gcc/4.9.2_1/include/c++/ (706 files)
/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/ (428 files)
/usr/local/Cellar/gcc/4.9.2_1/libexec/gcc/ (13 files)
/usr/local/Cellar/gcc/4.9.2_1/share/gcc-4.9.2/ (3 files)
/usr/local/Cellar/gcc/4.9.2_1/share/man/ (8 files)

解決方法

brew install gccでインストールしたgccなどが参照されるよう、シンボリックリンクを作成します。

$ ln -s /usr/local/bin/gcc-4.9 /usr/local/bin/gcc
$ ln -s /usr/local/bin/g++-4.9 /usr/local/bin/g++

パスが通った場所にシンボリックリンクを作成したので、期待通りの挙動となりました。

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/4.9.2_1/libexec/gcc/x86_64-apple-darwin13.4.0/4.9.2/lto-wrapper
Target: x86_64-apple-darwin13.4.0
Configured with: ../configure --build=x86_64-apple-darwin13.4.0 --prefix=/usr/local/Cellar/gcc/4.9.2_1 --libdir=/usr/local/Cellar/gcc/4.9.2_1/lib/gcc/4.9 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-4.9 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-cloog=/usr/local/opt/cloog --with-isl=/usr/local/opt/isl --with-system-zlib --enable-libstdcxx-time=yes --enable-stage1-checking --enable-checking=release --enable-lto --disable-werror --with-pkgversion='Homebrew gcc 4.9.2_1' --with-bugurl=https://github.com/Homebrew/homebrew/issues --enable-plugin --disable-nls --enable-multilib
Thread model: posix
gcc version 4.9.2 (Homebrew gcc 4.9.2_1)

すると、無事に動きました!

$ R

R version 3.1.3 (2015-03-09) -- "Smooth Sidewalk"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin13.4.0 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

>