在 M1 Mac 上安装 R 包 "nloptr" 出现问题。

5

我试图在R和RStudio中安装“lme4”库,但在我使用M1 Mac之前可以正常工作的情况下,现在无法安装。出现问题的依赖项是:“nloptr”。这是我的当前错误:

clang++ -arch arm64 -std=gnu++11 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/opt/R/arm64/lib -o nloptr.so init_nloptr.o nloptr.o test-C-API.o test-runner.o -L/Library/Frameworks/R.framework/Resources/lib -lRlapack -L/Library/Frameworks/R.framework/Resources/lib -lRblas -L/opt/R/arm64/gfortran/lib/gcc/aarch64-apple-darwin20.2.0/11.0.0 -L/opt/R/arm64/gfortran/lib -lgfortran -lemutls_w -lm -Lnlopt/lib -lnlopt -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
ld: warning: directory not found for option '-Lnlopt/lib'
ld: library not found for -lnlopt
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [nloptr.so] Error 1
ERROR: compilation failed for package ‘nloptr’
* removing ‘/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/nloptr’
Warning in install.packages :
  installation of package ‘nloptr’ had non-zero exit status

The downloaded source packages are in
    ‘/private/var/folders/ht/y6qd6yfn67x086jtwxvh42tw0000gn/T/RtmpULtpZq/downloaded_packages’ ```

我使用搭载 Monterey (12.1) 的 M1 Mac,并安装了 arm64 版本的 R。以下是我的当前版本 R 4.1.2:
> version
               _                           
platform       aarch64-apple-darwin20      
arch           aarch64                     
os             darwin20                    
system         aarch64, darwin20           
status                                     
major          4                           
minor          1.2                         
year           2021                        
month          11                          
day            01                          
svn rev        81115                       
language       R                           
version.string R version 4.1.2 (2021-11-01)
nickname       Bird Hippie    

我已在终端中尝试了以下命令:brew install nloptbrew install gcc
在此之前,我还遇到了以下错误: ld: warning: directory not found for option '-L/opt/R/arm64/gfortran/lib/gcc/aarch64-apple-darwin20.2.0/11.0.0' 通过搜索后,为了解决这个问题,我执行了以下操作: export PATH=$PATH:/opt/R/arm64/gfortran/binln -sfn `xcrun --show-sdk-path` /opt/R/arm64/gfortran/SDK

这可能是一个短暂的问题;nloptr软件包最近在CRAN上进行了更新,采用了新的架构,因此可能会出现不均匀的传播情况。 - Ben Bolker
如果它仍然无法正常工作,可能值得在 https://github.com/astamm/nloptr/issues 上发布一个问题。 - Ben Bolker
2个回答

4
在我的电脑上(运行Big Sur的M1 Mac),我刚刚尝试了install.packages("nloptr"),而没有先运行brew install nlopt
我收到了有关-Lnlopt/lib的相同警告提示。
ld: warning: directory not found for option '-Lnlopt/lib'
ld: library not found for -lnlopt
clang-13: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [nloptr.so] Error 1
ERROR: compilation failed for package ‘nloptr’

但在我看到警告之前,我看到:

checking if pkg-config knows NLopt... no
using NLopt via local cmake build on arm64

------------------ CMAKE NOT FOUND --------------------

CMake was not found on the PATH. Please install CMake:

 - yum install cmake          (Fedora/CentOS; inside a terminal)
 - apt install cmake          (Debian/Ubuntu; inside a terminal).
 - pacman -S cmake            (Arch Linux; inside a terminal).
 - brew install cmake         (MacOS; inside a terminal with Homebrew)
 - port install cmake         (MacOS; inside a terminal with MacPorts)

Alternatively install CMake from: <https://cmake.org/>

-------------------------------------------------------

install.packages("nloptr") 只要我使用brew install cmake安装CMake后就可以成功运行。

关于你最初的问题与gfortran有关,我建议尝试我的指南这里。R推荐安装特定版本的gfortran并配置编译器通过~/.R/Makevars找到该安装。你真的不需要去打乱你的PATH...


在执行了brew install cmake之后,它终于成功了!非常感谢! - Wesley Kerr

1
很不幸,无论是通过brew install cmake安装,还是从源代码安装nloptr,或者配置RStudio使用正确的PATH,都没有帮助到我。

根据文档中标记为仅适用于macOS和Linux的安装CMake的部分,nloptr包安装程序期望在以下路径之一找到cmake可执行文件:

  1. CMAKE_BIN环境变量指定的路径(剧透:设置它并没有起作用),或者
  2. /Applications/CMake.app/Contents/bin/cmake

以下是我最终在zsh shell中解决问题的方法:

brew install cmake
sudo mkdir -p /Applications/cmake.app/Contents/bin
cd /Applications/cmake.app/Contents/bin
sudo ln -s /opt/homebrew/bin/cmake

这种方法的缺点是它会将一个非应用程序放在“/Applications”目录中,但我可以接受。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接