Difficulty installing terra package in RStudio (positcloud)

I’ve got some polygons processed in ArcGIS representing glaciers, as well as a ERA-5 single level climate data which I want to clip to the polygons. I think I need the terra package to do it but every time I try to install the package either through code (ie install package) or through the package install icon I receive error messages and cannot load the package. Does anyone know what may be the issue? Thanks

ERROR: failed to lock directory ‘/cloud/lib/x86_64-pc-linux-gnu-library/4.5’ for modifying
Try removing ‘/cloud/lib/x86_64-pc-linux-gnu-library/4.5/00LOCK-terra’
Warning in install.packages :
  installation of package ‘terra’ had non-zero exit status

Please post the actual error message. You should format the message as code so it is easier to read. To do that, type a line with three back ticks before and after the error message, like this:
```
error message goes here
```

1 Like

Hi, I've just updated the post to include this

I'm not familiar with positcloud. Can you access the directory /cloud/lib/x86_64-pc-linux-gnu-library/4.5? If so, the error message suggests that you delete the file 00LOCK-terra from there. Have you tried that?

This happens a lot on PositCloud because installations get stopped and the lock file never goes away. To fix this, open the terminal in your session and type rm -rf /cloud/lib/x86_64-pc-linux-gnu-library/4.5/00LOCK-terra. Then, reinstall terra. If it still doesn't work, try installing it in a user library instead of the system library. Use the command install.packages("terra", lib="~/Rlibs"). most of the time, this is enough to get clipping workflows going again.

Great thank you. I’ll give it a go when I get the chance and get back to you

Hi again, I've tried both methods. Using the code in the terminal did not work, as it still responds with the same error code. However, when I run the install packages command it returns the following:

> library(terra)
Error in library(terra) : there is no package called ‘terra’

> install.packages("terra", lib="~/Rlibs")
Warning in install.packages :
  'lib = "~/Rlibs"' is not writable

Would you like to use a personal library instead? (yes/No/cancel)

Where do I go from here?

It seems like your user lib path was never made, so R is trying to write to a place that doesn't exist and can't be written to on PositCloud. There is a quick fix that usually works for terra:

mkdir -p ~/Rlibs
chmod 700 ~/Rlibs
echo 'R_LIBS_USER="~/Rlibs"' >> ~/.Renviron

restart the session after that, then:

install.packages("terra", lib="~/Rlibs")
library(terra)

Positcloud sometimes doesn't pay attention to the first "yes" prompt for personal libraries unless the folder is already there. To fix the install loop, you need to set the directory and permissions first. If it still gives you a lock error, delete the 00LOCK-* files again and try again. But once the user lib is writable, terra usually works.

Hi, I've tried this now, and deleted the lock file. Instead now it just ends the session half way through and redirects me to another RStudio window, and created another lock file. Would it be a suitable fix to install R Desktop to download and use terra, and then upload the outputs to my posit cloud session? Thanks

This looks like a locked library issue rather than a terra problem. Try removing the 00LOCK-terra folder and reinstall, or restart the session to clear the lock before installing again.