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:

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.