152

What is the conda version of this?

pip install -r requirements.txt --target ./lib

I've found these commands:

while read requirement; do conda install --yes $requirement; done < requirements.txt

But it doesn't tell how to specify --target ./lib

sophros
  • 8,714
  • 5
  • 30
  • 57
vineeth kanaparthi
  • 1,541
  • 2
  • 8
  • 4

5 Answers5

217

You can run conda install --file requirements.txt instead of the loop, but there is no target directory in conda install. conda install installs a list of packages into a specified conda environment.

Tim Swast
  • 12,039
  • 4
  • 35
  • 51
phd
  • 57,284
  • 10
  • 68
  • 103
  • 1
    When I do this on my `requirements.txt` specifying versions of packages, I get `InvalidVersionSpec: Invalid version '3.0.': empty version component` – Dr_Zaszuś Apr 07 '20 at 08:01
  • There us no target directory for in `conda install`. However specifying a location for a virtual environment is possible with the `--prefix` optional argument ([doc](https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#specifying-a-location-for-an-environment)) and target environment specifications can be given with the `--name` or `--prefix` optional arguments ([doc](https://docs.conda.io/projects/conda/en/latest/commands/install.html#Target%20Environment%20Specification)). – Remi Cuingnet Jun 25 '20 at 08:03
  • what do you mean by `conda install installs a list of packages into a specified conda environment.`? For me it usually installs it for whatever conda env is active at the moment. – Charlie Parker Apr 23 '21 at 18:08
  • 1
    @CharlieParker The OP wanted to install to a different directory; `pip` can do this but `conda` probably cannot. – phd Apr 23 '21 at 18:24
77

To create an environment named py37 with python 3.7, using the channel conda-forge and a list of packages:

conda create -y --name py37 python=3.7
conda install --force-reinstall -y -q --name py37 -c conda-forge --file requirements.txt
conda activate py37
...
conda deactivate

Flags explained:

  • -y: Do not ask for confirmation.
  • --force-reinstall: Install the package even if it already exists.
  • -q: Do not display progress bar.
  • -c: Additional channel to search for packages. These are URLs searched in the order

Alternatively you can create an environment.yml file instead of requirements.txt:

name: py37
channels:
  - conda-forge
dependencies:
  - python=3.7
  - numpy=1.9.*
  - pandas

Use these commands to create and activate the conda environment based on the specifications in the Yaml file:

conda env create -f environment.yml
conda activate py37

Use this command to list the environments you have:

conda info --envs

Use this command to remove the environment:

conda env remove -n py37

New! The ansible-role dockpack.base_conda can manage conda environments on Linux, Mac and Windows, and can be used to create a docker image with custom conda environments.

bbaassssiiee
  • 4,879
  • 1
  • 34
  • 46
  • 23
    explaining the flags would be useful – Ataxias Jul 09 '19 at 19:22
  • Is there any reason why after the 2nd command above python3 is removed from the environment and python2 is the only one remaining? – ionox0 Jan 14 '20 at 16:53
  • 1
    conda manages python environments, conda deactivate resets your shell, conda activate py37 sets your PATH. – bbaassssiiee Jan 14 '20 at 21:12
  • 1
    It could be usefull if you add where to put yaml file and how to install environment from it. I guess environment doesn't simply come to existence if you have yaml file. – Harvey Dec 11 '20 at 03:43
13

You can always try this:

/home/user/anaconda3/bin/pip install -r requirements.txt

This simply uses the pip installed in the conda environment. If pip is not preinstalled in your environment you can always run the following command

conda install pip
Amrit Das
  • 294
  • 1
  • 5
  • 11
2

A quick search on the conda official docs will help you to find what each flag does.

So far:

  • -y: Do not ask for confirmation.
  • -f: I think it should be --file, so it read package versions from the given file.
  • -q: Do not display progress bar.
  • -c: Additional channel to search for packages. These are URLs searched in the order
KingDarBoja
  • 616
  • 3
  • 10
1

would this work?

cat requirements.txt | while read x; do conda install "$x" -p ./lib ;done

or

conda install --file requirements.txt -p ./lib
john.da.costa
  • 4,244
  • 4
  • 25
  • 27