无法在 Ubuntu Conda 环境中编译 R 包:x86_64-conda-linux-gnu-c++:未找到

Unable to compile R packages in Ubuntu Conda environment: x86_64-conda-linux-gnu-c++: not found

我在 Ubuntu 20.04 LTS OS 上 运行ning Miniconda3 并在 conda 环境中安装了 R-4.0.3。当我尝试通过 R 提示从 CRAN 存储库安装软件包时,我得到

x86_64-conda-linux-gnu-c++: not found

我有 运行 source activate qweqwe 是环境的名称),正如内置 gcc 工具链上的 Anaconda 文档中所建议的那样。我还 运行 source activate root 并使用 conda install gxx_linux-64

安装了编译器工具链

我的 $PATH returns 以下:

/home/sreedta/miniconda3/envs/qwe/bin:/home/sreedta/miniconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

这是我尝试安装名为 bayesm

的软件包时的完整输出
> install.packages("bayesm")
--- Please select a CRAN mirror for use in this session ---
trying URL 'https://cloud.r-project.org/src/contrib/bayesm_3.1-4.tar.gz'
Content type 'application/x-gzip' length 2269364 bytes (2.2 MB)
==================================================
downloaded 2.2 MB

* installing *source* package ‘bayesm’ ...
** package ‘bayesm’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
x86_64-conda-linux-gnu-c++ -std=gnu++11 -I"/home/sreedta/miniconda3/envs/qwe/lib/R/include" -DNDEBUG -I../inst/include/ -I'/home/sreedta/miniconda3/envs/qwe/lib/R/library/Rcpp/include' -I'/home/sreedta/miniconda3/envs/qwe/lib/R/library/RcppArmadillo/include' -DNDEBUG -D_FORTIFY_SOURCE=2 -O2 -isystem /home/sreedta/miniconda3/envs/qwe/include -I/home/sreedta/miniconda3/envs/qwe/include -Wl,-rpath-link,/home/sreedta/miniconda3/envs/qwe/lib   -fpic  -fvisibility-inlines-hidden  -fmessage-length=0 -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -isystem /home/sreedta/miniconda3/envs/qwe/include -fdebug-prefix-map=/home/conda/feedstock_root/build_artifacts/r-base_1603047469992/work=/usr/local/src/conda/r-base-4.0.3 -fdebug-prefix-map=/home/sreedta/miniconda3/envs/qwe=/usr/local/src/conda-prefix  -c RcppExports.cpp -o RcppExports.o
/bin/sh: 1: x86_64-conda-linux-gnu-c++: not found
make: *** [/home/sreedta/miniconda3/envs/qwe/lib/R/etc/Makeconf:180: RcppExports.o] Error 127
ERROR: compilation failed for package ‘bayesm’
* removing ‘/home/sreedta/miniconda3/envs/qwe/lib/R/library/bayesm’
* restoring previous ‘/home/sreedta/miniconda3/envs/qwe/lib/R/library/bayesm’

The downloaded source packages are in
        ‘/tmp/Rtmp6hsphd/downloaded_packages’
Updating HTML index of packages in '.Library'
Making 'packages.html' ... done
Warning message:
In install.packages("bayesm") :
  installation of package ‘bayesm’ had non-zero exit status

如果您在 Conda 环境中安装了 R,我强烈建议避免通过 utils::install.packages 安装。相反,通过 Conda 安装。许多 CRAN 包都可以通过 Conda Forge 渠道获得,通常在包名称前加上“r-”。所以,试试

conda install -n qwe -c conda-forge r-bayesem

感谢@merv,我了解了使用 conda 构建环境的不同方法并成功实现了我的目标。他发布了 link 到他关于使用具有特定编译器和依赖项的 environment.yaml 文件构建的答案之一。我还学到了一些有价值的东西:“最好将源库的频道保持在最低限度。在以前的安装中,我结合了默认值,rpypiconda-forge 频道。在这个成功的set-up中只有conda-forge & pypi.

以下是我在我的 miniconda3 安装中为 R 正确编译所遵循的步骤。

  1. 完全删除了我现有的安装:
(base) $ rm -rf ~/miniconda3
  1. 我还从 /etc/profile 和 ~/.bashrc
  2. 中删除了以下行
export PATH="~/miniconda3/bin:$PATH"

(这是必要的,因为在安装时,我已经对 conda init 说“是”)

  1. 重新安装了 miniconda3(下载后和安装前请确保进行 SHA256 测试以确保文件完整性 - 我的第一次下载已损坏)

  2. 使用下面的 .yaml 创建了一个新环境 asd。我在这个练习中的 objective 是让 R 和 Python 使用 rpy2pybrms.

    相互协作
name: asd
channels:
  - conda-forge   # @merv had mentioned as best source for R related stuff
  - defaults      # only conda-forge has R-base=4.0.3 so defaults are second
  - dependencies: # this is where I added the gcc suite of libraries per @merv
  - python=3.6.11
  - libcurl
  - libv8
  - libgcc-ng  # gcc c
  - libgfortran-ng # gcc fortran
  - libgfortran4   # gcc fortran
  - libglib        # also needed for gcc (I could be wrong)
  - libstdcxx-ng   # gcc c++ / g++
  - conda
  - pip
  - wheel
  - r-base=4.0.3   # default channels only go as high as R-base=3.6.1.
  - pip:
      - rpy2==3.3.6
      - pybrms==0.0.33
  1. 虽然仍在 (base) $ 提示符下,但我使用
  2. 源激活了新环境
(base) $ source activate asd # this changes the prompt to (asd) $ 
  1. 然后我直接从conda-forger-v8r-rcppr-rcpparmadillo
  2. 安装了以下3个R包
(asd) $ conda install -c conda-forge r-v8  # repeat for r-rcpp & r-rcpparmadillo
  1. 在 (asd) $ 提示符下,我使用 (asd) $ R 启动 R 以进入 R 提示符和 运行
install.packages("bayesm")  # this was a package from CRAN that was failing compilation as a source package

这是我最近 3 天进行的测试,目的是测试 conda 环境中的 R 如何访问 built-in gcc 编译器而不是系统编译器

  1. 我在 R 提示符下使用 quit() 退出 R

quit()

  1. 返回 (asd) $ 提示以安装更多 R 包
(asd) $ conda install -c conda-forge boost-cpp # prerequisite for r-bh

(asd) $ conda install -c conda-forge r-bh # prerequisite for r-brms

这将安装一大堆 R 库。

作为 Anaconda、R 和 Python 在新的 OS (Linux-Ubuntu) 上的新用户,这既令人沮丧又令人欣慰。我希望这对另一个新用户有用。

Step1: 在主目录

中找到x86_64-conda-linux-gnu-c++
[showteth@localhost ~] find ~ -name "x86_64-conda-linux-gnu-c++"
/home/showteth/anaconda3/envs/r351/bin/x86_64-conda-linux-gnu-c++

步骤 2:将 /home/showteth/anaconda3/envs/r351/bin 添加到 .Renviron(在您的主目录中)

echo 'PATH=${PATH}:/home/showteth/anaconda3/envs/r351/bin' >> .Renviron

Step3: 重启rstudio服务器使配置生效