Virtual Environments

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.

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 setup steps, continue to the ipykernel steps.

The examples in the below steps show the commands for a user with the NetID jdoe1 setting up a virtual environment named envs.

  1. Open a JupyterLab Terminal and execute the conda create command below; 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
    

    For example, conda create --prefix /home/jdoe1/envs.

  2. Execute the command below to activate your virtual environment.

    source activate /home/NetID/env_name
    

    For example, source activate /home/jdoe1/envs.

    When your virtual environment is activated, your terminal prompt will look like (/home/NetID/env_name) NetID@jupyter-NetID:~$.

    For example, (/home/jdoe1/envs) jdoe1@jupyter-jdoe1:~$.

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

    For example, to install numpy, execute conda install numpy.

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

    If you only installed one package you can verify it using conda list package_name. For example, conda list numpy.

  5. When you want to deactivate your virtual environment, execute the conda deactivate command or activate another, existing, environment with the source activate /home/NetID/env_name command.

Install and Activate ipykernel to Use in a Notebook

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.

The examples in the below steps show the commands for a user with the NetID jdoe1 and a virtual environment named envs.

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

    For example, (/home/jdoe1/envs) jdoe1@jupyter-jdoe1:~$.

    If your virtual environment is not activated, execute the command below; 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 conda install command below to install ipykernel:

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

    python -m ipykernel install --user --name=env_name
    

    For example, python -m ipykernel install --user --name=envs.

  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.