Configure Miniconda So That You Can Choose a Separate Environment Directly From Jupyter Lab/Notebook

To set up Miniconda in such a way that you can see and select all your environments directly in Jupyter Lab/Notebook on Ubuntu, follow these steps:

Step 1: Install Miniconda

  1. Download the Miniconda installer for Linux:

     wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
    
  2. Install Miniconda:

     bash Miniconda3-latest-Linux-x86_64.sh
    
  3. Follow the instructions, and ensure you initialize Miniconda.

  4. Restart your terminal or run:

     source ~/.bashrc
    

Step 2: Install Jupyter Lab or Notebook

You can install either Jupyter Lab or Jupyter Notebook in the base environment:

conda install jupyterlab  # For Jupyter Lab
# or
conda install notebook  # For Jupyter Notebook

Step 3: Install ipykernel in Each Conda Environment

To ensure that each environment is accessible in Jupyter Lab/Notebook, you need to install the ipykernel package in every environment and register it as a kernel.

  1. Activate your environment:

     conda activate myenv
    
  2. Install ipykernel:

     conda install ipykernel
    
  3. Add the environment to Jupyter as a kernel:

     python -m ipykernel install --user --name=myenv --display-name "Python (myenv)"
    

Repeat this for all the environments you want to be accessible in Jupyter.

Step 4: Verify in Jupyter Lab/Notebook

  1. Launch Jupyter Lab or Jupyter Notebook:

     jupyter lab  # For Jupyter Lab
     # or
     jupyter notebook  # For Jupyter Notebook
    
  2. When you create a new notebook, you'll be able to select from all the available environments (kernels) under Kernel > Change Kernel.

Step 5: Optionally Install nb_conda_kernels

To make environment management even easier within Jupyter, you can install the nb_conda_kernels package, which automatically detects all available conda environments.

  1. Install nb_conda_kernels in your base environment:

     conda install nb_conda_kernels
    
  2. Launch Jupyter, and you'll see all your Conda environments automatically listed under the kernels.

This setup will allow you to see and select separate environments directly within Jupyter Lab or Notebook!

0
Subscribe to my newsletter

Read articles from Md. Fahim Bin Amin directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Md. Fahim Bin Amin
Md. Fahim Bin Amin

Microsoft Research Investigation Contributor to OSS (GitHub: FahimFBA) | Software Engineer | Top Contributor 2022, 2023 @freeCodeCamp | ➡️http://youtube.com/@FahimAmin