On this page:
Table of Contents |
---|
...
Conda is a tool commonly used to manage dependencies and projects. You can create a conda environment for each project and install the packages that you need for each projectin that conda environment. 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 on JupyterLab in IDAS.
Set up
Example: Create a conda environment with a Python kernel
1. First,
...
log in to the IDAS research instance with Python
...
. If you are a student in a class that uses Python in IDAS, follow the instructions here
...
to access your class instance.
...
In the top right corner of your IDAS home page, click New.
...
2a. In JupyterLab, click the Terminal tile under Other to start a Terminal session.
...
2b. If this is your first time using conda in the Terminal in IDAS, initialize your shell
...
Code Block |
---|
conda init bash |
...
by running the following command in the Terminal.
This command only needs to be run once. It will create a .bashrc
file in your IDAS home directory
...
if a .bashrc
doesn’t already exist.
Code Block |
---|
conda init bash |
2c. Close the Terminal by clicking the x
in the upper right corner of the Terminal window.
2d. Click the Terminal tile to open the Terminal again.
2e. Run the following command in the Terminal. This will source the .bashrc
file that was just created
...
.
Code Block |
---|
source ~/.bashrc |
The command prompt in your Terminal will change to indicate the active environment, which is the
...
base
...
environment by default.
Code Block |
---|
(base) |
...
HawkID@idas-research- |
...
HawkID:~$ |
...
|
Info |
---|
Every time you open a new Terminal session, you’ll have to run |
...
. |
3. In Terminal, the conda create
command can be used to create a new conda environment.
In this example, we will create a conda environment with Python 3.8
...
and name the environment
...
py38
...
.
Code Block |
---|
conda create -n py38 python=3.8 |
When prompted
...
Proceed ([y]/n)?
...
, press
...
y
...
to proceed.
Additional notes:
The conda create
command can be modified to fit your needs. Below are a few additional examples:
Additional example 1: Install packages when creating an environment:
Code Block |
---|
conda create -n another-env python=3.8 numpy requests |
Additional example 2: Specify the versions of the packages:
Code Block |
---|
conda create -n another-env python=3.7 numpy=1.16.1 requests=2.19.1 |
Additional example 3: Create a conda environment from an environment.yml file:
Code Block |
---|
conda env create -f environment.yml |
Info |
---|
For more examples of creating conda environments, please see the section "Managing environments" in the Conda User Guide. |
4. Once this conda environment has been created, we can activate it.
Code Block |
---|
conda activate py38 |
5. The command prompt in your Terminal will change to indicate the active environment.
Code Block |
---|
(py38) |
...
hawkid@idas-research-hawkid:~$ |
6. Next, we create a kernel in order to use Jupyter Notebook
...
with this conda environment. Install the IPython kernel:
Code Block |
---|
conda install ipykernel |
When
...
prompted Proceed ([y]/n)?
...
, press
...
y
...
to proceed.
7. Now install a kernel in this environment:
Code Block |
---|
python -m ipykernel install --user --name py38 --display-name "Python Project |
...
Vis" |
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 in the JupyterLab Launcher page.
...
8. Go back to the JupyterLab Launcher page by pressing Ctrl+Shift+L
(Windows) or Cmd+Shift+L
(Mac). Under Notebook, a new option for your kernel will now be available. In this example screenshot, the "Python Project Vis" kernel was just installed and now became available to use.
Click on that new option to start a notebook. In that notebook, you can use the packages that you installed in the conda environment.
...
9. To install Python packages to this conda environment using a Jupyter notebook:
a. Start a new Jupyter notebook by selecting the Python kernel you just installed. In this example, that is the "Python Project Vis" kernel in the previous step.
b. 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:
c. Then we can load and use the package as usual:
...
Once we are finished working with the current conda environment, we can deactivate it by typing the following in Terminal:
Code Block |
---|
conda deactivate |
...
10. We can also install Python packages to this conda environment using the Terminal:
Code Block |
---|
conda install --name py38 pandas |
Info |
---|
For more information on searching for and installing packages, see the section “Managing packages” in the Conda User Guide. |
Example: Create a conda environment with an R kernel
1. First, log in to the IDAS research instance with Python. If you are a student in a class that uses Python in IDAS, follow the instructions here to access your class instance.
2a. In JupyterLab, click the Terminal tile under Other to start a Terminal session.
...
2b. If this is your first time using conda in the Terminal in IDAS, initialize your shell by running the following command in the Terminal.
This command only needs to be run once. It will create a .bashrc
file in your IDAS home directory if a .bashrc
doesn’t already exist.
Code Block |
---|
conda init bash |
2c. Close the Terminal by clicking the x
in the upper right corner of the Terminal window.
2d. Click the Terminal tile to open the Terminal again.
2e. Run the following command in the Terminal. This will source the .bashrc
file that was just created.
Code Block |
---|
source ~/.bashrc |
The command prompt in your Terminal will change to indicate the active environment, which is the base
environment by default.
Code Block |
---|
(base) HawkID@idas-research-HawkID:~$ |
Info |
---|
Every time you open a new Terminal session, you’ll have to run |
3. In Terminal, the conda create
command can be used to create a new conda environment.
In this example, we will create a conda environment with R and name the environment r-env
.
Code Block |
---|
conda create -n r-env r-essentials r-base |
When prompted Proceed ([y]/n)?
, press y
to proceed.
Additional 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 ther-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.
4. Once this conda environment is created, we can activate it.
Code Block |
---|
conda activate r-env |
5. The command prompt in your Terminal will change to indicate the active environment.
Code Block |
---|
(r-env) hawkid@idas-research-hawkid:~$ |
6. 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:
Code Block |
---|
(r-env) hawkid@idas-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 |
7. Now install a kernel in R:
Code Block |
---|
# 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 displayname
will be displayed in the Notebook menu in JupyterLab.
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.
8. Go back to the JupyterLab Launcher page by pressing Ctrl+Shift+L
(Windows) or Cmd+Shift+L
(Mac). Under Notebook, a new option for your kernel will now be available, with the name "R 3.6.1"
Click on that new option to start a notebook. In that notebook, you can use the packages that you installed in the conda environment.
9. To install R packages in this conda environment, in Terminal:
Code Block |
---|
conda install --name r-env r-abind |
10. 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 Terminal. You don't need to refresh the notebook.
...
11. 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 Terminal:
Code Block |
---|
conda list -n r-env |
12. To view a list of installed packages in Jupyter 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" and "Managing packages" section sections in the Conda User Guide.
List all of your environments. In the output, your current environment will be marked with an asterisk (*).
Code Block |
---|
conda info --envs |
or
Code Block |
---|
conda env list |
To activate a conda environment that was previously created, type (without the angle brackets):
Code Block |
---|
conda activate <environment-name> |
Deactivate a conda environment once you are finished working with it:
Code Block |
---|
conda deactivate |
Or, to return to the base
environment, type (with no environment specified):
Code Block |
---|
conda activate |
View a list of packages installed in an environment:
If the environment is not activated, type (without the angle brackets):
Code Block |
---|
conda list -n <environment-name> |
If the environment was already activated with
...
conda activate
...
earlier
...
:
Code Block |
---|
conda list |
To see if a specific package is installed in an environment, type (without the angle brackets):
Code Block |
---|
conda list -n <environment-name> <package-name> |
Or, if the environment was already activated with conda activate
earlier, type (without the angle brackets):
Code Block |
---|
conda list <package-name> |
To install a package in an environment:
Code Block |
---|
conda install --name <environment-name> <package-name> |
Remove an environment:
Code Block |
---|
conda remove --name <environment-name> --all |
or
Code Block |
---|
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.