1

I have uploaded an R package onto my Ubuntu server. I then install this package into R library as follows:

install.packages('my_package', repo=NULL, dir='/usr/lib/R/library')

All other packages are installed in the /usr/lib/R/library directory as confirmed by running:

installed.packages()

and viewing the LibPath column.

I then add to library using:

library(my_package,lib.loc="/usr/lib/R/library")

but when I run installed.packages() and view the LibPath column I can see that it was added to a different directory. The directory looks like:

/home/my_server/R/x86_64-pc-linux-gnu-library/3.3

How can I install my package into the correct directory on Ubuntu?

Also tried:

install.packages('my_package', repo=NULL, lib.loc='/usr/lib/R/library')

Also tried changing the write permissions to that folder. Still doesn't work. No matter what I try, it always places my package in /home/my_server/R/x86_64-pc-linux-gnu-library/3.3

Cybernetic
  • 12,628
  • 16
  • 93
  • 132

2 Answers2

2

Finally got it to work by changing the write permissions using:

sudo chmod -R 0777 '/usr/lib/R/library'
Cybernetic
  • 12,628
  • 16
  • 93
  • 132
  • It is probably safer to change the owner in the folder, instead of giving 777 permissions? `sudo chown myuser:myuser /usr/lib/R/library/ * -R` – Gorka Mar 17 '23 at 07:59
  • Please, dear readers, don't do this on a multi-user system. You might as well hang a sign "DEPOSIT YOUR EVIL-DOING SCRIPTS HERE" or "PLEASE `rm -rf` ME AT A VERY INCONVENIENT TIME" Gorka's proposed alternative works fine (if you remove the errant `*`) if it's just your personal system. Under all other conditions, consider literally any other alternative, even installing that one package as root. – TheDudeAbides Jun 04 '23 at 13:45
0

Safe and recommended alternatives have already been discussed elsewhere, so this question should probably be closed as a duplicate.

But if it's just the one package, consider doing the install as root using sudo. This feels dirty, but if you're looking for a quick fix, this is slightly better than opening up a system directory for write access to all users henceforth. Except if that package contains malware, I guess.

It's typically advised that you don't mess with the "system" libraries, though, which are the domain of the package manager, i.e., apt on Ubuntu. As for a better long-term alternative, the short answer is: consider using /usr/local for locally-installed (as opposed to system-installed) packages. This is what /usr/local is for.

You can add yourself to the staff group on Ubuntu (or whatever particular "admin" group applies to your distro) and then grant yourself full-time access to /usr/local like this:

# you will need to log out and back in for this to take effect
sudo usermod -aG staff $USER

# assuming a local ext3/4 filesystem
# grant 'staff' read/write for existing files r/w/x for existing dirs
sudo setfacl -R -m g:staff:rwX /usr/local
# also apply these by default to new files and directories
sudo setfacl -R -d -m g:staff:rwX /usr/local

I'm not nearly an expert with configuring R for a large site, so please do your own research. However, based on observation, /usr/local/lib/R/site-library is already the first path in R_LIBS_SITE as defined in the factory-installed /etc/R/Renviron on Debian/Ubuntu, which suggests it's the correct place for a local admin to install packages.

So, if that directory exists on the filesystem and you have read/write access to it, then future install.packages as your regular user will just "do the right thing" and go into /usr/local:

> .Library.site
[1] "/usr/local/lib/R/site-library" "/usr/lib/R/site-library"      
[3] "/usr/lib/R/library"       

> install.packages("lubridate")
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
⋮

Hope that helps.

TheDudeAbides
  • 1,821
  • 1
  • 21
  • 29