Virtual Environments and Custom Kernels

Virtual environments allow you to install packages for projects without having to worry about conflicting dependencies with other packages/versions. You can set up multiple virtual environments with different package sets.

In JupyterLab, you can also use virtual environments to create custom kernels so that you can use your custom package sets in new or existing notebooks.

Create and Set Up a Virtual Environment

These steps show you how to create and set up a virtual environment in JupyterLab. This includes activating the environment and installing packages. If you want to use your virtual environment in a notebook, after these set up steps, continue to Create a Custom Kernel.

  1. Open a JupyterLab Terminal and execute the following conda create command; replace NetID with your UIUC NetID and env_name with the name you want for new virtual environment.

    conda create --prefix /home/NetID/env_name
    
  2. Activate your virtual environment with the following source activate command; replace NetID with your UIUC NetID and env_name with the name of your new virtual environment.

    source activate /home/NetID/env_name
    

    When your virtual environment is activated, your terminal prompt will look like:

    (/home/NetID/env_name) NetID@jupyter-NetID:~$
    

    For example, a user with the NetID jdoe1 that set up a virtual environment named envs would have this terminal prompt: (/home/jdoe1/envs) jdoe1@jupyter-jdoe1:~$

  3. Install packages into your virtual environment with conda install package_name; replace package_name with the name of the package you want to install.

    For example, to install numpy:

    conda install numpy
    
  4. After you have installed the packages you want in your virtual environment, verify them by executing conda list.

  5. When you want to deactivate your virtual environment, perform one of the following:

    • Execute conda deactivate.

    • Activate another, existing environment with the source activate command from step 2; replace env_name with the name of the virtual environment you want to activate.

Create a Custom Kernel - Install and Activate ipykernel

After you have a virtual environment set up, you can install ipykernel to use the virtual environment (and the packages you installed in it) in a notebook.

  1. In a Terminal window, verify that your virtual environment is activated. Your terminal prompt should start with (/home/NetID/env_name).

    If your virtual environment is not activated, execute the following command; replace NetID with your UIUC NetID and env_name with the name of your virtual environment.

    source activate /home/NetID/env_name
    
  2. Execute the following conda install command to install ipykernel:

    conda install -c conda-forge ipykernel
    
  3. Execute the following command; replace env_name with the name of your virtual environment.

    python -m ipykernel install --user --name=env_name
    
  4. Open a new Launcher window and launch a notebook.

  5. In the notebook open the Kernel menu and select Change kernel.

    Kernel menu with the change kernel option highlighted.
  6. If your new env_name kernel is listed in the drop-down menu, select it.

    Pop-up window where you select a kernel.

    If your new environment isn’t listed:

    1. Open the Kernel menu and select Restart Kernel.

      Kernel menu with the restart kernel option highlighted. Pop-up window where you confirm your choice to restart the kernel.
    2. Open the Kernel menu and select Change Kernel.

      Kernel menu with the change kernel option highlighted.
    3. Select your env_name kernel from the drop-down menu.

      Pop-up window where you select a kernel.