Here's a comprehensive step-by-step guide to export and recreate a Conda environment on a different machine, ensuring all dependencies and configurations are transferred correctly:
Activate the Conda Environment You Want to Export:
Open your terminal and activate the environment. Replace your_env_name
with the name of your environment:
conda activate your_env_name
Export the Environment to a YAML File:
Use the conda env export
command to create a YAML file named environment.yml
:
conda env export --name your_env_name --no-builds > environment.yml
-no-builds
ensures that the YAML file doesn't include platform-specific build information, making it portable across different operating systems.environment.yml
file will contain all the packages, versions, and dependencies required for your environment.Verify the Contents of environment.yml
File (Optional):
Open the environment.yml
file to ensure there are no absolute paths or unnecessary build information. Make sure it only includes the necessary packages and their versions:
name: your_env_name
dependencies:
- python=3.8.5
- numpy=1.19.2
- pandas=1.1.3
- matplotlib=3.3.2
...
Create an Exact List of Installed Packages (Optional):
If you want to ensure an exact recreation of the environment with the same builds and versions, create a spec-list.txt
file:
conda list --explicit > spec-list.txt
This file will include the exact URLs of package versions in the environment, ensuring precise replication.
Copy the environment.yml
and/or spec-list.txt
File to the New Machine:
Use your preferred method to transfer the files. For example, you can use SCP to copy the file if both machines are on the same network:
scp environment.yml user@new_machine:/path/to/destination
Replace user@new_machine
and /path/to/destination
with appropriate values.
Download and Install Anaconda or Miniconda:
Install Anaconda/Miniconda from the official website:
Verify the Installation:
After installation, check that Conda is correctly installed:
conda --version
Update Conda (Optional but Recommended):
Update Conda to the latest version to ensure compatibility:
conda update conda
Navigate to the Directory with the environment.yml
File:
Change to the directory where you placed the environment.yml
file:
cd /path/to/destination
Create the Environment Using the YAML File:
Run the following command to create the environment:
conda env create -f environment.yml
This command will read the environment.yml
file and recreate the environment with all the specified packages and versions.
Verify the Environment Creation:
List the available environments to ensure the new environment is created successfully:
conda env list
You should see your recreated environment in the list.
(Optional) Create the Environment Using spec-list.txt
(if you have it):
If you created a spec-list.txt
file in Step 1, use it to recreate the environment exactly:
conda create --name your_env_name --file spec-list.txt
This method ensures that the new environment matches the original environment down to specific builds and exact versions.
Activate the Recreated Environment:
Activate the environment to start using it:
conda activate your_env_name