本来使用conda安装R就是为了节约时间,不浪费解决依赖上,但是没想到还是有坑。
首先我创建一个R=4.4.2的环境
conda create -c conda-forge -n R442 r-base=4.4.2
conda activate R442
然后我尝试安装 tidyverse,这就是噩梦的来源,因为tidyverse是一堆包的集合,尤其是其中的httr,如果你的系统是刚安装的,为了装这个包,你得装不少的依赖。
also installing the dependencies ‘fastmap’, ‘lattice’, ‘colorspace’, ‘sys’, ‘bit’, ‘ps’, ‘base64enc’, ‘sass’, ‘digest’, ‘cachem’, ‘nlme’, ‘Matrix’, ‘farver’, ‘labeling’, ‘munsell’, ‘RColorBrewer’, ‘viridisLite’, ‘rappdirs’, ‘rematch’, ‘askpass’, ‘bit64’, ‘prettyunits’, ‘processx’, ‘evaluate’, ‘highr’, ‘xfun’, ‘yaml’, ‘bslib’, ‘fontawesome’, ‘htmltools’, ‘jquerylib’, ‘tinytex’, ‘backports’, ‘generics’, ‘glue’, ‘lifecycle’, ‘memoise’, ‘blob’, ‘DBI’, ‘R6’, ‘tidyselect’, ‘vctrs’, ‘withr’, ‘data.table’, ‘gtable’, ‘isoband’, ‘MASS’, ‘mgcv’, ‘scales’, ‘gargle’, ‘uuid’, ‘cellranger’, ‘curl’, ‘ids’, ‘rematch2’, ‘cpp11’, ‘pkgconfig’, ‘mime’, ‘openssl’, ‘timechange’, ‘utf8’, ‘systemfonts’, ‘textshaping’, ‘clipr’, ‘crayon’, ‘vroom’, ‘tzdb’, ‘progress’, ‘callr’, ‘fs’, ‘knitr’, ‘rmarkdown’, ‘selectr’, ‘stringi’, ‘fansi’, ‘broom’, ‘conflicted’, ‘cli’, ‘dbplyr’, ‘dplyr’, ‘dtplyr’, ‘forcats’, ‘ggplot2’, ‘googledrive’, ‘googlesheets4’, ‘haven’, ‘hms’, ‘httr’, ‘jsonlite’, ‘lubridate’, ‘magrittr’, ‘modelr’, ‘pillar’, ‘purrr’, ‘ragg’, ‘readr’, ‘readxl’, ‘reprex’, ‘rlang’, ‘rstudioapi’, ‘rvest’, ‘stringr’, ‘tibble’, ‘tidyr’, ‘xml2
安装的时候会出现如下的报错
install.packages("tidyverse")
# 错误开始
--------------------------------------------------------------------
ERROR: configuration failed for package ‘ragg’
* removing ‘/home/stark09/miniforge3/envs/R442/lib/R/library/ragg’
* installing *source* package ‘haven’ ...
** package ‘haven’ successfully unpacked and MD5 sums checked
** using staged installation
Found pkg-config cflags and libs!
Using PKG_CFLAGS=
Using PKG_LIBS=-lz
--------------------------- [ANTICONF] --------------------------------
Configuration failed to find the zlib library. Try installing:
* deb: zlib1g-dev (Debian, Ubuntu, etc)
* rpm: zlib-devel (Fedora, EPEL)
* csw: libz_dev (Solaris)
* brew: zlib (OSX)
If zlib is already installed, check that 'pkg-config' is in your
PATH and PKG_CONFIG_PATH contains a zlib.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
-------------------------- [ERROR MESSAGE] ---------------------------
<stdin>:1:10: fatal error: zlib.h: No such file or directory
compilation terminated.
--------------------------------------------------------------------
ERROR: configuration failed for package ‘haven’
* removing ‘/home/stark09/miniforge3/envs/R442/lib/R/library/haven’
The downloaded source packages are in
‘/tmp/RtmpbP83Ac/downloaded_packages’
Updating HTML index of packages in '.Library'
Making 'packages.html' ... done
Warning message:
In install.packages("tidyverse") : installation of 12 packages failed:
‘data.table’, ‘curl’, ‘xml2’, ‘httr’, ‘gargle’, ‘ragg’, ‘googledrive’, ‘rvest’, ‘dtplyr’, ‘googlesheets4’, ‘haven’, ‘tidyverse’
他会提示你的zlib不在,于是我们用conda去安装zlib
conda install -c conda-forge zlib
之后继续安装 tidyverse,
Found pkg-config cflags and libs!
Using PKG_CFLAGS=-I/usr/include/x86_64-linux-gnu
Using PKG_LIBS=-lcurl
--------------------------- [ANTICONF] --------------------------------
Configuration failed because libcurl was not found. Try installing:
* deb: libcurl4-openssl-dev (Debian, Ubuntu, etc)
* rpm: libcurl-devel (Fedora, CentOS, RHEL)
If libcurl is already installed, check that 'pkg-config' is in your
PATH and PKG_CONFIG_PATH contains a libcurl.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
又出现很多错误,我们先解决其中的curl报错 。我们会发现,用 conda install -c conda-forge libcurl
会提示已经安装了。同时搜索的时候也能看到libcurl。
$ ls /home/stark09/miniforge3/envs/R442/lib | grep libcurl
libcurl.so
libcurl.so.4
libcurl.so.4.8.0
仔细观察,可以发现,运行提示的Using PKG_CFLAGS=-I/usr/include/x86_64-linux-gnu,用的不是我们conda的依赖库。进一步发现,用 which pkg-config
发现是/usr/bin/pkg-config
,因此,我们还需要专门安装pkg-config
conda install -c conda-forge pkg-config
然后用 install.packages('curl')
就可以了。继续安装tidyverse,会遇到liblzma找不到
* installing *source* package ‘ragg’ ...
** package ‘ragg’ successfully unpacked and MD5 sums checked
** using staged installation
Package liblzma was not found in the pkg-config search path.
Perhaps you should add the directory containing `liblzma.pc'
to the PKG_CONFIG_PATH environment variable
Package 'liblzma', required by 'libtiff-4', not found
Found pkg-config cflags and libs!
Using PKG_CFLAGS=
Using PKG_LIBS=-L/home/stark09/miniforge3/envs/R442/lib -lfreetype -lpng16 -ltiff -ljpeg
-----------------------------[ ANTICONF ]-------------------------------
Configuration failed to find one of freetype2 libpng libtiff-4 libjpeg. Try installing:
* deb: libfreetype6-dev libpng-dev libtiff5-dev libjpeg-dev (Debian, Ubuntu, etc)
* rpm: freetype-devel libpng-devel libtiff-devel libjpeg-devel (Fedora, CentOS, RHEL)
* csw: libfreetype_dev libpng16_dev libtiff_dev libjpeg_dev (Solaris)
If freetype2 libpng libtiff-4 libjpeg is already installed, check that 'pkg-config' is in your
PATH and PKG_CONFIG_PATH contains a freetype2 libpng libtiff-4 libjpeg.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
但实际上,liblzma有了呀
ls /home/stark09/miniforge3/envs/R442/lib | grep liblzma
liblzma.so.5
liblzma.so.5.6.4
继续仔细观察提示,发现他要求有一个liblzma.pc
Package liblzma was not found in the pkg-config search path.
Perhaps you should add the directory containing `liblzma.pc'
to the PKG_CONFIG_PATH environment variable
这里就是一个知识点,区分运行时包和开发是包。运行时包包括
- 共享库文件(
.so
文件) - 运行程序所需的基本组件
而pkg-config
找不到它是因为pkg-config
需要的是开发相关文件,也就是
- 头文件(
.h
文件) - pkg-config配置文件(
.pc
文件) - 用于编译的静态库(
.a
文件) - 符号链接和其他编译所需资源
不同的R包要求不同,有的只需要运行时包,有的需要开发时包。
通过用conda进行搜索,找到liblzma-devel这个库
conda search -c conda-forge '*liblzma*
# liblzma-static 5.6.4 hbcc6ac9_0 conda-forge
conda install -c conda-forge liblzma-devel
然后ragg这个包就行了。继续安装tidyverse,遇到了xml2这个包的报错
/home/stark09/miniforge3/envs/R442/bin/../lib/gcc/x86_64-conda-linux-gnu/14.2.0/../../../../x86_64-conda-linux-gnu/bin/ld: cannot find -lxml2: No such file or directory
collect2: error: ld returned 1 exit status
这个就简单多了,我就猜测试libxml2没有,安装libxml2解决问题了。
conda install -c conda-forge libxml2
最后,经过一系列磨难后,tidyverse也算是成功安装了
总结一下,为了安装tidyverse,你还需要安装如下4个环境。
conda install -c conda-forge pkg-config libxml2 zlib liblzma-devel