Skip to content

Library is not writable [SOLUTION]

Mattan S. Ben-Shachar edited this page Nov 2, 2020 · 3 revisions

The Problem

When trying to install a package you get the an error similar to this:

Warning in install.packages :
  'lib = "<some library path>"' is not writable
Warning in install.packages :
  Cannot create dir '<path>\?????', reason 'Invalid argument'
Error in install.packages : unable to create '<path>\?????'

Why does this happen?

Seems like you don't have writing permissions to the default folder, and R is not able to create a new folder - possibly due to the default path having non ASCII letters? (This happens to my students with Hebrew paths.)

Solution(s)

(These have only been tested on Windows...)

1. Always run as administrator

This solution is based on this guide.

  1. Close all windows of R or Rstudio.
  2. Open Start and search for RStudio.
  3. Right-click the top result, and select Open file location.
  4. Right-click the app shortcut and select Properties.
  5. Click on the Shortcut tab, then on the Advanced button.
  6. Check the Run as administrator option.
  7. Click the OK button, then the Apply button, and finally the OK button.
  8. Open RStudio and try to install packages again.

Done.

If this doesn't work, try the next solution.

2. Use custom r library

This solution is based on this awesome guide.

  1. Create a folder

This is where packages will be installed, so make sure you put is somewhere where it won't accidentally get deleted.
It should also be comprised of only base English letters.
Suggestion: Folder R_PKGS in C:.

  1. Open R / Rstudio as admin
    Make sure no other windows of R/Rstudio are open.

  2. Run in R console

file.edit(file.path(R.home(component = "home"), "library", "base", "R", "Rprofile"))

This should open a file names Rprofile. Scroll to the bottom of it.

  1. Add the following code to the bottom
#### MSB ####
{
  # WHERE TO SAVE?
  pkg_install_dir <- "<REPLACE ME WITH THE NEW PATH>"
  
  if (.libPaths()[1] != pkg_install_dir) {
    # DOES THE FOLDER EXIST?
    if (pid_not_exist <- !file.exists(pkg_install_dir))
      warning(pkg_install_dir, " directory does not exist.")
    
    # IS THE FOLDER WRITABLE
    if (!pid_not_exist && (pid_not_write <- file.access(pkg_install_dir, mode = 2)))
      warning("Cannot write to", pkg_install_dir, ". Are you admin?")
    
    # change file order
    if (!pid_not_exist && !pid_not_write) {
      .libPaths(unique(c(pkg_install_dir, .libPaths())))
      cat("NOTE: Added ", pkg_install_dir, " to .libPaths(), as your default pkg installation folder.\n")
      cat("NOTE: This may not work when updating or fresh-installing R.\n\n")
    }
  }
}
#### \MSB ####

And replace <REPLACE ME WITH THE NEW PATH> with the full path to the new folder.
Note: that if your pack has any backslashes ("\", not "/"), they must be doubled. So C:\R_PKGS must be C:\\R_PKGS.

  1. Save (ctrl + S) and close R / Rstudio.

  2. Open R / Rstudio

You should see in the console a message telling you that you path has been added as the default installation path.

  1. Install a package

It should work 🤷‍♂️

Clone this wiki locally