35

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.

Eli Sander
  • 473
  • 1
  • 5
  • 7

9 Answers9

35

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! ^^^

Quar
  • 148
  • 3
jerpint
  • 466
  • 5
  • 4
17

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
alditis
  • 311
  • 3
  • 5
5

Correct Fix

(works for versions >= 4.6)

find . -type f -name 'conda' check where the conda binary is and thene cd to it or just give the complete path and run

conda config --set auto_activate_base true

To deactivate just do the same but with false. Obviously:

conda config --set auto_activate_base false

Quick & Dirty Fix #1

Paste the following into your .bashrc, replace with the obvious and source your .bashrc (source .bashrc). Should work for Miniconda3 version >= 4.6

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/<YOUR_USER>/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/<YOUR_USER>/miniconda3/etc/profile.d/conda.sh" ]; then
        . "/home/<YOUR_USER>/miniconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/<YOUR_USER>/miniconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

This is a cut and paste from my own .bashrc, you can remove the comments but I find them handy as delimiters.

Quick & Dirty Fix #2

Remove the whole thing with your favourite removal command (rm -rf ~/miniconda3), run the install script again and pay attention to the prompt as it will aks you if you want to autostart it.

Whatever rocks your boat :)

runlevel0
  • 333
  • 1
  • 7
  • 16
  • 1
    Note you may need to run this first: export PATH="/home//miniconda3/bin:$PATH" before it'll find the conda binary ;) – Owl Sep 21 '22 at 15:17
  • 1
    Hi Owl, not necessary, `find` will return the whole path and I mentioned that the user should use that one. And the .bashrc code already indicates the full path. Conda will set the $PATH itself once activated. But thanks for the tip (it's an old answer and I had to check) ; – runlevel0 Sep 28 '22 at 12:40
  • 1
    ahh yes good point. If for some reason you do need to run the conda binary - say you have a script that does it that's part of some config utility, export the path with the path to the conda binary first. – Owl Sep 28 '22 at 14:04
5

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.

Ogre55
  • 484
  • 6
  • 8
4

Use:

conda init bash

That will automatically edit your .bashrc.

Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493
1

As commented by @oya163, if .bashrc doesn't work then try copying ~/.bashrc to ~/.profile, it resolved my issue.
Also, run conda init, it will setup necessary steps for conda activate and setup commands are added into ~/.bashrc file by conda init. Later, one can copy this whole ~/.bashrc to ~/.profile or ~/.bash_profile

I use the below scripts to setup above mentioned things

#!/bin/bash
conda_setup() {
      echo "setting up conda..."

      # setting up .profile
      BASHRC_FILE="~/.bashrc"
      PROFILE_FILE="~/.profile"

      if [ ! -f "$PROFILE_FILE" ] 
      then
            conda init
            cp -rf "$BASHRC_FILE" "$PROFILE_FILE"
      fi
      ls -ll "$PROFILE_FILE"

      echo "conda setup done" 
}

conda_setup || echo "some issue in conda setup"
ThunderBird
  • 1,915
  • 13
  • 19
  • 31
0

This may be somewhere above (but I got it wrong first off). Its important that in your bashrc file you first export your conda path. So the path to conda (or miniconda etc) will come above the conda activate <env> line. The format is like so:

export <path to conda bin>

<any other conda initializations>

conda activate <env>
ashley
  • 101
  • 2
0

If you want a conda environment to be activated by default when you launch a new bash terminal, you can add the following line to your ~/.bashrc file:

export PATH=<PATH_TO_YOUR_CONDA_ENVIRONMENT/bin>:$PATH

You should replace <PATH_TO_YOUR_CONDA_ENVIRONMENT/bin> in the above line with the full path to your conda environment.

In your case, you can add the following line to your ~/.bashrc file:

export PATH=~/anaconda3/envs/myenv/bin:$PATH

Basically we are adding the bin directory of your conda environment as the first entry in your PATH which is essentially what the activate convenience script will do. After this, When you open a new bash terminal, the conda environment will be "activated"/"enabled" by default.

Note that you may not see the (myenv) prefix to your bash prompt like you would if you did source activate myenv. If you want the prefix to your prompt to show up as well, add the following line to your ~/.bashrc file:

export PS1="(myenv)"$PS1

Where (myenv) is any custom name you can give that will show up as the prefix to the bash prompt.

  • For me this works, but `conda info --envs` still shows the `root` environment as current. Is there any reason I can't just add `source activate py34` (py34 is my environment name) to my ~/.bashrc? It seems to work, but is there some reason this wasn't recommended? – BStateham May 04 '18 at 05:42
  • 2
    Make sure that the `PATH` variable is pointing to the `bin` folder under the conda environment's folder and not the base/root bin folder. For example: `~/anaconda3/envs/myenv/bin` and not `~/anaconda3/bin`) Yes. you can simply add the anaconda bin folder (eg.: `~/anaconda3/bin`) to the system PATH and then `source activate ENV_NAME` in your `~/.bashrc` or `~/.bash_profile`. It was not recommended because, the config files (`~/.bashrc` or `~/.bash_profile`) are themselves *sourced* and are not *executed* when a new bash terminal is opened. – Praveen Palanisamy May 05 '18 at 13:45
-1

To activate conda environment simply put this at the end of your .bashrc file to open .bashrc open terminal, go to home directory. Run/type nano .bashrc, at the prompt put the following at the end of the file:

conda activate my_environment_name

now save the .bashrc file (Ctrl+Shift+o) press enter.

slava
  • 3,787
  • 9
  • 30
  • 65
Cat
  • 101
  • While I am no conda expert, I can tell you `~$` refers to nothing but `~$` (which isn't anything). `~` or `$HOME` will reference `/home/user/` – j-money Mar 05 '19 at 11:08
  • yes "~ "is what im referring to. so when you see ~$ your in home environment just trying to explain so anyone can understand, its pity you downvoted over the dollar sign as this could have been helpful pertaining to the actual question in hand... – Cat Mar 05 '19 at 11:45
  • Unfortunately for me, I commented but did not downvote (so now I get the aftermath).. In any event how can anyone understand `~$` as the correct path when it isn't even a path? – j-money Mar 05 '19 at 11:47
  • ok point taken , i will try and be more "specific" – Cat Mar 05 '19 at 11:53
  • This used to work but longer does -- at least on Mac (for those of us who still use bash rather than Apple's recommended zsh). The above command at the end of .bashrc is ignored, and conda starts up in "base" environment regardless of which environment you try to activate. – sh37211 Sep 10 '22 at 22:49
  • ^Ooo just found a fix for my previous comment: run `conda config --set auto_activate_base false` to disable the auto loading of base. After that, it WILL start up with the environment you specify! – sh37211 Sep 10 '22 at 22:56