How to have a conda clone environment on another machine?

4.1K    Asked by DavidEdmunds in Data Science , Asked on Feb 15, 2023

I developed a machine learning model with Python (Anaconda + Flask) on my workstation and all goes well. Later, I tried to ship this program onto another machine where of course I tried to set up the same environment, but the program failed to run. I copied the program to other machines where it also runs smoothly.

I cannot figure out what the problem is in the failed case (both the program code and the error message are copious so I am not able to present them here) but I'm almost certain that it is something with the different versions of the dependencies.

So, my question is that given an environment where a certain program runs well, how can I clone it to another where it should run well also? Of course, without the cloning of the full system. 

Answered by David Edmunds

Use Conda Pack to have conda clone environment in another machine.

install using conda or pip:
conda install -c conda-forge conda-pack
pip install conda-pack
Then for:
Backing up:
# Pack environment my_env into my_env.tar.gz
$ conda pack -n my_env
# Pack environment my_env into out_name.tar.gz
$ conda pack -n my_env -o out_name.tar.gz
# Pack environment located at an explicit path into my_env.tar.gz
$ conda pack -p /explicit/path/to/my_env
And to restore it on the other machine(s):
# Unpack environment into directory `my_env`
$ mkdir -p my_env
$ tar -xzf my_env.tar.gz -C my_env
# Use Python without activating or fixing the prefixes. Most Python
# libraries will work fine, but things that require prefix cleanups
# will fail.
$ ./my_env/bin/python
# Activate the environment. This adds `my_env/bin` to your path
$ source my_env/bin/activate
# Run Python from in the environment
(my_env) $ python
# Cleanup prefixes from in the active environment.
# Note that this command can also be run without activating the environment
# as long as some version of Python is already installed on the machine.
(my_env) $ conda-unpack
a bit of an explanation :

If you plan on getting an exact copy of your current environment and then move it to another machine with the same platform and OS, without redownloading all packages again from the Internet (good for offline machines/behind firewalls). All other previous methods require internet connection. so in case you don't have access to the internet, then you can use conda pack.

Conda Pack

Conda-pack is a command line tool that archives a conda environment, which includes all the binaries of the packages installed in the environment. This is useful when you want to reproduce an environment with limited or no internet access. All the previous methods download packages from their respective repositories to create an environment. Keep in mind that conda-pack is both platform and operating system specific and that the target computer must have the same platform and OS as the source computer. To install conda-pack, make sure you are in the root or base environment so that it is available in sub-environments. Conda-pack is available at conda-forge or PyPI.

Your Answer

Answer (1)

To clone a Conda environment from one machine to another, you can use the conda env export and conda env create commands to create a YAML file containing the environment specifications and then recreate the environment on the target machine using that YAML file. Here's a step-by-step guide:

Export Environment Specification: On the source machine, navigate to the directory containing the environment you want to clone and export its specifications to a YAML file using the following command:

conda env export --name  > environment.yml

Replace with the name of the environment you want to clone.

Transfer YAML File: Transfer the environment.yml file to the target machine. You can use methods such as email, cloud storage, USB drive, or any other means to transfer the file.

Create Environment: On the target machine, navigate to the directory containing the environment.yml file and use the following command to create the environment based on the specifications in the YAML file:

conda env create -f environment.yml

This command will create a new Conda environment with the same name and packages as specified in the YAML file.

Activate Environment: After the environment has been created, activate it using the following command:

conda activate 

Replace with the name of the environment you cloned.

By following these steps, you can easily clone a Conda environment from one machine to another. This method ensures that the environment's dependencies and configurations remain consistent across different machines.

2 Months


Parent Categories