How to install packages using Conda on Jupyter notebook

Yashasvika Duggal
  • Updated

Jupyter Notebooks come with pre-configured environments that include software commonly used for research analyses. However, these may not include all of the packages that are necessary for a given analysis. Conda is a package management system to find and install the packages you need. 

Overview

Conda is a cross-platform package management system that works on Windows, MacOS, and Linux. Conda is used to install software packages and their dependencies. As a package manager, it can help you find and install packages.

By default, Jupyter Notebooks come with a pre-configured base environment that includes software most researchers will use for their analyses. However, sometimes you will need to add, remove, or update packages to better suit your needs. To do that, you will need to configure a new conda environment where you can fully customize the installed software. 

How to install conda

You can install conda and keep it on a detachable persistent disk. 
1. In your Jupyter Notebook, run the following code, substituting in a name for your new environment for name_of_new_environment (this may take 2-4 minutes to complete)

 conda create --clone base --prefix /home/jupyter/name_of_new_environment -y 

Screenshot showing the code used to create a new environment called 'newEnvironment' in a Jupyter notebook, using Conda.

Note: The -y is important. Since the output is non-interactive, this will respond “y” to any prompts. Without the flag, the notebook will hang, and you will need to restart the kernel.

2. To see the new environment, refresh the webpage

3. Verify that you're in the correct environment by going to the Kernel menu on the toolbar, choosing Change Kernel, and selecting Python[conda:env:name_of_new_environment]. For example, if you named your new environment newEnvironment, you would select Python[conda:env:newEnvironment].

Screenshot showing how to select the newly-created environment. Click on the 'Kernel' menu on the toolbar at the top of the notebook, then hover over 'Change kernel' and select the Python environment with your new environment's name. In this example, that environment is called 'Python[conda:env:newEnvironment].' Orange rectangles highlight each of these steps within the 'Kernel' menu.

Now you should see the name of your new kernel displayed at the top of the notebook:

Screenshot showing the name of the newly-created kernel at the top of an example notebook. An orange rectangle highlights the kernel's name.

4. Once you have selected your new kernel, use the following code to customize your environment by adding a package (substituting the name of the package you want to install for "package name"): 

!conda install package name

You can also use conda to remove packages from your notebook's environment, or change a package's version. See Conda's documentation for more details on these commands.

5. Import the packages that you add in order to start using them in your notebook:

import package name 

Note: When installing conda on a Jupyter instance, keep in mind the configuration of your persistent disk. If you delete your Cloud Environment while retaining your persistent disk, you will still get rid of conda.

Screenshot showing the window that appears when you delete your Cloud Environment's Persistent Disk. The window provides a choice between deleting the Cloud Environment, but not the Persistent Disk; and deleting both the Cloud Environment and the Persistent Disk.

To learn more about setting up your persistent disk, read How to set up persistent disk storage for your analysis app

Was this article helpful?

1 out of 5 found this helpful

Comments

0 comments

Please sign in to leave a comment.