How do I activate a conda environment in my .bashrc?

I use Conda for package management in Python. I have a basic environment which I use almost all of the time, and I want it to be loaded by default when I open a terminal. How do I set up my .bashrc to load the environment?

So far, I tried source activate myenv, but my understanding is that I need to provide an actual path within the .bashrc file. I then tried source ~/anaconda3/envs/myenv/bin/activate. Although this doesn't throw an error, it also doesn't activate the environment. I'm running Ubuntu 16.04.


It looks like the accepted answers might be out of date. From the docs:

If your shell is Bash or a Bourne variant, enable conda for the current user with

$ echo ". /home/<user>/miniconda3/etc/profile.d/conda.sh" >> ~/.bashrc

or, for all users, enable conda with

$ sudo ln -s /home/<user>/miniconda3/etc/profile.d/conda.sh /etc/profile.d/conda.sh

The options above will permanently enable the 'conda' command, but they do NOT put conda's base (root) environment on PATH. To do so, run

$ conda activate

in your terminal, or to put the base environment on PATH permanently, run

$ echo "conda activate" >> ~/.bashrc

Previous to conda 4.4, the recommended way to activate conda was to modify PATH in your ~/.bashrc file. You should manually remove the line that looks like

export PATH="/home/<user>/miniconda3/bin:$PATH"

^^^ The above line should NO LONGER be in your ~/.bashrc file! ^^^


For bash use:

$ cd YOUR_PATH_ANACONDA/bin
$ ./conda init bash

That will automatically edit your .bashrc.

Reload:

$ source ~/.bashrc

Test (install Spyder):

$ conda install -c anaconda spyder

Run Spyder

$ spyder

During the Anaconda install there should be an entry added the .bashrc file like this

export PATH="/home/<user>/anaconda3/bin:$PATH"

if it is not there, verify the install by running which conda, and update .bashrc with the path up to bin.

This points to the 'conda' executable, and sets up the path to handle conda activate.

Add this line after the export command:

source activate <your_environment>

from there you can source ~/.bashrc to load the environment to the current shell.