Using conda with Miniforge to manage environments
Intended for:
Conda users
Scenario/Use case:
Use conda with Miniforge to manage software environments
Instructions:
conda is a command-line executable that can be used to manage software environments. It also manages the installation of software into such environments. It primarily is used to support the installation of Python-based software, but it is not limited to that. It can be used in conjunction with pip but care needs to be taken when doing so.
Usage of Anaconda or Miniconda
Anaconda is free for use at Fermilab.
Install using Miniforge
Miniforge provides an installation of conda for Windows, Linux, and macOS. For all platforms, the user-level installation seems best for many users. That is what is described here.
On MacOS, Homebrew installation of Miniforge is not recommended because when Homebrew updates Miniforge, all environments are lost. Instead, it is recommended to use the shell installer for miniforge-conda; this does an installation under $HOME which automatic updates do not mangle. The installed version of conda can be updated with the usual conda update conda command.
Creating a new installation to match an existing one
The most convenient way to create a new conda environment to match an existing one is to use the conda env export function as shown below. However, three important cautions must be kept in mind.
- Remove any mention of the defaults channel in the output YAML file.
- Edit the path to the environment to that which you want to use.
- Make sure the name of the environment matches the prefix path you provide if the path is into one of the ones that conda manages.
The command to generate the YAML file (which you must edit, as noted above) is:
# Use either "-n environment-name" or "-p path-to-environment" to identify the environment you want to copy
conda env export --from-history [-n <environment-name> | -p <path-to-environment>] > environment.yaml
The --from-history is critical. That is the flag that limits the report to include only those packages that were directly requested during the creation (or updating) of the environment. When moving to a different OS, or from Anaconda to Miniforge, the detailed list of dependencies calculated by conda can vary. By including only what was directly requested, this allows conda to find the set of supporting packages that are needed and consistent.
Before creating the new environment, edit the environment.yaml file as noted above. To create the new environment, based on that YAML file, the command is:
conda env create -f environment.yaml
Cautions
Pay attention to .condarc files
A centralized installation of any version of conda includes a site-level configuration file .condarc somewhere in the system-managed directories. The command conda info will tell you where this is (look for "populated config files"; it is typically the first one listed). You probably also have a user-level configuration file (typically at $HOME/.condarc. Finally, each environment might also contain a .condarc file specific to that channel.
To avoid entanglements with licensed software from Anaconda, Inc., you need to make sure none of your .condarc files includes the channel defaults. This channel contains packages curated by Anaconda, Inc., and the use of this channel is controlled by their licensing terms.