'Loading some packages with Rstudio server does not work, while it works with R server

I am using Rstudio server on a remote server and I have some packages already installed.

When I try to load libraries like raster or terra using Rstudio server, I get error:

> library(terra)

Error: package or namespace load failed for ‘terra’ in dyn.load(file, DLLpath = DLLpath, ...):
 unable to load shared object '/home/pearless/R/x86_64-pc-linux-gnu-library/4.1/terra/libs/terra.so':
  libproj.so.15: cannot open shared object file: No such file or directory

> library(raster)

Error: package or namespace load failed for ‘raster’ in dyn.load(file, DLLpath = DLLpath, ...):
 unable to load shared object '/home/pearless/R/x86_64-pc-linux-gnu-library/4.1/terra/libs/terra.so':
  libproj.so.15: cannot open shared object file: No such file or directory

Please, note that loading other packages using Rstudio server such as tidyverse works just fine:

> library(tidyverse)

── Attaching packages ──────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.1 ──
✓ ggplot2 3.3.5     ✓ purrr   0.3.4
✓ tibble  3.1.6     ✓ forcats 0.5.1
✓ readr   2.0.2     
── Conflicts ─────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()

When I use R console in the server without using Rstudio server, the raster and terra packages can be loaded without any problem:

$ R

R version 4.1.1 (2021-08-10) -- "Kick Things"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(terra)
terra version 1.4.11

> library(raster)
Loading required package: sp

How can I solve this problem, so that I can also load these packages in Rstudio server?

UPDATE based on @user2554330's comment:

  1. .libPaths() gives exactly the same result in both.

  2. Sys.getenv("PATH") and Sys.getenv("LD_LIBRARY_PATH") give different results in R and Rstudio server consoles - Rstudio server does not seem to be seeing most of the path variables. I ran pth = Sys.getenv("PATH") and ld_pth = Sys.getenv("LD_LIBRARY_PATH") in R in the server, and copied the results and pasted them into Sys.setenv(PATH = "result_of_pth") and Sys.setenv(PATH = "result_of_ld_pth") in the Rstudio server respectively. After doing this, I still get the same error when I load the libraries raster and terra.

  3. Yes, both are running with the same username.



Solution 1:[1]

The problem appears to be that terra does not work, and because raster depends on it, that package cannot load either.

From a Google search on the error message libproj.so.15: cannot open shared object file: No such file or directory I see this and this discussion, and perhaps more relevant pages.

These discuss the sf package but that package uses the same system dependencies as terra, so this should apply to terra as well.

Solution 2:[2]

Per robert-hijmans reply, I solved this by adding the following:

apt-get -y update && apt-get install -y \
    libudunits2-dev \
    libgdal-dev \
    libgeos-dev \
    libproj-dev \
    libmysqlclient-dev 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Robert Hijmans
Solution 2 danlooo