Robert's Data Science Blog

Install R Packages in WSL

In newer versions of Windows the Windows Subsystem for Linux provides a pretty authentic Linux experience.

However, the filesystem can be weird at times. The /tmp folder is never flushed as it usually is after restarts and R sometimes fail to install packages – in particular in the very last step when the package is copied from a temporary folder to its final destination in a library folder.

Consider the complaint I got with install.packages("xml2"):

mv: cannot move ‘/home/robert/R/x86_64-pc-linux-gnu-library/3.6.2/00LOCK-xml2/00new/xml2’ to ‘/home/robert/R/x86_64-pc-linux-gnu-library/3.6.2/xml2’: Permission denied

ERROR: moving to final location failed

The 00LOCK-xml2 folder is created while the package is compiling on Linux and then deleted when the package has been copied to its final destination.

This lock can be avoided with an extra argument to install.packages:

install.packages("xml2", INSTALL_opts = c('--no-lock'))

The INSTALL_opts are for the command line installation of the tar.gz file with xml2's source code. So instead of running the default

R CMD INSTALL xml2_1.2.4.tar.gz

the command becomes

R CMD INSTALL --no-lock xml2_1.2.4.tar.gz