Docker中安装R包systemfonts失败:缺失ft2build.h文件
问题
基于r-base:4.3.2基础镜像构建R应用Docker镜像时,执行renv::restore()安装systemfonts包失败,报错fatal error: ft2build.h: No such file or directory。已尝试安装libfreetype6和libfreetype6-dev,但问题仍存在。systemfonts是renv.lock中tidyverse依赖树的一部分,请问还需安装哪些系统依赖才能解决该问题并成功构建systemfonts包?
Dockerfile内容
# Use r-base as the base image FROM r-base:4.3.2 RUN apt-get update && apt-get install -y \ libcurl4-openssl-dev \ libssl-dev \ libxml2-dev \ libfreetype6 \ libfreetype6-dev \ libharfbuzz-dev \ libfribidi-dev \ libpng-dev \ libtiff5-dev \ libtiff-dev \ libjpeg-dev \ libfontconfig1-dev \ pandoc \ curl \ unzip \ fonts-liberation \ fontconfig \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* # Set the working directory WORKDIR /usr/src/app # Install essential R packages RUN R -e "install.packages(c('renv'), repos = 'https://cloud.r-project.org')" # Set up renv RUN mkdir -p renv/cache ENV RENV_PATHS_CACHE=/usr/src/app/renv/cache # Copy renv files COPY renv.lock . COPY .Rprofile . COPY renv/activate.R renv/ COPY renv/settings.dcf renv/ # Restore the R environment RUN R -e "renv::restore()" COPY . . # Download and install AWS CLI RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" \ && unzip awscliv2.zip \ && ./aws/install \ && rm -rf awscliv2.zip aws RUN aws --version CMD ["Rscript", "create-dashboard-ecs.R"]
报错详情
1432.0 - Installing systemfonts ... FAILED 1432.3 Error: Error installing package 'systemfonts': 1432.3 ======================================= 1432.3 1432.3 * installing *source* package 'systemfonts' ... 1432.3 ** package 'systemfonts' successfully unpacked and MD5 sums checked 1432.3 ** using staged installation 1432.3 Found pkg-config cflags and libs! 1432.3 Using PKG_CFLAGS= 1432.3 Using PKG_LIBS=-lfontconfig -lfreetype 1432.3 ** libs 1432.3 using C++ compiler: 'g++ (Debian 13.2.0-13) 13.2.0' 1432.3 rm -f systemfonts.so caches.o cpp11.o dev_metrics.o font_matching.o font_registry.o ft_cache.o string_shape.o font_metrics.o font_fallback.o string_metrics.o emoji.o cache_store.o init.o unix/FontManagerLinux.o 1432.3 g++ -std=gnu++17 -I"/usr/share/R/include" -DNDEBUG -I'/usr/src/app/renv/staging/1/cpp11/include' -fPIC -g -O2 -ffile-prefix-map=/build/reproducible-path/r-base-4.3.2=. -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -mbranch-protection=standard -Wdate-time -D_FORTIFY_SOURCE=2 -c caches.cpp -o caches.o 1432.3 In file included from caches.h:7, 1432.3 from caches.cpp:1: 1432.3 ft_cache.h:9:10: fatal error: ft2build.h: No such file or directory 1432.3 9 | #include <ft2build.h> 1432.3 | ^~~~~~~~~~~~ 1432.3 compilation terminated. 1432.3 make: *** [/usr/lib/R/etc/Makeconf:200: caches.o] Error 1 1432.3 ERROR: compilation failed for package 'systemfonts' 1432.3 * removing '/usr/src/app/renv/staging/1/systemfonts' 1432.3 install of package 'systemfonts' failed [error code 1] 1432.3 Traceback (most recent calls last): 1432.3 13: renv::restore() 1432.3 12: renv_restore_run_actions(project, diff, current, lockfile, rebuild) 1432.3 11: renv_install_impl(records) 1432.3 10: renv_install_staged(records) 1432.3 9: renv_install_default(records) 1432.3 8: handler(package, renv_install_package(record)) 1432.3 7: renv_install_package(record) 1432.3 6: withCallingHandlers(renv_install_package_impl(record), error = function(e) writef("FAILED")) 1432.3 5: renv_install_package_impl(record) 1432.3 4: r_cmd_install(package, path) 1432.3 3: r_exec_error(package, output, "install", status) 1432.3 2: abort(all) 1432.3 1: stop(fallback) 1432.3 Execution halted
解决方案
核心原因
从报错日志的Using PKG_CFLAGS=为空可以看出,编译脚本虽检测到pkg-config,但无法获取freetype的编译参数,导致编译器找不到ft2build.h头文件。问题根源是缺少pkg-config工具,它负责管理系统库的编译路径和参数。
需要添加的系统依赖
在apt-get install的依赖列表中加入pkg-config即可解决问题。
修改后的Dockerfile(关键部分)
RUN apt-get update && apt-get install -y \ libcurl4-openssl-dev \ libssl-dev \ libxml2-dev \ libfreetype6 \ libfreetype6-dev \ libharfbuzz-dev \ libfribidi-dev \ libpng-dev \ libtiff5-dev \ libtiff-dev \ libjpeg-dev \ libfontconfig1-dev \ pandoc \ curl \ unzip \ fonts-liberation \ fontconfig \ pkg-config \ # 添加这一行 && apt-get clean \ && rm -rf /var/lib/apt/lists/*
额外验证(可选)
如果添加pkg-config后仍有问题,可手动设置环境变量指定freetype的配置路径,在WORKDIR之后添加:
ENV PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig
不过一般情况下,安装pkg-config后会自动识别路径,无需额外配置。
内容的提问来源于stack exchange,提问作者Vivek Katial
相关产品推荐
相关产品推荐

