GLIBCXX error on RStudio + conda

󰃭 2023-03-07

I usually install R and the packages required for a project through conda and the conda-forge channel and do not use a system-wide R installation through the Linux package manager. However, RStudio is not installed via conda, but using the deb-package file provided by Posit, which means it cannot be run without first activating a conda environment that at least contains the r-base package.

Since some weeks, I frequently encounter this error when loading packages in RStudio:

> library(tidyverse)
Error: package or namespace load failed for ‘tidyverse’:
 .onAttach failed in attachNamespace() for 'tidyverse', details:
  call: NULL
  error: package or namespace load failed for ‘tidyr’ in dyn.load(file, DLLpath = DLLpath, ...):
 unable to load shared object '/home/ptr/software/miniconda3/envs/my-env/lib/R/library/dplyr/libs/dplyr.so':
  /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.29' not found (required by /home/ptr/software/miniconda3/envs/my-env/lib/R/library/dplyr/libs/dplyr.so)

The operating system is Linux Mint 20.3 (Una), which is not the newest distribution one could have in 2023, but it still does its job good enough so far.

Running strings /lib/x86_64-linux-gnu/libstdc++.so.6 | grep GLIBCXX reveals that the highest available version of the C++ standard library in Linux Mint is GLIBCXX_3.4.28, so we are just a bit behind the required GLIBCXX_3.4.29. :-(

However, the conda environment should also include this library in the appropriate version matching the r-base package, it is just not used by RStudio. Funny enough, the error does not occur when just running R on the terminal and trying to load the library?!

A quick fix is to add the lib/ folder of the conda environment to the environment variable LD_LIBRARY_PATH and restart RStudio:

conda activate my-env
export LD_LIBRARY_PATH=$CONDA_PREFIX/lib
rstudio

Now the library can be loaded without problems!