40

I've installed Anaconda and set Path environment variable to C:\Anaconda3; C:\Anaconda3\Scripts.

Then I try to run in Git Bash

conda install python

But there is an error message "bash: conda: command not found". I would like to know why.

DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74
KHCheng
  • 621
  • 2
  • 6
  • 9
  • type: `echo $PATH` in your git bash and double check if your git bash see the paths you just added. For instanse my git bash gives me something like: `/c/Anaconda3/condabin:/c/Anaconda3/Scripts:/c/Anaconda3:` – khashashin Mar 08 '19 at 01:51

8 Answers8

48

To be able to run conda on gitbash you need to add it to the path. Many times I've seen that's done by default - as shown in the setup for this workshop. If it doesn't, as it seems your case, then you can run their setup directly by running:

. /c/Anaconda3/etc/profile.d/conda.sh

After running that you should be able to run conda commands.

To keep this setup permanently you can add such line on your .profile or .bashrc file (read more about their differences). A way of doing so is running the follwing:

echo ". /c/Anaconda3/etc/profile.d/conda.sh" >> ~/.profile

You may encounter problems if the path where Anaconda was installed contains spaces (e.g., C:\Program Files). In that case you would need to change the anaconda location or edit conda.sh script with something like:

sed -e '/^_CONDA_EXE=.*/a alias myconda="${_CONDA_EXE/ /\\\\ }"' \
    -e 's/\$_CONDA_EXE/myconda/g' /c/Program\ Files/Anaconda3/etc/profile.d/conda.sh > conda_start.sh

This sed command inserts a new alias definition myconda which changes the anaconda path from Program Files to Program\ Files so bash doesn't stop with an error like this one:

bash: /c/Program: No such file or directory

The second sed command replaces the _CONDA_EXE variable by the new alias created.

Since the above doesn't modify the file provided by anaconda, you will need to update your .profile file to load the file we've just created, conda_start.sh, instead.

jeffhale
  • 3,759
  • 7
  • 40
  • 56
dvdgc13
  • 782
  • 7
  • 11
  • 2
    note: `~/.profile` may not be read if `~/.bashrc` or `~/.bash_login` exists. – DannyDannyDanny Nov 22 '19 at 09:52
  • 1
    I am running into the same issue here. I have followed these instructions, but I get a new error: conda bash: C:\bld\conda_1565126647711\_h_env\Scripts\conda.exe: No such file or directory. I have no idea why this path is: C:\bld\conda_1565126647711\_h_env\!! When I looked further I noticed that the 'conda activate' bash script at ~\Anaconda3\Scripts\activate, and within the script I have this variable: _CONDA_ROOT="C:\bld\conda_1565126647711\_h_env"! I think it has issues with the virtual envs. I have tried to modify that path to my actual Anaconda path, but does not work, any ideas?? – TwinPenguins Nov 24 '19 at 18:56
  • 8
    I think my solution above may not be up to date anymore. If you installed a new version of Anaconda, and checked "Add anaconda to my PATH environment variable" during its instalation, then I believe you can run `conda init bash` from a git bash terminal. This will create an entry to your `.bash_profile`. In your case, I think the difference between where you have your conda installation and your environments may be giving your troubles. Are your "virtual envs", [anaconda environments](https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html)? – dvdgc13 Nov 25 '19 at 19:23
  • Note the "\ " trick to include a path with spaces does work inside the echo command too! No need for the sed command. – AlexGeorg Jan 14 '22 at 14:36
17

First, you need to move to the directory where conda is located.

(some path such as C/Anaconda3/Scripts or ../miniconda3/Scripts or anaconda3/bin)

then, open the terminal.

(or, if you use Windows and can't find where the conda is, try moving to directory such as C:\Users\User_Name\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Anaconda3 (64-bit) and open the Anaconda prompt.)

Then, do this:

conda init

(or, put ./ such as ./conda init)

or something like

conda init bash

(or ./conda init bash)

if you use Mac OS:

conda init zsh

will work well.

if you wanna use different shell,

conda init [shell_name]

[shell_name] can be: bash, cmd.exe, fish, powershell, tcsh, xonsh, zsh, etc.

use conda init --help for more info.

starriet
  • 2,565
  • 22
  • 23
  • I don't see how this can work. The OP said the "conda" command is not found. – Ben Farmer Oct 29 '20 at 03:06
  • 1
    You just need to work out where the conda executable is and use the full path. So in this case, it's `\C\Anaconda3\Scripts\conda init bash`. – Nzbuu Nov 16 '20 at 20:35
  • 3
    STEP BY STEP: - switch to `../miniconda3/Scripts/` - open git bash in folder - `./conda init bash` - This willl change `.bash_profile` file. – icemtel Dec 25 '20 at 10:17
12

Joining @dvdgc13. In my case, I fixed the problem by adding

. C:/Users/user/Anaconda3/etc/profile.d/conda.sh

to my .bash_profile.

enter image description here

DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74
2

I tried to do the same thing as you did but I couldn't get it to work. starriet had the working answer but I am going to make it easier for everyone else reading. You can directly open command windows with explorer instead of struggling with paths.

  1. Find your Anaconda3 folder with Windows Explorer This could be a user install where would be in your user folder such as "C:/Users/your_name/Anaconda3".

  2. Shift + Right Click on the explorer and click on "Open PowerShell Windows Here". Note: you can just click "Git Bash" and you open Bash instead which makes no difference to the command.

Shift+Right Click

  1. Type in "conda init" in the PowerShell window. This works even if you don't have the right paths because the command line looks for the right exe in the current dictionary. If you scroll down in the explorer, you should be able to find it.

My PowerShell window would look a little different than yours because of my prompt but it makes no difference. PowerShell window

  1. Exit the PowerShell and open Git Bash. Type "conda" to confirm that things work.
f0lie
  • 46
  • 4
0

For MAC users, do this:

$ echo ". /usr/local/anaconda3/etc/profile.d/conda.sh" >> ~/.bashrc
$ source ~/.bashrc
Nditah
  • 1,429
  • 19
  • 23
0

I tried many ways, but they are incomplete until I run the following commands:

  1. Go to the path of anaconda3 is C:\Users\USER_NAME\anaconda3 and open commend line over there and print the following: (YOUR_PATH = C:\Users\USER_NAME\anaconda3 )

    echo 'export PATH="$PATH:[YOUR_PATH]:[YOUR_PATH]/Scripts"' >> .bashrc
    echo 'alias python="winpty python.exe"' >> .bashrc
    
  2. If Git bash is opened, close it and reopen it again and type the following to make sure anaconda and python work without problems:

    conda --version 
    python -- version 
    

If you see the versions are printed, everything works well.

Hamzah
  • 8,175
  • 3
  • 19
  • 43
0

In my case conda command was recognised in cmd, but not in bash. I used conda init bash instead of simple conda init in cmd. This command modified the .bash_profile file, which was residing in my C:\Users\xyz directory, and added the following code in it

# >>> conda initialize >>>

# !! Contents within this block are managed by 'conda init' !!
eval "$('/C/Users/Saifullah/miniconda3/Scripts/conda.exe' 'shell.bash' 'hook')"
# <<< conda initialize <<<

now it is working in bash terminal too.

0

For me all the above did not work, but I got it working by fixing the path. In the .bash_prfile the following path was inserted: /cygdrive/c/Users/Username/Anaconda3/Scripts/conda.exe

I changed it to C:\Users\UsernameAnaconda3\Scripts\conda.exe and conda could be found by the bash.

Merk
  • 171
  • 12