How to add Conda Environment to Jupyter Notebook: A Step-By-Step Guide

Are you tired of feeling like a juggling circus act, constantly using multiple environments while trying to work on Jupyter Notebook? Well, it’s time to bring in the big guns: Conda! 

Conda is the superhero of package managers that can create, manage, and switch between different environments like a pro. And the best part is, it’s a breeze to add Conda environments to Jupyter Notebook! But how?

In this post, we’ll show you how to harness the power of Conda and add it to your Jupyter Notebook toolkit and some amazing benefits. 

So without much ado, let’s get into this.

How to Add a Conda Environment to the Jupyter Notebook?

Well, there are many methods to add a Conda Environment to Jupyter’s Notebook, but we’ll discuss easy ones that anyone in the tech industry can use. Let’s note down the first one.

  1. First, open your terminal or Anaconda prompt and activate the Conda environment you want to add to Jupyter Notebook using the command:[conda activate env_name].
  2. Now, install the ipykernel package in the activated environment using the command:
  3. [conda install ipykernel]
  4. Once done with the above step, run this command to add the environment to Jupyter Notebook: [python -m ipykernel install --user --name env_name --display-name "Python (env_name)"]
  5. Now time to launch Jupyter Notebook by typing a command in your terminal or Anaconda prompt: [jupyter notebook]
  6. When the Jupyter Notebook is launched, you can create a new notebook in the environment you just added by selecting “New” in the top right corner and selecting the environment from the dropdown menu.

Another popular method

  1. First, open your terminal or Anaconda prompt, and create a new Conda environment using [conda create --name env_name]. You can replace  env_name with the name you want.
  2. Now you need to activate the new environment by using the [conda activate env_name] command
  3. Install the Jupyter Notebook using the [conda install jupyter] command.
  4. Once Jupyter Notebook is installed, you can launch it by typing [jupyter Notebook]
  5. Now it’s time to create a new notebook in Jupyter by selecting the “New” button in the top right corner and choosing the Conda environment you just created.

How to Create New Conda Environment in Jupyters Notebook

Nothing can be easier than creating a New Conda Environment In Jupyter’s Notebook. Here are some easy peasy steps you can follow right after reading the post.

  1. First, launch Jupyter Notebook by typing this command [jupyter notebook]  in your terminal or Anaconda prompt
  2. Once the Jupyter Notebook is launched, click on “New” in the top right corner and select “Terminal” to open a new terminal window.
  3. Now, in this window, create a new Conda environment using the following command:
  4. [conda create --name env_name]
  5. Now you have to  activate the new environment by typing: [conda activate env_name]
  6. After activating the environment, it’s time to install any packages or dependencies you need for your project using the [ conda install ] command.
  7. Once you have installed the necessary packages, you can create a new Jupyter Notebook in your new environment by clicking on “New” in the top right corner and selecting “Python [env_name]” from the dropdown menu. That’s all.

Activating and Deactivating Conda Environment in Jupyter’s Notebook

Now guess what? You can take your package management in Jupyter Notebook to the next level by mastering the essential skills of activating and deactivating Conda environments.

Steps To activate a Conda Environment in Jupyter Notebook:

  1. First of all, Open the Jupyter Notebook and steer to the notebook that you want to work in the Conda environment.
  2. Now, type [!conda activate env_name] to activate the Conda environment in a new code cell,
  3. You can now run the cell by hitting “Shift+Enter.”
  4. Once the cell finishes running, the environment will be activated, and you can start working on it. Isn’t it something really simple?

Steps To deactivate a Conda Environment in Jupyter Notebook:

  1. Again, type [!conda deactivate] in a new code cell and run the cell by hitting “Shift+Enter.”
  2. Once the cell finishes running, the active environment will be deactivated, and you will be returned to the base environment.

It’s important to note that activating an environment in Jupyter Notebook only affects the current notebook session. If you want to use the environment in other notebooks, you must activate it in each notebook separately.

How to install Packages in the Conda Environment?

Installing packages in Conda environments is a straightforward process. Here’s how to install packages in a Conda environment:

  • Open your terminal or Anaconda prompt and activate the Conda environment where you want to install the package. Copy and paste this command as it is. [conda activate env_name]
  • Once you have completely activated the environment, you can install packages using the conda install command with your desired name of the package to install. For example, to install the panda’s package, you would type: [conda install pandas]
  • Hit enter to execute the command. Conda will then start its checks for dependencies and prompt you to confirm the installation of the package and any necessary dependencies. Type “y” and tap enter button to proceed with the installation.
  • Once the package is installed, you can verify this installation by importing it in a Python script or Jupyter Notebook.

