============
Environments
============

Anaconda Notebooks is a hosted JupyterLab service that enables you to run JupyterLab notebooks online while simultaneously taking advantage of conda's robust environment management tools. To execute code in a conda environment in Anaconda Notebooks, you must associate each environment with a language-specific kernel. 

Environments and kernels
========================

What is an environment?
-----------------------

An environment is a folder or directory that contains a specific collection of conda packages and their dependencies.

Working in separate environments allows you to maintain and run packages without interference from other collections of packages. For example, you may require separate conda environments to maintain distinct versions of Python based on distinct package dependencies.

What is a kernel?
-----------------

A kernel is a programming-language-specific process that interprets your code, runs it, and gives you the results. In Jupyter Notebooks, kernels allow users to run code in cells and receive immediate output. For example, ``ipykernel`` enables interactive computing in Python, and ``r-irkernel`` enables the same for R. 

In order for an environment to be able to run code for a notebook, it must contain a kernel package; otherwise, the environment will not be recognized by JupyterLab as an available kernel. For this reason, the default environments available in Anaconda Notebooks already contain the ``ipykernel`` package. 

To add a kernel package to a custom environment, see :ref:`creating a custom environment <custom_notebook_env>`. 

.. _default_notebook_env:

Using default environments
==========================

Custom environments often use large amounts of your limited Anaconda Notebooks storage space. Because of this, Anaconda provides a number of *read-only* default environments that contain all the packages from the latest release of Anaconda Distribution.

The default environment naming convention is typically ``anaconda-<YEAR>.<MONTH>-py<PYTHON_VERSION>``, which maps to Anaconda Distribution releases:

.. csv-table::
    :file: ../csv/built-in-notebook-envs.csv
    :header-rows: 1

.. dropdown:: Packages included in anaconda-2024.02-py10
    :animate: fade-in 
    :color: primary 

    .. csv-table::
        :file: ../csv/anaconda-2024-02-py10.csv
        :header-rows: 1

.. dropdown:: Packages included in anaconda-ai-2024.04-py10
    :animate: fade-in 
    :color: primary 

    .. csv-table::
        :file: ../csv/anaconda-ai-2024-04-py10.csv
        :header-rows: 1

To see a list of available environments:
    
    .. code-block:: none
        
        conda info --envs

To see a list of packages in your current environment:
    
    .. code-block:: none

        conda list

.. _custom_notebook_env:

Creating custom environments
============================

If you need a specific set of packages that are not included in one of our default environments, you can create your own environment to customize to your needs.

.. note::
    Custom environments are stored using your dedicated, persistent Anaconda Notebooks storage. This ensures the custom environment will be available after the current session. Custom environments use your personal storage space and can easily get quite large, so *only include the packages you need*.

To create a new environment with an associated kernel:

#. Open a terminal from the Launcher, which you can access by clicking the blue plus **+** in the top-left corner. 

    .. thumbnail:: /img/notebook_open_terminal.png
        :align: center

#. Create a conda environment with an associated kernel by running the following command:

    .. code-block:: none

        # Replace <ENV_NAME> with a new name for your environment
        # Replace <PACKAGE> with the name of a package you want to install in your environment
        # Replace <KERNEL_PACKAGE> with the appropriate kernel package
        conda create --name <ENV_NAME> <PACKAGE> <KERNEL_PACKAGE>

    .. note::
        - To create a Python kernel, replace <KERNEL_PACKAGE> with ``ipykernel``.
        - To create an R kernel, replace <KERNEL_PACKAGE> with ``r-irkernel``.

For more information about creating conda environments, see the `official conda docs <https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html>`_.

It might take a minute for your environment to be created and available for use. You might need to close and reopen your active notebook or refresh the browser for your new environment to appear.

Activating environments
=======================

There are a few locations from which you can activate your environment:

.. tab-set::

    .. tab-item:: Terminal

        Run the following within Anaconda Notebooks:

            .. code-block:: none
    
                # Replace <ENV_NAME> with the name of your environment
                conda activate <ENV_NAME>


    .. tab-item:: Launcher

        Select the notebook displaying your custom environment name.

            .. thumbnail:: /img/notebook_launcher-env.png
                :align: center

    .. tab-item:: Notebook

        Click the kernel in the top right, then switch to your desired environment in the **Select Kernel** modal.

            .. thumbnail:: /img/notebook_kernel.png
                :align: center

Installing additional packages
==============================

Even after you've created an environment, you can continue to add packages as needed. To install additional packages in your environment, run the following command:

    .. code-block:: none

        # Replace <PACKAGE> with the name of a package you want to install
        conda install <PACKAGE>

Deactivating environments
=========================

It is best practice to deactivate your environment when you are finished working in it. To deactivate your active environment, run the following command:

    .. code-block:: none

        conda deactivate
