Conda Environment Migration and Restoration

Use this process when migrating or restoring Conda environments from a reliable source (such as production) to a sandbox instance

Source Environment (Export Steps)

Perform the following steps:
  1. Prepare the backup directory:
    • Create a directory to store the exported environments by executing the following command:

      mkdir conda_export

    • Ensure that the mount point has sufficient storage.
  2. Activate the environment.

    Activate the environment you wish to export:

    conda activate <conda_env_name>

    # or, for older conda:

    source activate <conda_env_name>

  3. Export the Environment.

    Pack the active conda environment (ignore missing files):

    conda pack -n <conda_env_name> -o <conda_env_name>.tar.gz --ignore-missing-files

    Note:

    Do not use this method for custom environments without prior validation.
  4. Deactivate the Environment.

    Deactivate the environment after packing:

    conda deactivate

    # or, for older conda:

    source deactivate

  5. Pre-requisite - Install the conda-pack (if it is missing).
    • If the conda-pack is not installed, run:

      pip install conda-pack

    • Use index-url or extra-index-url if your repository requires them.

Target Environment (Import Steps):

Perform the following steps to transfer the environment archive:
  1. Transfer the environment archive.

    Copy the exported .tar.gz file to the target server (For example, use scp or another file transfer tool).

  2. Prepare the target directory.
    1. Log in to the target server.
    2. Navigate to the <<miniconda3>>/envs directory:

      cd <<miniconda3>>/envs

    3. Create a directory for the environment to be imported:

      mkdir <Env_to_be_imported>

      # For example:

      mkdir ml4aml_8.1.2.6.0

    4. Rename the existing tar files if present, to avoid overwrites.
  3. Untar the environment.

    Extract the archive contents into the newly created environment directory:

    tar -xzf <Env_to_be_imported>.tar.gz -C <Env_to_be_imported>

  4. Test the python executable.
    1. Before activating, you can test the environment's python:

      ./<Env_to_be_imported>/bin/python

    2. You must see the python prompt; exit with quit().
  5. Activate the environment.
    • Activate the imported environment:

      source <Env_to_be_imported>/bin/activate

      Note:

      Your prompt must now reflect the activated environment.
  6. Verify the python version.

    Confirm you are running python from within the activated environment:

    python

  7. Cleanup prefixes.

    Fix any hardcoded prefixes in the environment:

    conda-unpack

    This step can be performed either within the activated environment or by specifying the path to the python binary.

  8. Register the imported environment.

    Add the environment path to your environment tracking file (optional but recommended for automation):

    echo "<<miniconda3>>/envs/<Env_to_be_imported>" >> ~/.conda/Environment.txt

  9. List the conda environments.

    Confirm the new environment appears in your environment list:

    conda env list

  10. Enroll or use the environment as needed.

    Proceed with any application-specific enrollment steps.