Setting Up Conda in Git Bash

Setting Up Conda in Git Bash

If you’re a Windows user following along with the Data Science or Computer Science career paths on Codecademy (or the Learn Python courses), you may have noticed the recommendation to use Git Bash for your terminal, and the instructions on installing Python via Miniconda, but the lack of guidance on using conda within the Git Bash terminal rather than CMD or Anaconda Prompt.

If you use and enjoy Git Bash and want to take advantage of the power of conda without switching terminals, follow the instructions below to get it up and running.

(This post assumes you have already installed and used Git Bash previously)

What is conda, and why would I want to use it?

Before we dive into the instructions, it’s important to understand a little about conda and how it differs from pip. In short, conda is a very powerful package manager that excels at managing dependencies and offers an easy way to create and use virtual environments for your projects. It is used primarily in the Data Science world, but can be used for much, much more.

For a detailed breakdown, read the excellent post by Jake VanderPlas (Software Engineer, Google), Conda: Myths and Misconceptions

Instructions

1. Install Anaconda

Although Codecademy recommends installing Miniconda, I highly recommend saving some headaches and installing the full version of Anaconda if you have the space on your PC and you are serious about getting into Data Analysis/Data Science. Miniconda is a barebones version of the Anaconda distribution, and is a little less beginner-friendly, coming with only Python, conda, pip, and a couple of dependencies necessary to make them work.

If you are low on hard drive space, Miniconda is fine — just don’t be surprised by the number of packages you’ll have to install that would be ready out of the box with Anaconda (Pandas, Numpy, Matplotlib, Seaborn, Jupyter, Scikit-learn, etc.).

You can download the installer for Anaconda here.

2. Run Anaconda Prompt (skip this section if you are familiar with conda)

Once Anaconda is downloaded and installed, you will be ready to use conda. Before we set it up in Git Bash, it is useful to see what it looks like when you are using conda.

Anaconda and Miniconda come with a program called Anaconda Prompt on Windows, which is essentially just CMD terminal that is pre-set-up for conda. If you type Anaconda in your Windows search bar, you will see Anaconda Prompt come up. Click on it to use conda for the first time:

Once you open it up, you will see something like this:

Notice that at the beginning of the prompt, you see (base). This tells you that you are currently inside of the conda virtual environment called base. This base environment is where all of the pre-loaded packages are contained. Later on, as you become more familiar with conda, you should begin creating new environments for each project you work on. When you do, you will see the name of the currently active conda environment inside of the parentheses. For example, if you make and activate an environment called “test”, you will see (test) at the beginning of the prompt.

If you are interested in seeing which packages are installed in your base environment, simply type the command conda list.

3. Make conda run in Git Bash

A) Open Git Bash

Now that you have an idea of how conda should look, go ahead and open Git Bash. As you can see, there is nothing on the prompt to indicate whether you are inside an active conda environment or not. Unfortunately, it’s not quite that simple. If you type conda list in Git Bash, you will receive an error that looks something like this:

bash: conda: command not found

This is because conda has only been set up to run from Anaconda Prompt by default.

B) Add the conda shell script to your .bashrc

In order to make the conda command available in Git Bash, you need to add conda’s shell script to your .bashrc file. This is the same file that you store your bash aliases in (such as the sqlite3 alias you probably created when you followed these instructions).

The shell script we need is located inside of the folder your Anaconda distribution added to your computer. If you remember where that is, you can navigate there using the File Explorer. If you don’t remember, the easiest way to find it is to use the search bar and search for “Anaconda3” and scroll down to the folders:

Open this folder, then navigate to etc -> profile.d.

Inside of the profile.d folder, you should see a file called conda.sh. This is the file you want to add to your .bashrc file.

Now, there are a few ways you can do this: a) you can open your .bashrc and type it in there; b) you can type the path to conda.sh in Git Bash and add it to your .bashrc from there; or c) you can open Git Bash in the profile.d folder and utilize the bash command PWD in order to lower the risk of spelling errors in the path. The last option is my preferred choice, so that is what I will cover here.

