74

How can I add a package to an existing conda environment?

If it is a python package I can use pip install <package>, but what if pip does not work?

Is it sufficient to activate the environment and use conda install <package>?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Soerendip
  • 7,684
  • 15
  • 61
  • 128

5 Answers5

84

You've answered your own question. In fact you really want to do conda install ... instead of using pip if you can.

You can install a conda package also without activating the environment. Just use conda install -n <env_name> <package> or conda install -p <path/to/env> <package>.

faph
  • 1,605
  • 13
  • 12
  • Is it recommended to use `conda-forge`? (e.g. `conda install -c conda-forge pyarrow`) – Joe Jul 07 '18 at 14:05
  • 2
    @Joe Using `conda-forge` is just specifying the channel on Anaconda where the installer should look for the package. As long as the package you want is in the main/default channel (i.e. the one that's searched when you run `conda install` without the `-c` flag), I don't see any reason to use `conda-forge` – Addison Klinke Dec 04 '18 at 15:44
32

If you want to install a specific package inside a specific conda environment, you can use the following command.

First activate the conda environment and then do:

$ conda install --name <conda_env_name> -c <channel_name> <package_name>

For a concrete example, let's assume that you want to install chainer from the channel anaconda to an already created conda environment named chainerenv, then you can do:

$ conda install --name chainerenv -c anaconda chainer
kmario23
  • 57,311
  • 13
  • 161
  • 150
  • 7
    Why would one need to activate the conda environment if one is also including the `--name ` flag in one’s command to name the environment? – TransferOrbit Jan 06 '20 at 19:12
  • Inside a conda virtual environment, it is not necessary to use `-n` to specify the environment since we are already inside the environment. – jdhao Jun 09 '20 at 13:08
3

If you want to install a package in the environment, you can use

conda install -p /path/to/env package

example:

conda install -p /users/dekstop/env-test Django
vntm
  • 31
  • 2
1

There's an alternative way to do this and I have just tested it on my own mac:

example: i want to install a non-conda package at my python2.7 environment:

  1. go to terminal

  2. activate the desired environment by: source activate py27

  3. after you successfully activated the environment, you can install the package you wanted by: pip install package

user140536
  • 144
  • 1
  • 7
0

The answer is yes (usually). One example is that you can activate your conda environment and then directly do conda install pandas.tar.bz2 on the existing tar.bz2 files from /conda_envs/.pkgs (leftovers from other environments) If you don't have a tarball package like that but you have the src with setup.py you can just do the usual install by python setup.py install (or python setup.py develop to link the src)

JoeyZhao
  • 514
  • 5
  • 12