Permission denied while installing R package

I've installed R (version 3.6.0) on my ubuntu 18.04 which I run on the windows subsystem WSL.

Now I'm having a problem to install some specific packages. For example xml2. When I run the command install.packages("xml2"), i get the following error:

mv: cannot move '/usr/local/lib/R/site-library/00LOCK-xml2/00new/xml2' to '/usr/local/lib/R/site-library/xml2': Permission denied
ERROR:   moving to final location failed

I guess there might be some way to give myself full permission for managing my own stuff, does anyone know how to get around this issue?


This solution suggested on stackoverflow.com worked for me.

The best way to avoid this is to use the '--no-lock' argument on the command line, e.g.,

R CMD INSTALL --no-lock <pkg>

On NFS file systems it is sometimes not obvious what things you have to close. From within R, you can do this from within your command using:

install.packages("Rcpp", dependencies=TRUE, INSTALL_opts = c('--no-lock'))

Uninstall R version 3.6.0, and remove the R PPA if there is one from your software sources in /etc/apt/sources.list by preceding the line containing this PPA with a # character to turn it into a comment. Then run sudo apt update to refresh your list of available software. Now that you have removed the R packages that don't work, you are ready to install r-base and r-cran-xml2 from the default Ubuntu 18.04 repositories.

To install xml2 R package in Ubuntu 18.04 open the terminal and type:

sudo apt install r-base r-cran-xml2  

This GNU R XML parser package works with XML files using a simple, consistent interface. It's built on top of the 'libxml2' C library.