Congrats! Packages have been installed successfully.

How to Register a Conda Environment with Jupyter’s Notebook

Registering a Conda environment with Jupyter Notebook allows you to use the environment within Jupyter Notebook. Here’s how to register a Conda environment with Jupyter Notebook. 

  1. As a first and foremost step, open your terminal or Anaconda prompt.
  2. Now instantly activate the Conda environment you want to register with Jupyter Notebook by [conda activate env_name]
  3. Install the [ipykernel] package using [conda install ipykernel]. Wait for it until installed. 
  4. Now you have to run the following command to register the environment with Jupyter Notebook:
  5. [python -m ipykernel install --user --name env_name --display-name "Custom name"]
  6. Replace [env_name] with the environment name you want to register, and replace “Custom name” with the name you want for the environment in Jupyter Notebook.
  7. Press the enter button to implement the command, and once done, the environment will be registered with Jupyter Notebook. 

Selecting and changing the kernel in Jupyter Notebook

Two best yet easy methods exist for selecting and changing the kernel in Jupyter Notebook. Let’s Go through them both one by one. 

Method 1: Using the notebook interface

  1. Open Jupyter Notebook.
  2. Navigate to the notebook that you want to change the kernel for.
  3. Look for “Kernel” in the top menu bar and click on it.
  4. Now find “Change kernel” from the dropdown menu and click.
  5. Here you have a new window showing a list of available kernels. Select the kernel you want to use from the list.
  6. Click “Change” to change the kernel for the notebook.
  7. Once the kernel is changed, you can start working in the new kernel environment.

Method 2: Using the command line

  1. Open your terminal or Anaconda prompt.
  2. Now activate the Conda environment you need to use as the kernel using the [conda activate env_name] command
  3. Install the ipykernel package in the environment using the following command:
  4. Conda install ipykernel
  5. Once the ipykernel package is installed, run below mentioned command to add the Conda environment as a kernel for Jupyter Notebook:
  6. python -m ipykernel install –user –name env_name –display-name “Custom name”
  7. Press enter, and your Conda environment will be added as a kernel for Jupyter Notebook.
  8. Open Jupyter Notebook and navigate to the notebook you want to change the kernel for.
  9. Click on “Kernel” in the top menu bar.
  10. Now choose the kernel you just added from the dropdown menu and start working as you need. 

Using different Conda environments in Jupyter Notebook

Learned so much about adding Conda environments in Jupyters notebook, but still needed clarification about how to use different environments? Below is your go-to guide.

  1. Open your terminal or Anaconda prompt.
  2. Install the nb_conda package in the base environment using [conda install nb_conda]
  3. Once the package is installed, launch Jupyter Notebook.
  4. In Jupyter Notebook, navigate to the notebook for which you want to use a different Conda environment.
  5. Click on “Kernel” in the top menu bar.
  6. Select “Change kernel” from the dropdown menu.
  7. A new window will open here, showing a list of available kernels. Select the Conda environment you want to use from the list.
  8. Click “Change” to change the kernel for the notebook.
  9. Once the kernel changes, you can start working in the new Conda environment.

Benefits of Using Conda Environments in Jupyter Notebook

Using Conda environments in Jupyter Notebook may sound common, but it has some amazing benefits that may shock you. Let’s explore some of them.

Organize Your Workflows

With Conda environments, you can isolate the packages and dependencies for each project separately, making it easy to operate and organize your workflows. Now you don’t have to stress about conflicts between projects or packages and can easily switch between different environments as needed.

More Control Over Package Versions

Conda environments give you more control over your package versions, making it easier to reprise your results and share your code with others. You can install specific versions of packages or even create virtual environments to test new packages without impacting other projects.

Easy to Share and Collaborate

Conda environments make it simple yet fun to communicate your code and collaborate with others. You can easily export and import environments, making it easy for others to replicate your project setup. Plus, with Conda, you can easily install packages from public or private repositories, making it easier to share your code with others.

Long Story Short

In data science, adding Conda environments to Jupyter Notebook is like having a secret weapon in your toolkit. With these few simple steps, you can unlock the power of Conda’s package management capabilities and take your workflows in Jupyter Notebook to the next level. 

So go forth and conquer the world of data with the full force of Conda and Jupyter Notebook at your fingertips!

Scroll to Top