You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 22.04下安装R包testthat失败求助

安装testthat包时编译错误导致无法安装devtools

尝试在R中安装devtools包,但因testthat包安装失败受阻。单独执行install.packages("testthat")时出现编译错误,具体错误信息如下:

> install.packages("testthat")
Installing package into ‘/home/alejo/R/x86_64-pc-linux-gnu-library/4.0’
(as ‘lib’ is unspecified)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  243k  100  243k    0     0   192k      0  0:00:01  0:00:01 --:--:--  192k
Microsoft R Open 4.0.2
The enhanced R distribution from Microsoft
Microsoft packages Copyright (C) 2020 Microsoft Corporation

Using the Intel MKL for parallel mathematical computing (using 6 cores).

Default CRAN mirror snapshot taken on 2020-07-16.
See: https://mran.microsoft.com/.

* installing *source* package ‘testthat’ ...
** package ‘testthat’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
gcc -std=gnu99 -I/opt/microsoft/ropen/4.0.2/lib64/R/include -DNDEBUG -I../inst/include     -DCOMPILING_TESTTHAT  -DU_STATIC_IMPLEMENTATION   -fpic  -DU_STATIC_IMPLEMENTATION -O2 -g  -c init.c -o init.o
gcc -std=gnu99 -I/opt/microsoft/ropen/4.0.2/lib64/R/include -DNDEBUG -I../inst/include -DCOMPILING_TESTTHAT  -DU_STATIC_IMPLEMENTATION   -fpic  -DU_STATIC_IMPLEMENTATION -O2 -g  -c reassign.c -o reassign.o
g++  -I/opt/microsoft/ropen/4.0.2/lib64/R/include -DNDEBUG -I../inst/include -DCOMPILING_TESTTHAT  -DU_STATIC_IMPLEMENTATION   -fpic  -DU_STATIC_IMPLEMENTATION -O2 -g  -c test-catch.cpp -o test-catch.o
g++  -I/opt/microsoft/ropen/4.0.2/lib64/R/include -DNDEBUG -I../inst/include -DCOMPILING_TESTTHAT  -DU_STATIC_IMPLEMENTATION   -fpic  -DU_STATIC_IMPLEMENTATION -O2 -g  -c test-example.cpp -o test-example.o
g++  -I/opt/microsoft/ropen/4.0.2/lib64/R/include -DNDEBUG -I../inst/include -DCOMPILING_TESTTHAT  -DU_STATIC_IMPLEMENTATION   -fpic  -DU_STATIC_IMPLEMENTATION -O2 -g  -c test-runner.cpp -o test-runner.o
In file included from /usr/include/signal.h:328,
                 from ../inst/include/testthat/vendor/catch.h:6467,
                 from ../inst/include/testthat/testthat.h:65,
                 from ../inst/include/testthat.h:1,
                 from test-runner.cpp:7:
../inst/include/testthat/vendor/catch.h:6490:33: error: size of array ‘altStackMem’ is not an integral constant-expression
 6490 |         static char altStackMem[SIGSTKSZ];
      |                                 ^~~~~~~~
../inst/include/testthat/vendor/catch.h:6541:45: error: size of array ‘altStackMem’ is not an integral constant-expression
 6541 |     char FatalConditionHandler::altStackMem[SIGSTKSZ] = {};
      |                                             ^~~~~~~~
make: *** [/opt/microsoft/ropen/4.0.2/lib64/R/etc/Makeconf:168: test-runner.o] Error 1
ERROR: compilation failed for package ‘testthat’
* removing ‘/home/alejo/R/x86_64-pc-linux-gnu-library/4.0/testthat’
Warning in install.packages :
  installation of package ‘testthat’ had non-zero exit status

已试过通过Anaconda源安装、查看testthat的GitHub相关问题,但因为没有devtools,无法用GitHub安装方式。本人对Linux和R操作不太熟悉,寻求帮助。


解决方法

这个错误源于SIGSTKSZ在当前系统中不是编译期常量,属于testthat旧版本与系统编译器的兼容性问题,可尝试以下方案:

  • 安装预编译二进制包
    多数Linux发行版提供R包的预编译版本,以Debian/Ubuntu为例,直接用系统包管理器安装:

    sudo apt-get install r-cran-testthat
    

    跳过源码编译,直接获取可用版本。

  • 安装兼容的旧版testthat
    该问题在testthat 3.0.0版本中已修复,无需devtools,直接从CRAN归档安装:

    install.packages("https://cran.r-project.org/src/contrib/Archive/testthat/testthat_3.0.0.tar.gz", repos = NULL, type = "source")
    
  • 升级系统编译器
    若gcc/g++版本过旧,升级到9及以上版本可解决编译常量问题,以Ubuntu为例:

    sudo apt-get update
    sudo apt-get install gcc-9 g++-9
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 90
    

    完成后重新运行install.packages("testthat")。

  • 用conda创建完整R环境
    借助Anaconda直接创建包含devtools的环境,自动处理所有依赖:

    conda create -n r-dev r-base r-devtools
    conda activate r-dev
    

内容的提问来源于stack exchange,提问作者Another.Chemist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 12:45:50