92

I have this issue during package installation in R version 3.0.2 (2013-09-25) on an Ubuntu machine:

install.packages("randomForest")
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages :
  'lib = "/usr/local/lib/R/site-library"' is not writable

How to resolve it?

2
  • 5
    just do sudo R if you are using terminal. I cant tell if this is a script to execute thought.
    – bb8
    Commented Oct 11, 2019 at 0:26
  • 2
    do NOT just sudo this. leads to all sorts of issues with read-write access.
    – emilBeBri
    Commented May 20, 2022 at 17:43

16 Answers 16

72

For R version 3.2.2 (2015-08-14) this problem should be dealt with since R suggests within the installation process a different path to store your R libraries. The installation looks like this: (Here 'random' is used as an example package)

install.packages('random')

Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages("random") :
'lib = "/usr/local/lib/R/site-library"' is not writable

Would you like to use a personal library instead?  (y/n) y

Would you like to create a personal library
~/R/pc-linux-gnu-library/3.2
to install packages into?  (y/n) y

So during the installation answering both questions with 'y' should install the package correctly.

Update 18/01/19

In case you don't want to store your R packages in an additional file:

As Antoine-Sac and Robert TheSim point out you can add yourself to the staff group in order to be able to write to 'site-library'. (Click on the names to see their important additions)

Before this update I mentioned in this comment the option of changing the permission of the folder 'site-library' using 'chmod o+w' in order to be able to write to it. Assuming security issues but unable to tell at the time I warned about it but have primarily been waiting for somone to clear this up. Antoine-Sac and Robert TheSim have done so in the meantime. Thanks!

5
  • 2
    You mention "possible downside." What are possible downsides? Can I muck up my R installation accidentally if I do this? Commented Mar 8, 2018 at 17:28
  • 1
    Are there downsides to saying yes to both questions?
    – Stephen
    Commented Sep 28, 2018 at 16:10
  • Changing permissions funked up my installation somehow! It was not enough and I had to do a full reinstall as per here
    – agent18
    Commented Oct 23, 2018 at 20:12
  • Sorry, I haven't noticed your comments until now. @Stephen: To my knowledge answering both questions with yes doesn't cause any problems at all. A personal library will simply be created automatically. frank: Changing the permissions of the folder 'site-library' didn't lead to problems on my machine. Unfortunately I can't tell for other systems and as Thej Kiran mentions problems might occur. So the sure way is creating the library automatically.
    – manuel_va
    Commented Oct 30, 2018 at 20:05
  • 3
    Don't do that! Setting o+w allows any other user (which means any process running) to write to the site-library, which is a security concern. The site-library directory belongs to group staff. Simply add yourself to the staff group (on debian and related sudo adduser <username> staff, see the answer below for the more generic command) and you you can write to it. No need to change the permissions at all.
    – asachet
    Commented Jan 17, 2019 at 10:41
28

If you are on Windows, you can run R (or RStudio) as an administrator.

Close your R, then go to the R or RStudio icon, right-click and "open as administrator". It works perfectly, all error messages while installing packages are gone forever.

4
  • thank you. Your answer helped me as you stated about elevating to administrator. I just relaunched the command editor with admin prevelage, then eveything went as charm. FYI, I am using a Windows machine, not Linux. I don't know if you can do the same on Linux Commented Nov 2, 2018 at 16:42
  • Running things with privilege is easy in Linux, and it is used often. To open RStudio with root privilege type sudo rstudio command in terminal.
    – BanAnanas
    Commented Jul 12, 2020 at 16:18
  • You, sir, are a legend.
    – Sidders
    Commented Aug 29, 2022 at 6:09
  • You deserve every possible thumbs-up m8! Commented Oct 9, 2022 at 14:51
25

add yourself to the group called 'staff'

sudo usermod -a -G staff your_user_name

replace your_user_name with your login username, then logoff and relogin.

DO NOT use chmod 777 which is a breach of security and btw. a complete non-sense!!!

8
  • 1
    Yes this works perfectly fine - just need to log in and out of rstudio. Thanks for pointing out that site-library belongs to staff!
    – asachet
    Commented Jan 17, 2019 at 10:46
  • Worked perfectly! Thank you!
    – PeterD
    Commented Feb 2, 2019 at 21:36
  • This might be specific to the centos like distros and not generic enough Commented Apr 16, 2019 at 10:53
  • 1
    Does whoami provide me the your_user_name variable you are using?
    – Kots
    Commented Apr 30, 2019 at 7:40
  • 1
    the owner of that folder is root, not staff
    – giammi56
    Commented Apr 22, 2021 at 12:52
25

The problem is that the default install location is a place where you do not have write permissions.

The solution is to use an install location where you do have write permissions.

Specifically, I'd suggest using the following commands to create a personal library folder in a place that doesn't require special permissions and that will automatically be detected the next time you startup R:

dir.create(Sys.getenv("R_LIBS_USER"), recursive = TRUE)  # create personal library
.libPaths(Sys.getenv("R_LIBS_USER"))  # add to the path

install.packages("randomForest")  # install like always
library(randomForest)  # use library like always

The call to dir.create follows the suggestion in this faq to create a folder named according to Sys.getenv("R_LIBS_USER"). This is a good choice since it will be found on the next startup of R so you will be able to use install.packages and library without specifying special locations. The .libPaths function call lets us avoid restarting R by immediately adding the new folder to the library path. The first two lines are only needed if you do not yet have a personal library created, but there is no harm in running them repeatedly. After that, installing packages and using libraries can be done as usual.

