Start Rstudio Server session in conda environment

If I start Rstudio at the command line from within a conda environment, the IDE loads with all my packages and paths set up correctly.

Is there a way I could have an Rstudio Server session start in a conda environment? I tried putting a .Renviron file in the project home with source activate <env>, but that didn't do it.

Also, I've tried reticulate and it flat out doesn't function.

Is there a way to have Rstudio Server start my session in a specific conda environment?

1 Like

Do you plan to use reticulate on the server ?
If so, you should read this article
By configuring RETICULATE_PYTHON environment variable on the server, it will always use the same python version. You can configure with Sys.getenv in .Rprofile or using .Renviron, for your user. A server admin can also do this server-wide.

use_conda function is another lighter option to put in your projects.

If you don’t use reticulate, you may be able to configure your session profile on their server to activate an environment at the beginning of your session.
If you have rstudio server pro, there is a config file to do that at the start of an r session in rstudio server.

Hope it helps

@cderv -- I have tried reticulate, but unfortunately, it doesn't actually work.

> library(reticulate)
> use_condaenv('dada2')
> system('conda list | grep dada2')
# packages in environment at /home/balter/conda/envs/dada2:
bioconductor-dada2        1.6.0                  r3.4.1_0    bioconda
> library(dada2)
Error in library(dada2) : there is no package called ‘dada2’

The problem here is that no :package: dada2 is installed on your R libraries. Did you try to import this as python :package: in R? If so, I think you should use reticulate::import

os <- import("os")

Also, you can use reticulate::conda_list() to list the available conda environment.
When trying to load an environment, I think you should put the exact name, here bioconductor-dada2 according to you example. Also, if you want to be sure that it is loaded, you can use required = TRUE in use_condaenv to get an error if it fails located the correct python version. See ?use_condaenv
Can you try again whith these infos ?

After reading a bit more about reticulate I think I now understand that it only brings in the Python content of the library.

Sorry to be blunt, but in my opinion this is not really very useful. The ENTIRE point of conda is that it manages environments comprised of software from all realms simultaneously. So use_condaenv bring in Python from the environment, but not R packages.

So, I'm still trying to figure out how to robustly use a conda environment with RStudio.

This is my best attempt:

conda_base = system("which conda | sed 's/bin\\/conda//'")
env = "myenv"
env_base = paste0(conda_base,"envs/",myenv,"/")

PATH = Sys.getenv('PATH')
message(paste0("PATH: ",PATH))

Sys.setenv("R_LIBS_SITE"=paste0(env_base, "/lib/R/library/"))
#Sys.setenv("R_LIBS_USER"=paste0(env_base, "/lib/"))
Sys.setenv("R_INCLUDE_DIR"= paste0(env_base, "/lib/R/include/"))
Sys.setenv("R_SHARE_DIR"=paste0(env_base, "/lib/R/share"))
Sys.setenv("R_HOME"=paste0(env_base, "/lib/R"))
Sys.setenv(PATH=paste0(env_base, "/bin:", PATH))
.libPaths(paste0(env_base, "/lib/R/library/"))

#Sys.setenv(LD_LIBRARY_PATH=paste0(env_base, "/lib/R/library/"))

I'm especially unsure about the LD_LIBRARY_PATH.

I finally understand that you want to use R conda environment with RStudio. reticulate is not for this.

Usually, I have seen R and Anaconda used together on Desktop, and Anaconda Navigator allow that:

For doing it without navigator, did you try to configure RStudio to use the R version in the conda env you want with RSTUDIO_WHICH_R ?

See linux part, in this doc

There is an example you can adapt

I am not sure it works, however, it worth the try I think.

But I'm guessing I need to do much more than just point to the R executable. I'll need to also set up paths to "include", "share", "lib", etc., right?

Most people do not use anaconda navigator because that is a behemoth. You can't install it on a small cloud VM.

Also, I'm mostly using RStudio Server.

Have you tried?
When you open this executable in the conda env, does the libpath point to the library inside the conda env? I suspect that some configurations with this R executable are made such that everything run in that env.

And how does this work without Rstudio? At the end , rstudio is just an IDE above a R engine. If you configure nothing when launching the R executable in the conda env you want from command line, I think it should work as is with rstudio.

