Most of the time, if you are installing packages that are available on CRAN, the following command will do the trick.

install.packages(pkgs, type = "binary")

However, if you are installing packages that requires compiling, especially if needs specific compiling versions, and your default compiler is too old (gcc 4.8), and you see errors like lib/libstdc++.so.6: version GLIBCXX_3.4.20 not found. This post can hopefully help you.

Some headlines

  1. Linking libstdc++
  2. Installing your own gcc without sudo and recompile
  3. Static linking gcc
  4. Static linking fortran

Easy fix

Ask system admin for sudo, then do the following

sudo apt-get install libstdc++6

In case this still have not fixed your problem, you may need the following, (find out where your libstdc++.so.6 is, cd there)

sudo mv libstdc++.so.6 libstdc++.so.6.orig
sudo ln -s YOUR_NEW_lib64/libstdc++.so.6.SOME_MINOR_VERSION libstdc++.so.6

Will fix things most of the time. I have run into this problem a few times when having trouble installing stringi.

For reference, see this post.

Install gcc and compile R packages

See this previous post, you may want to use newer gcc such as gcc-11.2.0. Let’s say you have followed the instruction, and installed gcc-11.2.0 at YOUR_PATH. You can modify your Makevars file to configure some of the compiling flags.

Create ~/.R/Makevars with the following content

CC=YOUR_PATH/gcc-11.2.0/bin/gcc -std=gnu99
CXX=YOUR_PATH/gcc-11.2.0/bin/g++
CXX14=YOUR_PATH/gcc-11.2.0/bin/g++

Most likely you will need to recompile Rcpp after this step.

Static linking gcc

By default, R uses the following flags for compiling

CC = gcc -std=gnu99
CFLAGS = -g -O2

You want to add the following lines into your Makevars, replace YOUR_PATH

CXXFLAGS=-O0 -march=native -mtune=native
CXXFLAGS += -fPIC -flto -static-libgcc -static-libstdc++ -LYOUR_PATH/gcc-11.2.0/lib64

In the case you need c++14

Add the following lines

CXX14FLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function -fPIC
CXX14FLAGS+=-flto -Wno-unused-local-typedefs -static-libgcc -static-libstdc++ -LYOUR_PATH/gcc-11.2.0/lib64

Add the following line to .bash_profile

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:YOUR_PATH/gcc-11.2.0/lib64

Static linking fortran

This doesn’t happen often, but when it does,

By default

FLIBS = -lgfortran -lm -lquadmath

You want to add the following lines into your Makevars, replace YOUR_PATH

FLIBS = -static-libgfortran -LYOUR_PATH/gcc-11.2.0/lib64/ -lm -lquadmath