Using Multiple Python Versions and Environments with RStudio Server Pro and Jupyter Notebooks

Overview

RStudio Server Pro versions 1.2.5 and higher include the ability to use Jupyter Notebooks and JupyterLab.

In addition to the Python version and environment that is used in the RStudio Server Pro configuration, you can configure additional Python versions and environments using the following steps.

Prerequisites

  • RStudio Server Pro 1.2.5 and higher installed and configured with Jupyter Notebooks and one version of Python

  • Determine if you want to add a new Python version or environment for all users (Method 1, requires administrator access) or only for your user (Method 2)

Method 1 - Install additional Python version for all users

For administrators

Administrators can use this method to install an additional version of Python that is available globally to all users.

1. Install additional Python version

Install an additional version of Python on the server following the steps to Install Python.

Info

Our recommended installation instructions for Python allow you to make multiple versions of Python available and avoid replacing existing versions of Python when updating system packages.

2. Install the ipykernel package

From the previous step, you should still have the PYTHON_VERSION environment variable defined with the version of Python that you installed. If not, you can define this environment variable before proceed by running the following command and replacing 3.7.6 with the new version of Python that you installed:

export PYTHON_VERSION=3.7.6

Install the ipykernel Python package in the root of your newly installed Python environment using the following command:

Terminal

$ sudo /opt/python/${PYTHON_VERSION}/pip install ipykernel

3. Register additional Python version as a new kernel

Register the additional version of Python as a new kernel using the following command:

Terminal

$ sudo /opt/python/${PYTHON_VERSION}/bin/python -m ipykernel install --name py${PYTHON_VERSION} --display-name "Python ${PYTHON_VERSION}"

4. Verify the new kernel in Jupyter

In your browser, navigate to the RStudio Server Pro interface and log in.

Select New Session, choose Jupyter Notebooks or JupyterLab as the IDE, then click the Start Session button.

From the menu bar, select Kernel > Change Kernel and select the newly installed kernel.

Info

You can repeat the steps in this section to install additional Python versions and environments that will be available to all users.

Method 2 - Register an additional Python environment only for your user

For users

Users can use this method to register an additional Python environment that is available only to their user.

1. Create virtual environment

Open a Terminal in Jupyter Notebook or Jupyter Lab and create a virtual environment.

$ /opt/python/${PYTHON_VERSION}/bin/virtualenv ~/venvs/my_environment

# Optionally add a --python flag to set the Python version
$ /opt/python/${PYTHON_VERSION}/bin/virtualenv ~/venvs/my_environment --python /opt/python/${PYTHON_VERSION}/bin/python3

Replace ${PYTHON_VERSION} with the version of Python you want to use and ~/venvs/my_environment with the location you want to use for your virtual environment, this directory should be in your home directory.

2. Install the ipykernel package

Install the ipykernel Python package in the Python environment that you want to use from Jupyter Notebooks in RStudio Server Pro using the following command:

Terminal

$ ~/venvs/my_environment/bin/pip install ipykernel

Replace ~/venvs/my_environment/bin/pip with the path to pip in the environment that you want to use from Jupyter Notebooks in RStudio Server Pro.

Optionally install any other libraries you want to be available in that virtual environment.

$ ~/venvs/my_environment/bin/pip install pandas matplotlib

Or if you manage dependencies using a requirements.txt file:

$ ~/venvs/my_environment/bin/pip install -r requirements.txt

3. Register additional Python version as a new kernel

Register the additional Python environment as a new kernel using the following command:

Terminal

$ ~/venvs/my_environment/bin/python -m ipykernel install --name my-python-env --display-name "Custom Python environment" --user

Replace ~/venvs/my_environment/bin/python with the path to Python in the environment that you want to use from Jupyter Notebooks in RStudio Server Pro.

You can change my-python-env and Custom Python Environment in the above command to your desired kernel name and display name.

3. Verify the new kernel in Jupyter

In your browser, navigate to the RStudio Server Pro interface and log in.

Select New Session, choose Jupyter Notebooks or JupyterLab as the IDE, then click the Start Session button.

From the menu bar, select Kernel > Change Kernel and select the newly installed kernel.

Info

You can repeat the steps in this section to install additional Python environments that will be available only to your user.

Publishing Jupyter Notebooks to RStudio Connect

If you are using the rsconnect_jupyter Python package to deploy content to RStudio Connect and using multiple Python environments you have to:

  1. Install the rsconnect_jupyter Python package and enable the server extension in the global Python environment that RSP is configured for:

    $ /opt/python/3.7.6/bin/pip install rsconnect_jupyter
    
    # Enable Python extension
    $ /opt/python/3.7.6/bin/jupyter-serverextension enable --sys-prefix --py rsconnect_jupyter
    
  2. Install the rsconnect_jupyter Python package and enable the notebook extension on each environment:

    $ ~/venvs/my_environment/bin/pip install rsconnect_jupyter
    
    # Install `rsconnect-jupyter` as a jupyter extension
    $ ~/venvs/my_environment/bin/jupyter-nbextension install --sys-prefix --py rsconnect_jupyter
    
    # Enable JavaScript extension
    $ ~/venvs/my_environment/bin/jupyter-nbextension enable --sys-prefix --py rsconnect_jupyter
    

For more information refer to the documentation for rsconnect_jupyter.

Troubleshooting RStudio Server Pro and Jupyter

Refer to the support article on troubleshooting Jupyter Notebooks in RStudio Server Pro for additional information on troubleshooting RStudio Server Pro with Jupyter.

Additional Documentation

For more information on RStudio Server Pro and Jupyter Notebooks, refer to the following reference documentation:

For more information on using Jupyter kernels for different environments and kernel specs, refer to the following reference documentation: