Search code examples
rinstallationdevtools

R devtools::install(): how to install to user-local library?


I'm trying to install a package developed locally, via devtools::install(), within an R session started as normal user (not sudo) in a Linux machine. The installation fails with the error

no permission to install to directory ‘/usr/local/lib/R/[...]

this is because the R libraries are administrated as sudo.

Is there any way to install into a user-local library?

Note that:

  • I cannot use devtools::load_all() because this route does not fully allow the use of parallelization (it's a known issue).

  • Unfortunately I didn't manage to find any help about this on Stackoverflow – but probably it's because I'm using the wrong search terms. Happy to delete this question if someone can point me to a relevant one.

Cheers!


Solution

  • If you check out the ?devtools::install help page, you'll see a note in the "Details" section that says

    To install a package in a non-default library, use withr::with_libpaths().

    You can specify whatever path you want

    mylib <- "~/Rlib"
    withr::with_libpaths(mylib, {
       devtools::install(...)
    })
    

    You can then use that path when you load the library install.packages(..., lib=mylib) or use .libPaths(mylib) to add it more generally.