2
  • I had the same problem in linux, and this was the answer which solved it. Thanks!.
    – Francisco
    Commented Nov 21, 2019 at 11:32
  • This is the most R-native solution. You get my upvote.
    – jkix
    Commented Aug 24 at 3:49
24

For someone who tried to use install.packages() with multiple packages like this:

install.packages("vcd","vcdExtra","plyr")

and got similar warning:

Warning in install.packages :
  'lib = "vcdExtra"' is not writable
Would you like to use a personal library instead? (yes/No/cancel) cancel
Error in install.packages : unable to install packages

You should put the package names in a vector:

install.packages(c("vcd","vcdExtra","plyr"))

as the second parameter in install.packages() is lib.

8
  • 3
    I had the same problem until I realized I should be wrapping the package names in c(). So, install.packages(c("vcd","vcdExtra","plyr")) should work Commented May 14, 2019 at 13:29
  • Feel free to edit my answer by adding your solution
    – BanAnanas
    Commented May 14, 2019 at 13:48
  • 2
    install.packages("vcd","vcdExtra","plyr") should've been install.packages(c("vcd","vcdExtra","plyr")), the second parameter in the install.packages is lib
    – Ash
    Commented Apr 10, 2020 at 15:37
  • @Ash, thank you for the explanation. I have removed other solution. Feel free to edit my answer further if needed :)
    – BanAnanas
    Commented Apr 11, 2020 at 17:10
  • This worked for me on windows 10 , R 4.0.5 Commented Sep 15, 2021 at 19:20
6

It means exactly what it says. You don't have write permission in that folder. Either you need to change the permissions for that folder, or change the R library location.

2
  • thank you sir..! I have doubt in one more question.. I got the error while installing the rgl package in Rstudio. install.packages("rgl") i got the error of Package LibPath Version Priority Depends Imports LinkingTo Suggests Enhances License License_is_FOSS License_restricts_use OS_type Archs MD5sum NeedsCompilation Built.
    – Priya
    Commented Sep 15, 2015 at 11:32
  • 1
    Third option (see below): add yourself to the staff group which gives you permission to write in that folder.
    – asachet
    Commented Jan 17, 2019 at 10:48
3

If you are using OS windows 10 then maybe Ransomware protection is on. You need to disable that.

I was facing the same problem. I had the access to write. but suddenly it stopped. I couldn't install the packages. Disabling Ransomware protection worked for me.

1

The "XX" is not writable error can also mean that the library path you're providing does not exist.

1

We can use Rscript with sudo.

$ sudo Rscript -e "options(repos=list(CRAN='https://cloud.r-project.org')); \
install.packages('randomForest')"
1
  • 1
    Simplest answer. It worked for me.
    – jkix
    Commented Aug 24 at 4:07
0

Use sudo to Rscript code. I have same error fixed using sudo Rscript filename.R

Error

$ Rscript babynames.R 
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages("babynames") :
  'lib = "/usr/local/lib/R/site-library"' is not writable
Error in install.packages("babynames") : unable to install packages
Execution halted

Fix

$ sudo Rscript babynames.R 
[sudo] password for abhay: 
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
also installing the dependencies ‘cli’, ‘glue’, ‘utf8’, ‘fansi’, ‘lifecycle’, ‘magrittr’, ‘pillar’, ‘pkgconfig’, ‘rlang’, ‘vctrs’, ‘tibble’
0

Maybe can try sudo chmod +777 #nameoflib It works for me

0

I noticed that if rstudio is sandboxed it gives the "is not writable" warning.

0

I was struggling to reproduce the Quarto book mlr3book in Ubuntu 22.04 because the package MASS requires R-4.2 instead of R-4.1 (the default). After uninstalling R-4.1 and installing R-4.2, I got this error that didn't get before with 4.1: Installing package into ‘/usr/local/lib/R/site-library’ (as ‘lib’ is unspecified). I suspected that the settings for the local user library was removed.

My solution was accidental. I opened RStudio to see if I had the same problem installing packages as I had in the terminal; I installed one package successfully. So, I went back to my terminal, and now I don't get the error anymore.

I cannot explain exactly how RStudio fixed this but I assume it automatically sets up a local user library when it starts.

0

I got a similar error message at:

renv::activate()
renv::restore()

Intrestingly renv::activate() did not set the library path correctly. So I suspect a bug in rstudi/[email protected]

Based on answer of @teichert I found a workaround as:

.libPaths(renv::paths$library())
renv::activate()
renv::restore()
-3

If you are using R with RStudio, rather than starting RStudio with tray icon, start Rstudio or R with command line using sudo rstudio or sudo R.

It will solve your problem for sure, it works for me. It requires sudo privilege to write something in installation directory.

3
  • 9
    It looks more reasonable to change permission on single directory than allowing an application to potentially nuke whole system
    – jangorecki
    Commented Apr 18, 2016 at 14:23
  • This also makes updating and uninstalling packages a real pain.
    – jsta
    Commented Nov 2, 2017 at 17:35
  • "with great power comes great responsibility": you don't want to use sudo this way, won't you? Fix the permissions on filesystem and/or change the location where to install the package. To an extent, use the $R_LIBS_USER
    – Giupo
    Commented Sep 16, 2021 at 13:20
-4

You can change permission to 'site-library' and all included directories.

sudo chmod 777 -R /usr/local/lib/R/site-library

2
  • 3
    This is probably quite unsafe!
    – cd98
    Commented Oct 3, 2017 at 0:53
  • What's so unsafe in make R library folder writable? I don't really understand why it's not writable in the first place. It's whole idea/design is for you to write libraries into it. Commented May 19, 2020 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.