Table of Contents

Conda Environments in Codio

Pavel Dimens Updated by Pavel Dimens

Conda Environments in Codio

Rather than creating an entirely new stack to accommodate certain software configurations (e.g. package versions, python versions), it may be a lot simpler to create a conda environment and generate a custom kernel from it to use in Jupyter Notebooks without the need for creating and maintaining a new stack. 

Mamba is recommended over conda, especially inside Codio environments. It is orders of magnitude faster at resolving dependency trees and uses substantially less RAM to do so. The instructions below install mambaforge, which includes both conda and mamba.

Installing conda/mamba

To install conda:

  1. Find the appropriate Linux installer on the mambaforge repository, where “appropriate” reflects the operating system and processor architecture. If doing this for Codio, it will be this installer, for Linux amdx86_64.
  2. Copy the URL of the download and in the Codio unit, download the file with wget
# with wget
wget https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-Linux-x86_64.sh

# or with curl
curl -LO https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-Linux-x86_64.sh
  1. Once it’s downloaded, run the installer
bash Mambaforge-Linux-x86_64.sh
  1. Follow the prompts. The defaults are fine, but for Codio stack management, it would be easier to change the install directory to /home/codio/conda (or /mamba). Say yes to initializing conda for your shell.
  2. Exit the terminal session and that should be enough to load conda at startup. If it doesn't work, reboot the box and it will be there. You should notice nothing, but the conda commands should now work. Makes sure the base environment doesn't activate at shell login.
conda config --set auto_activate_base false

And that's it! From this point onwards, you can substitute occurrences of conda with mamba.

Create a conda environment

conda environments are created with the command 

conda create -n env_name
# more verbose
conda create --name env_name

Where env_name is what you want the name of the environment (and subsequent Jupyter kernel) to be. For the sake of example, let’s create a new environment named BTT

conda create -n BTT

It will create the environment and tell you how to access it (conda activate BTT). After activating the environment, you can install whatever software you need (feel free to browse the Anaconda.org site for existing package recipes). As an example, let’s install python3.9 and scikit-learn. You can specify a version with the equal sign (=) if necessary.

conda activate BTT
conda install python=3.9 scikit-learn

It will go through the motions of resolving dependency trees and finally install the software if there are no conflicts. More instructions are available in the conda docs, like specifying channels. You can also install packages at the same time as creating an environment, just combining the two syntaxes:

conda create -n BTT python=3.9 scikit-learn 

Specifying channels

You can search for software available through anaconda on the website https://anaconda.org/. It will tell you what "channel" the package is in, the versions available, and how to install it. If ever you need to include a channel specification when installing, it's done by adding the -c flag, followed by the name of the channel. This option can be listed multiple times. Example:

conda install -c conda-forge -c bioconda samtools

This would include the conda-forge and bioconda channels when searching for samtools and its dependencies.

Loading a conda environment at login

If the students are working with command line tools rather than Jupyter Notebooks, you may want/need them to have a particular conda environment loaded at the start of the session. To do this in a way where stack-level changes aren't necessary, we need to add conda activate env_name to a terminal tab in the Guide Editor→Layout.

Integrating conda to create custom Jupyter Kernels

We can extend this further by creating a Jupyter kernel that will use a specified conda environment, meaning we can choose the appropriate software environment on a per-notebook basis.

  1. Your environment will need ipykernel installed into it. You should already have activated the environment you want to install this package into.
# conda activate BTT
conda install -c anaconda ipykernel
  1. Followed by installing your BTT environment as a kernel. You should still have your target environment active.
python -m ipykernel install --user --name=BTT

After this, you should be able to choose the BTT kernel in Jupyter and use all the software/packages present in the BTT conda environment (you may need to reboot the machine to restart the Jupyter server).

Jupyter Notebook menu showing Python3 and BTT kernel options

How did we do?

Using the RStudio Starter Pack

Contact