342

How can a pre-existing conda environment be updated with another .yml file. This is extremely helpful when working on projects that have multiple requirement files, i.e. base.yml, local.yml, production.yml, etc.

For example, below is a base.yml file has conda-forge, conda, and pip packages:

base.yml

name: myenv
channels:
  - conda-forge
dependencies:
  - django=1.10.5
  - pip:
    - django-crispy-forms==1.6.1

The actual environment is created with: conda env create -f base.yml.

Later on, additional packages need to be added to base.yml. Another file, say local.yml, needs to import those updates.

Previous attempts to accomplish this include:

creating a local.yml file with an import definition:

channels:

dependencies:
  - pip:
    - boto3==1.4.4
imports:
  - requirements/base. 

And then run the command: conda install -f local.yml.

This does not work. Any thoughts?

darthbith
  • 18,484
  • 9
  • 60
  • 76
  • 1
    Is it possible to update the currently activated environment?? I just tried this but the update went to the environment named in the yml file. – Soerendip Nov 07 '19 at 04:49
  • this might be useful to start from scratch: `conda env create -f ~/CoqGym/coq_gym.yml` (see: https://stackoverflow.com/questions/54492671/how-to-install-list-of-python-libraries-using-yml-file-without-making-new-enviro) – Charlie Parker Jan 04 '21 at 20:46

4 Answers4

539

Try using conda env update:

conda activate myenv
conda env update --file local.yml --prune

--prune uninstalls dependencies which were removed from local.yml, as pointed out in this answer by @Blink.

Attention: if there is a name tag with a name other than that of your environment in local.yml, the command above will create a new environment with that name. To avoid this, use (thanks @NumesSanguis):

conda env update --name myenv --file local.yml --prune

See Updating an environment in Conda User Guide.

ankostis
  • 8,579
  • 3
  • 47
  • 61
alkamid
  • 6,970
  • 4
  • 28
  • 39
  • 20
    You don't need the equals sign between `-f` and `file.yml`. Removing the equals sign will make tab completion work on the `.yml` filename. – BallpointBen May 09 '18 at 14:42
  • 22
    Add `--name env_name` to ignore any `name: foo` tag in `local.yml`. Also prevents the need to activate myenv first. Full command: `conda env update --name env_name --file local.yml` From: https://stackoverflow.com/a/45525593/3399066 's comment – NumesSanguis Nov 15 '19 at 09:22
  • 3
    In the most recent conda version I think we use "conda activate myenv" instead of "source activate myenv" – lightbox142 Feb 17 '20 at 16:11
  • Is there any way, we can pass an argument where this command will just update available libraries in .yml files, which will ensure that error is not thrown when any of the library listed is not available? – dan Nov 06 '20 at 22:42
  • 2
    make sure that the YAML file has no `name` parameter, otherwise it **creates** with this **update** command – Dima Lituiev Dec 24 '20 at 21:48
  • why aren't you passing the `--prune` flag? as in `conda env update -f local.yml --prune`? – Charlie Parker Apr 26 '21 at 17:19
  • 1
    @DimaLituiev can you clarify what you mean with `otherwise it creates with this update command`? what is the issue if the name is there already? also, will the prefix word at the bottom create an issue? – Charlie Parker Apr 27 '21 at 14:41
  • FYI `conda env update --file local.yml --prune` will generate an environment with the name specified in the .yml (i.e., `name: `) so activating your destination environment does nothing – Jesse H. Apr 06 '22 at 03:17
  • Is that doable without using conda? Just with regular pip/virtualenv? – Rômulo Férrer Filho Aug 24 '22 at 02:27
  • if the env file includes `name: myenv`, is it necessary to `conda activate myenv` or provide `--name myenv`? – william_grisaitis Nov 21 '22 at 03:30
80

The suggested answer is partially correct. You'll need to add the --prune option to also uninstall packages that were removed from the environment.yml. Correct command:

conda env update -f local.yml --prune
Blink
  • 1,408
  • 13
  • 21
  • 4
    Even the **--prune** flag is not enough to remove pip installed dependencies... – Jean Paul Mar 04 '20 at 13:03
  • True. I think it will remove the conda dependencies but not the ones listed under pip – Blink Mar 05 '20 at 16:49
  • 1
    Is there any way, we can pass an argument where this command will just update available libraries in .yml files, which will ensure that error is not thrown when any of the library listed is not available? – dan Nov 06 '20 at 22:42
  • 1
    would there be a problem if my `requirements.yml` file has a `name:` at the top? – Charlie Parker Apr 27 '21 at 14:42
34

alkamid's answer is on the right lines, but I have found that Conda fails to install new dependencies if the environment is already active. Deactivating the environment first resolves this:

source deactivate;
conda env update -f whatever.yml;
source activate my_environment_name; # Must be AFTER the conda env update line!
Dave
  • 44,275
  • 12
  • 65
  • 105
  • 7
    How does conda know which env to update if it's not the currently active one? – Thomas Fauskanger Jul 18 '18 at 16:35
  • 6
    @ThomasFauskanger you can specify it explicitly with `-n `, but by default it seems to work with the expected environment (maybe the most recently active, or just a default environment choice) – Dave Jul 18 '18 at 17:36
  • 21
    @ThomasFauskanger The name of the environment is specified in the YAML file if it has been exported by conda. – Thomas Aug 30 '18 at 07:42
  • 5
    I thought the old point of this question is to update a different environment to the one specified in the yaml file. – Giacomo Aug 01 '19 at 21:02
  • 2
    @ThomasFauskanger the yaml file includes the environment name – Abdulrahman Bres Oct 31 '19 at 21:03
1

Recently Conda introduced the option to stack environments, which should solve this problem.

doneforaiur
  • 1,308
  • 7
  • 14
  • 21
seali33
  • 36
  • 3