Go ahead and close Git Bash, and then right click inside the file explorer and choose “Git Bash Here”:

This will open a new instance of Git Bash to the correct directory.

Now, so long as the path to your file has no spaces, you can add conda.sh to your .bashrc with this simple command:

echo ". ${PWD}/conda.sh" >> ~/.bashrc

If the path does contain spaces (such as in your username), you will need to add single quotes in the command like so:

echo ". '${PWD}'/conda.sh" >> ~/.bashrc

After running this command, you will need to close Git Bash and reopen it for it to take effect.

C) Reopen Git Bash and activate conda!

When you reopen Git Bash, you still won’t see (base) in your prompt — that’s because you haven’t activated conda yet! To activate it, simply type the command conda activate and hit Enter. You should see (base) pop up above your prompt:

image

And that’s it! You’ve successfully enabled conda in your Git Bash terminal. Now Git Bash can do virtually everything Anaconda Prompt can do, just make sure to use the conda activate command whenever you want to use conda in a new Git Bash window (you can also use conda deactivate if you want to exit your conda environment and use a different installation of Python).

Thanks for reading, and happy coding!

18 Likes

Hi coco, sorry for the delay I needed a break.

Consider myself a total noobie in this maters so I am not entierly sure what is going on while following some of this instructions, neither I knew Python was already bundled with Anaconda…

Long story short, I have probably messed up somewhere so I uninstalled EVERYTHING, Anaconda, Python even VS code just in case :laughing:

Then I followed every step on your guide and now it works perfectly! :ok_hand:

I am going to delete my previous post to aviod confussions.

I still have some doubts though, with this setup I dont need to add anything else to the path?
Neither python?

My apollogies, and thank you for your time!

3 Likes

All good @kennyx0r , I’m glad you got it working!

I was mostly concerned that you (or anyone else reading through) would get even more confused by the alternate method you were using. I remember exactly what it was like being a new to programming and trying to set up everything on my own device for the first time. That’s precisely why I wrote this post — to save others the agony that I had to go through! :laughing:

As far as adding Python to your path, you probably won’t need to so long as you are only using Anaconda Prompt or Git Bash (with conda activated). I don’t remember for sure, but Anaconda may give you the option to automatically add it to your path while installing. If not, you’ve already shown that you know how to do so if the need arises (just add the python.exe file to the PATH variable in your system environment variables). If you later decide to download another version of Python from python.org, that installer also asks if you want to add it to your PATH variable, so you can do it with just a click of a button.

Now, if VS Code doesn’t recognize your version of Python, you may have to add the path to that python.exe file inside of VS Code itself. This can easily be done inside the VS Code settings. Just search the settings for “python path” and enter the path in this section:
image

Hopefully this helps get you all set up! If not, feel free to post any other questions here in the forums.

Happy coding!

1 Like

Hi, I have been struggling with this all day.

Getting Anaconda to work on my MacBook pro was no problem at all, as simple as installing it and it worked, however, I have had nothing but issues on windows. I have tried following your steps, (thank you so much for them) at least 3 times now all to no success.

I have anaconda installed and it works fine in the anaconda prompt. I follow all your steps and this is what I get:

and yet conda still isn’t working. I would greatly appreciate any assistance in this and if you could help me get this working I would be massively thankful.

Thanks in advanced

Did you catch the instructions about altering the command if your username contains alternative characters and the necessity to close and reopen the git bash window?

It might be worth manually checking the contents of your .bashrc file (at ~/.bashrc) to make sure the correct file path has been added (any text editor would be suitable). If you need to double check the path then a drag and drop of that folder into the git bash window should paste the full path.

2 Likes

I’ve tried this several times and it just isn’t working for me, I keep being told that the conda command doesn’t exist. Here is my .bashrc:

Got the same issues are the last two posters. I installed anaconda for all users, the .bashrc contains
. /c/ProgramData/Anaconda3/etc/profile.d/conda.sh
which is the correct path.
But it doesnt do anything.