I like the idea of "rstudio is just an IDE above a R engine," and it can be true for the desktop IDE (when I start it from the command line it runs using the same path as my shell), but it is most definitely not true for the server. I can make very trivial examples where I run something in R command line that behaves completely differently from inside the RStudio Server IDE.

But the server version is definitely going to be more complicated. That's to be expected because the server starts R as a system service. I just wish there was some better documentation or clarity about how to configure it in different ways.

I can make a global setting in /etc/rstudio/rserver.conf as per the docs. But that doesn't help me use a particular environment of choice, or more simply my "own" R.

When you change the setting to the R version of your own env it does not work ? As you are refering to the doc, should we understand you have a RStudio Server PRO ?

I think that know I understand more clearly what you mean, I will move your issue in the r-admin topic. It definitely belongs there and some rstudio folks we surely be able to help you more, and they will be interested by your feedback. Also, if you have a pro licence, you should also contact the pro support.

Too bad my advices did not worked for you, it really seemed from the different documentations on the internet that it works for other people to set this env variable.

1 Like

Have you tried the instructions at ?


conda is not a package manager in the strict sense because it is also an environment, which is why you are struggling so much. To get what you want working for RStudio Server, the entire RStudio Server service (the daemon, the log files, the PATH(s), R, etc) all have to run inside of the conda environment. Think of a conda environment as a VM and the errors you are seeing will make more sense. For example, you wouldn't expect to be able to RStudio Server outside a VM and expect it to use R and packages from inside the VM . Same problem here. You have to put RStudio inside the conda environment. For RStudio Desktop this is reasonable, but as you describe, RStudio Server is more challenging because it expects to be installed in a Linux OS structure.

Python and R packages / libraries are different enough that I'd recommend using different tools to manage them. I've been successful using conda or virtualenv to manage my Python dependencies (incorporated into R via reticulate) while using R tools (packrat, devtools, base R, etc) to manage the R environment. As an example, this enables R users to continue to use the familiar install.packages instead of trying to use conda to install R packages (which, as you suggest, is not natural).


@cderv I am using the open-source version. According to the docs, the professional edition is launched in a bash shell while the OSS version is not. I'd love to better understand how it is launched and how the R kernels within it are launched. That would help a lot.

The cynic in me thinks that the reason RStudio does it this way is to intentionally hobble the OSS version.

I can set rstudio_which_r in /etc/rstudio/rserver.conf. But then I can only use one conda environment.

The Cyverse thing seems to apply to their system. Besides, I don't run the entire Anaconda behemoth. Just the conda package manager.

I also tried having export RSTUDIO_WHICH_R=<....../conda/envs/myenv/bin/R> in a .Renviron file. This did nothing. In fact, I'm not convinced the .Renviron file is run at all. Some tests have proved negative.

I REALLY would appreciate if you could escalate this to r-admin channel. I think there is large community that would benefit from being able to efficiently use conda as a package manager for RStudio. Note that Jupyter makes this seamless. The nb_kernels extension allows Jupyter to discover the available environments and let you start your notebook (in any language) using the one of your choice.

@andrie -- As I said, finding a way to use conda with RStudio is extremely important to the computational community. That page has a list of possible approaches, some of which work in some situations. However there is not a solution for open source RStudio Server to work with a choice of environments on a project-based level.

It has been done. Did you see the answer of @slopp?

It seems what you want to do is difficult and be a corner case due to conda working behavior. I am like him with all this: I use conda for python only with reticulate and use R ecosystem directly to deal with package management by project. You have several solution for that (as :package:) like packrat, jetpack, or rsuite and also helping tools like devtools or remotes. Never had a problem with that, and it allows to have everything available as some :package: are not available through conda.

Per the documentation, you need to define this in a linux user profile (like ~/.profile. .Renviron is for defining environment variable for R only, not RStudio. What you want to try here, is indicating RStudio when it starts to use this version of R. As I understood @slopp, it is like having one rstudio server per environment project, as VM. So, if this works for one environment, you need then a mechanism to switch this variable by project. At the end, very challenging.

The post is now in R-Admin channel, we'll see if others feels like you and have some experience.