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)
- 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.
- Create a directory to store the exported environments by executing
the following command:
- Activate the environment.
Activate the environment you wish to export:
conda activate <conda_env_name># or, for older conda:source activate <conda_env_name> - 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-filesNote:
Do not use this method for custom environments without prior validation. - Deactivate the Environment.
Deactivate the environment after packing:
conda deactivate# or, for older conda:source deactivate - 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.
- If the conda-pack is not installed, run:
Target Environment (Import Steps):
- Transfer the environment archive.
Copy the exported .tar.gz file to the target server (For example, use scp or another file transfer tool).
- Prepare the target directory.
- Log in to the target server.
- Navigate to the <<miniconda3>>/envs directory:
cd <<miniconda3>>/envs - Create a directory for the environment to be imported:
mkdir <Env_to_be_imported># For example:
mkdir ml4aml_8.1.2.6.0 - Rename the existing tar files if present, to avoid overwrites.
- 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> - Test the python executable.
- Before activating, you can test the environment's
python:
./<Env_to_be_imported>/bin/python - You must see the python prompt; exit with quit().
- Before activating, you can test the environment's
python:
- Activate the environment.
- Activate the imported environment:
source <Env_to_be_imported>/bin/activateNote:
Your prompt must now reflect the activated environment.
- Activate the imported environment:
- Verify the python version.
Confirm you are running python from within the activated environment:
python - Cleanup prefixes.
Fix any hardcoded prefixes in the environment:
conda-unpackThis step can be performed either within the activated environment or by specifying the path to the python binary.
- 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 - List the conda environments.
Confirm the new environment appears in your environment list:
conda env list - Enroll or use the environment as needed.
Proceed with any application-specific enrollment steps.