Make sure there is a space between the period and the first forward slash in the path. Then make sure the .bashrc is saved. If Git Bash is open, close it, because it needs to be restarted for this to take effect. Re-open Git Bash and try the command conda activate.

If it still doesn’t work, please post the actual error message as it’s really hard to help debug this sort of issue in the forums. It is almost always user error, and we have no way of knowing which steps you did, in which order, if there were any typos, etc.

However, there are also other factors that may be coming into play here with newer versions of Anaconda. I only wrote what worked for me, but maybe some of the answers on this SO thread will be useful to those who are having trouble with my instructions:

When in doubt, remember that Google is your friend.

I prety much copy pasted the commands here, so it’s 100% identical.

echo “. ${PWD}/conda.sh” >> ~/.bashrc
Added the line
. /c/ProgramData/Anaconda3/etc/profile.d/conda.sh

Alternatively
echo “. ‘${PWD}’/conda.sh” >> ~/.bashrc
added the line
. ‘/c/Users/Kordanor’/conda.sh

(again, lines copy basted from gitbash)

I only added the first at first. But that didnt work. So I added the second as well, no change.image

Upon restarting and using the command I get:
$ conda activate
bash: conda: command not found

Hello I had the same issue. I tried uninstalling and installing several times following the instruction yet none.

Try this. It resolved my issues.

Try uninstalling your anaconda again. And install it again. run as administrator. This time make sure to check “Add Anaconda3 to the system PATH environment variable” before you click Install

Capture
Hope this will help you as well. Although, I am not yet sure how it will affect the performance of the program

2 Likes

@jingbertmuros you saved me.

Furthermore, when my Anaconda installed my shell file was named “conda” not “conda.sh” so when I used the code provided in the original post I was not able to activate it. simply by using:

echo ". ${PWD}/conda" >> ~/.bashrc

I was successful

Still didn’t work :frowning:

My challenge is a strange one. The directions are are lovely and easy to follow quite alright, the problem I have is that I can’t even see the Anaconda3 folder where I’d get the “etc” and “profile.d” to begin with. And I certainly have Anaconda installed.

Please help :pleading_face:

tks, i’ve done Setting Up Conda in Git Bash

I am stuck.
Inside the anaconda3 folder, there is no profile.d file. How do I correct this? I have a snip belowanaconda3 snip

1
After the command:
echo “. ${PWD}/conda” >> ~/.bashrc
Close your terminal.
Open your terminal, type source ~/.bashrc
type conda activate

1 Like

Using windows 10, I do not have the right-click option to ‘Git Bash Here’ (googled a couple ways to create this option but was unsuccessful).

Decided to navigate to the directory within git bash using cd (I believe this is your option b) and then input the code. Wound up working (on the second try): I can use the conda activate code within gitbash and see (base) above the next prompt.

My issue now is when I open git bash, the first line displays “bash: ./c/Users/myusername/anaconda3/etc/profile.dconda.sh: No such file or directory”. Any ideas on how to stop this from happening? I am guessing that it was caused by my first attempt to add the conda.sh file to .bashrc; I did not use single quotes and there may have been some other form of user error.

I assume I could solve it by completely uninstalling bash and repeating the process (hopefully getting it right the first time), and this might allow me to add the right click option to Git Bash Here while configuring the installation, but in the spirit of learning about Git Bash I am curious if there is another way.

Thanks

Thanks for all the contributions, both the patient explanations ánd the questions. It wasn’t working for me either, even after adding the path to conda to .bashrc. Turns out, I had to conda init bash to have conda thru git bash. Learned it here:
https://stackoverflow.com/questions/55507519/python-activate-conda-env-through-shell-script

Hope that helps somebody. Re not being able to find the folder, try choosing the folder yourself upon installing. Before that, it was nested in a local folder buried deep in USER. (Newb here so excuse my short understanding) Cheerio!
Anne

I finally got this to work for me by applying the path to ~/.bash_profile instead of ~/.bashrc. I don’t know if it will work for anyone else but I would give it a try before you delete everything and change how you installed everything.

1 Like

Same, now it works. Thanks!