87

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?

shadowtalker
  • 12,529
  • 3
  • 53
  • 96
Priya
  • 1,069
  • 1
  • 9
  • 8

13 Answers13

69

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!

manuel_va
  • 934
  • 8
  • 11
  • 2
    You mention "possible downside." What are possible downsides? Can I muck up my R installation accidentally if I do this? – Nathan majicvr.com Mar 08 '18 at 17:28
  • 1
    Are there downsides to saying yes to both questions? – Stephen Sep 28 '18 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](http://agent18.github.io/R-installation.html) – agent18 Oct 23 '18 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 Oct 30 '18 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 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 Jan 17 '19 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.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Seyi
  • 463
  • 5
  • 11
  • 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 – Abdulkarim Kanaan Nov 02 '18 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 Jul 12 '20 at 16:18
  • You, sir, are a legend. – Sidders Aug 29 '22 at 06:09
  • You deserve every possible thumbs-up m8! – Farid Huseynov Oct 09 '22 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!!!

Robert TheSim
  • 433
  • 5
  • 6
22

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.

BanAnanas
  • 478
  • 8
  • 17
  • 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 – Nikolay Nenov May 14 '19 at 13:29
  • Feel free to edit my answer by adding your solution – BanAnanas May 14 '19 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 Apr 10 '20 at 15:37
  • @Ash, thank you for the explanation. I have removed other solution. Feel free to edit my answer further if needed :) – BanAnanas Apr 11 '20 at 17:10
  • This worked for me on windows 10 , R 4.0.5 – Claudio Paladini Sep 15 '21 at 19:20
  • This is the expected way to install multiple packages at once, so It should work regardless of the OS, and version I believe :) – BanAnanas Sep 15 '21 at 19:33
  • This is answers a question that the original question poster did not ask. I do not understand why this is the top answer for this question. – Colin McGovern Nov 11 '21 at 20:53
  • Hi Colin, because the warnings are similar, many end up finding the question while searching for a solution to the problem this answer solves for them. I thought It wouldn't harm anyone If I post the solution I figured worked for the problem I had at the time. If you wish, you are free to post a specific question and answer it with my answer and take the credit there :) – BanAnanas Nov 12 '21 at 09:09
19

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.

teichert
  • 3,963
  • 1
  • 31
  • 37
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.

Community
  • 1
  • 1
shadowtalker
  • 12,529
  • 3
  • 53
  • 96
  • 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 Sep 15 '15 at 11:32
  • 1
    Third option (see below): add yourself to the `staff` group which gives you permission to write in that folder. – asachet Jan 17 '19 at 10:48
2

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.

mattliu
  • 823
  • 12
  • 28
1

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

Ljupcho Naumov
  • 196
  • 2
  • 12
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’
iamabhaykmr
  • 1,803
  • 3
  • 24
  • 49
0

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

Fish Ung
  • 17
  • 8
0

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

ASKING
  • 1
  • 1
-4

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.

Scarabee
  • 5,437
  • 5
  • 29
  • 55
user3218971
  • 547
  • 1
  • 6
  • 21
  • 9
    It looks more reasonable to change permission on single directory than allowing an application to potentially nuke whole system – jangorecki Apr 18 '16 at 14:23
  • This also makes updating and uninstalling packages a real pain. – jsta Nov 02 '17 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`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/libPaths.html) – Giupo Sep 16 '21 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

Igor Misechko
  • 23
  • 1
  • 1
  • 3
    This is probably quite unsafe! – cd98 Oct 03 '17 at 00: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. – David Arenburg May 19 '20 at 16:06