In this page:
Conda is a tool commonly used to manage dependencies and projects. You can create a conda environment and install the packages that you need for each project. If you make changes to one of your conda environments, your other environments are not affected. For more information, please refer to the Conda User Guide.
The following examples illustrate common conda tasks using JupyterHub in IDAS.
Set up
First, create an IDAS instance with Python, R, or Julia. If you are a student in a class that uses IDAS, follow the instructions here to access your class instance.
In the top right corner of your IDAS home page, click New.
Click Terminal in order to start a Terminal session.
Example: Create a conda environment with a Python kernel
- First, create a Terminal session using the steps in the "Set up" section above.
In this example, we will create a conda environment with Python 3.8 and name the environment "py38".
conda create -n py38 python=3.8
When prompted "Proceed ([y]/n)?", press "y" to proceed.
Side notes:
- You can also install packages when creating an environment, e.g.: conda create -n another-env python=3.8 numpy requests
- And you can specify the versions of the packages, e.g.: conda create -n yet-another-env python=3.7 numpy=1.16.1 requests=2.19.1
- You can also create a conda environment from an environment.yml file, e.g,
conda env create -f environment.yml
- For more examples of creating conda environments, please see "Managing environments" in the Conda User Guide.
Once this conda environment is created, we can activate it.
conda activate py38
The command prompt in your Terminal will change to indicate the active environment.
(py38) hawkid@jupyter-notebook-research-hawkid:~$
Next, we create a kernel in order to use Jupyter Notebook with this conda environment. Install the IPython kernel:
conda install ipykernel
When prompted "Proceed ([y]/n)?", press "y" to proceed.
Now install a kernel in this environment:
python -m ipykernel install --user --name py38 --display-name "Python 3.8"
The value for "--name" is used by Jupyter internally. Any existing kernel with the same "--name" value will be overwritten. The "--display-name" will be displayed in the Notebook menu.
- Reload the IDAS home page. Under New / Notebook, an option called "Python 3.8" will now be available.
- To install Python packages in this conda environment, first start a new Jupyter Notebook by selecting New / Python 3.8.
- In a cell in this new notebook, use the %conda magic that is built into IPython to install packages within the current kernel. For example, install the "pandas" package:
- Then we can load and use the package as usual:
Example: Create a conda environment with an R kernel
- First, create a Terminal session using the steps in the "Set up" section above.
In this example, we will create a conda environment with R and name the environment "r-env".
conda create -n r-env r-essentials r-base
When prompted "Proceed ([y]/n)?", press "y" to proceed.
Side notes:
- You can create an R conda environment with just the "r-base" package, like so: conda create -n r-env r-base
- However, the "r-essentials" package includes approximately 80 of the most popular packages for R, so it is convenient to install the "r-essentials" bundle rather than installing each individual package later.
- Many R packages are available to install with conda, but not all. The latest index of R packages built by Anaconda, Inc. can be found on Anaconda Cloud or at http://repo.anaconda.com/pkgs/r/
- For more information about using R with conda, please see Using R language with Anaconda and R language packages for Anaconda in the Anaconda documentation.
- For more examples of creating conda environments, please see "Managing environments" in the Conda User Guide.
Once this conda environment is created, we can activate it.
conda activate r-env
The command prompt in your Terminal will change to indicate the active environment.
(r-env) hawkid@jupyter-notebook-research-hawkid:~$
Next, we create a kernel in order to use Jupyter Notebook with this conda environment. We will install a kernel through IRkernel. The r-irkernel package was already installed with the r-essentials bundle earlier. You can check to make sure you have r-irkernel in your conda environment:
(r-env) hawkid@jupyter-notebook-research-hawkid:~$ conda list irkernel # packages in environment at /home/hawkid/.conda/envs/r-env: # # Name Version Build Channel r-irkernel 0.8.15 r36_0 defaults
Now install a kernel in R:
# launch R in Terminal R > library(IRkernel) > IRkernel::installspec(name = "ir361", displayname = "R 3.6.1")
The value for "name" is used by Jupyter internally. Any existing kernel with the same "name" value will be overwritten. The "display-name" will be displayed in the Notebook menu.
Note that we specified the R version (3.6.1) in the "name" and "displayname". This helps remind us what R version we are using, especially if we have multiple conda environments and kernels.
- Reload the IDAS home page. Under New / Notebook, an option called "R 3.6.1" will now be available.
- To install R packages in this conda environment, in the Terminal:
# make sure we are in the right environment conda activate r-env # then install the package we want conda install r-abind
- Then you can load and use the package in Jupyter Notebook as usual. If you have an open notebook, you can use the package in the notebook right after you install the package in the Terminal. You don't need to refresh the notebook.
In general, any packages that are installed in the conda environment will be available to use in the notebook. To view a list of installed packages in the conda environment, type in the Terminal:
conda list -n r-env
- To view a list of installed packages in the notebook, use the installed.packages() function:
Other useful conda commands
The following commands may be useful to manage conda environments. Type the following commands in Terminal. For more information, please refer to the "Managing environments" section in the Conda User Guide.
List all of your environments. In the output, your current environment will be marked with an asterisk (*).
conda info --envs
or
conda env list
To activate a conda environment that was previously created, type (without the angle brackets):
conda activate <environment-name>
Deactivate a conda environment once you are finished working with it:
conda deactivate
Or, to return to the "base" environment, type (with no environment specified):
conda activate
- View a list of packages installed in an environment:
If the environment is not activated, type (without the angle brackets):
conda list -n <environment-name>
If the environment was already activated with "conda activate" earlier:
conda list
To see if a specific package is installed in an environment, type (without the angle brackets):
conda list -n <environment-name> <package-name>
Or, if the environment was already activated with "conda activate" earlier, type (without the angle brackets):
conda list <package-name>
Remove an environment:
conda remove --name <environment-name> --all
or
conda env remove --name <environment-name>
Using pip in a conda environment
Please note that issues may arise when using pip and conda together. The "Using pip in an environment" section in the Conda User Guide outlines best practices for using pip in a conda environment.
Contact
If you have any questions or comments, please contact research-computing@uiowa.edu.