Creating a Conda Environment

Why Should I Use Conda?

Conda is an open-source package and environment management system that simplifies software installation and version control. With Conda, you can:

  • Install and manage software (including Python and R packages) along with all their dependencies

  • Create isolated environments so different projects can use different versions of the same software without conflicts.

  • Reproduce analyses easily since an environment file (environment.yml) can capture the exact software versions used in your workflow.

This makes conda especially useful for computational biology and single-cell RNA-seq analysis where software dependencies can be complex and version-sensitive.

NoteConda vs renv (Python vs R origins)

Although Conda was originally popularized for Python workflows, it can also manage R environments (and many non-Python tools).
However, for R-specific projects, many users prefer renv for its tight integration with R and CRAN/Bioconductor packages.
In mixed R + Python projects, a common pattern is:

  • Use renv to lock down R packages for the R portion of the analysis, and
  • Use conda (or another Python environment manager) for Python dependencies (often accessed from R via reticulate). If your workflow includes both R and Python components, you may find it helpful to use both tools in tandem. To learn more about renv, see our renv tutorial.
NoteInstalling Conda

To install Conda, follow the Miniconda installation instructions.
Miniconda is the lightweight version of Anaconda and is recommended for most users.

Create a New Environment

There are many ways to organize your Conda environments, depending on your workflow and software dependencies. As a general rule, you should create a dedicated Conda environment for each project to prevent version conflicts and ensure reproducibility.

To create a conda environment with Python, type into the command line:

conda create --name myenv python

and replace myenv with your desired environment name. Then, to activate your environment:

conda activate myenv

Now, anything you run in this shell will use only the packages that you have explicitly installed. To deactivate your environment, run:

conda deactivate

Installing a Package with Conda

To install a package with Conda, you usually have to specify a channel — an online repository where packages are stored and distributed. When you install a package with Conda, it looks for that package in one or more channels (for example, defaults, conda-forge, or bioconda).

  • defaults — maintained by Anaconda; includes general-purpose packages.

  • conda-forge - a community-maintained channel providing up-to-date, cross-platform scientific software.

  • bioconda — specializes in bioinformatics software and builds on conda-forge for its dependencies.

When installing bioinformatics tools like kb-python or seqspec, it’s best practice to specify both conda-forge and bioconda as channels to ensure compatibility:

conda install -c conda-forge -c bioconda kb-python

How to Save and Restore an Old Environment

You can save your conda environment in a YAML file with:

conda env export --from-history > environment.yml

The --from-history flag saves only the packages you explicitly installed, keeping the file concise. If you prefer to include all dependencies and versions, omit this flag.

To recreate the same environment elsewhere:

conda env create -f environment.yml --name myenv

Using Conda Environments with JupyterLab and Jupyter Notebooks

Most Scanpy workflows are run interactively in JupyterLab or Jupyter notebooks. To use a conda environment in JupyterLab, you need to register it as a kernel. This allows you to select that environment when creating or running notebooks.

Registering a conda environment as a Jupyter kernel

First, from your project environment:

conda activate myenv
conda install -c conda-forge ipykernel
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"

Then start JupyterLab from wherever you normally run it. The Python (myenv) kernel will still be available.

Quick sanity checks

Inside a notebook cell, confirm you are using the expected environment:

import sys
print(sys.executable)

You can also confirm that Scanpy is being imported from the environment you expect:

import scanpy as sc
print(sc.__version__)

Removing a kernel (optional)

If you no longer need the kernel registration:

jupyter kernelspec list
jupyter kernelspec uninstall myenv