How to Successfully Install Anaconda on a Mac (and Actually Get it to Work) | by Anne Bonner | Towards Data Science

You know you need it.
As you’re getting started in data science, machine learning, or artificial intelligence, you’re quickly going to realize that you need to be able to use Anaconda. You might want to use Jupyter Notebooks, Spyder, or another awesome program, but one way or another, you’re going to need this thing to work.
While setting up a computer recently, I remembered how hard it was to get Anaconda to work the first time. The installation itself was no problem! The official guide is clear and comprehensive when it comes to the installation itself. You work through it, follow a few simple steps, and you’re good to go!
At the end, it tells you to verify that your installation was successful by typing a command like “conda list” and you’re done!
Nope.
“Conda” didn’t work the first time I installed it. There’s no handy link sitting there that will tell you what to do. You verify it and then you’re done.
You’re on your own.
There’s nothing more frustrating than not even being able to open the program that you need.
It’s not an easy thing to get started in machine learning and artificial intelligence, and being unable to get the necessary tools correctly installed is just crippling. As you start to understand more of the language and commands that you need to make things work, you’ll find that there are plenty of simple answers out there. But none of them make any sense when it’s all new! Just Googling your questions can be challenging.
I had actually done this to myself.
I work on a MacOS (Mojave, if you’re interested). After spending some time working in the terminal, I had turned it into something that was fast, efficient, and also very nice to look at. You can see everything that I did to make my terminal awesome here, but essentially, it’s running
iTerm2
Homebrew
Zsh
Oh My Zsh
a few Oh My Zsh plugins
Z
Syntax Highlighting
towardsdatascience.com
Little did I know that this would make my Anaconda installation a tiny bit more difficult. One single line of code more difficult, as it turns out, but a million times more challenging for a newbie.
This is the step-by-step installation process with a simple one-line fix that will allow you to “conda” anything you want. Even if you’re using the lovely and amazing Zsh.
So here’s the thing: the installation was successful, but there was a tiny problem between Zsh and the way my terminal wanted to talk to Anaconda. This can happen for any number of reasons, but Zsh was the culprit here.
It was incredibly easy to fix! But because I had no idea what I was doing, it took forever to figure out.
No one else should have to go through that.
The official installation guide is here if you feel like checking it out.
The first time I decided to download Anaconda, I went through the normal graphical Mac installation. It worked fine as an application, but it just wouldn’t work with my terminal.
Unwilling to give up, I uninstalled Anaconda and then tried the command-line installation. That one is great because you can decide during installation that you want your terminal to communicate with Anaconda. You’ll get a prompt during installation that asks, “Do you wish the installer to initialize Anaconda3 by running conda init?”
I’d recommend giving that one a big yes.
Of course, it still didn’t work. It probably would, though, if you didn’t have a tricked out terminal! If you aren’t using Zsh and Oh My Zsh, you might be set.
So how do you get this to work the way it’s supposed to if you’ve made changes to your terminal?
It’s simple!
These are the simple steps for a successful Anaconda installation, even in the face of the dreaded “conda command not found” error message.
You can go here to download Anaconda. Then scroll down a little to the part that says “Anaconda 2019.03 for macOS Installer.”
You’ll need to know which version of Python you have, so go to your terminal and type
python
You have a couple of choices for your download. You can choose the graphical installer, which means that you’ll install Anaconda the same exact way that you’d install any other program. Or you can choose the command line installer, which means that you’ll go to your terminal and type out (or copy and paste) the commands.
We’re going to do the command line installer, so click that link under your version of Python and your download should start.
After your download is complete, head on over to your terminal.
If you have Python 3.7, you’ll run
bash ~/Downloads/Anaconda3-2019.03-MacOSX-x86_64.sh
For Python 2.7, run
bash ~/Downloads/Anaconda2-2019.03-MacOSX-x86_64.sh
Keep in mind that if you didn’t download Anaconda to your downloads folder, you’ll need to change that path.
Review the license agreement and accept it by hitting “Enter” about a million times until you get to the end. At the end, type “yes” if you want to agree.
(It will prompt you. It will prompt you repeatedly if you miss it.)
If you’re happy with the location it suggests, hit “Enter.” You can change locations or cancel the installation by entering CTRL-C. Pay attention to this location! It will come in handy if you get the error message.
Now hold still. Be patient. This can take a few minutes and you won’t know if anything is happening at first. You could go and grab a beverage if you want, but be quick because the installer will ask, “Do you wish the installer to initialize Anaconda3 by running conda init?” and you almost certainly want to be there to type “yes” when you’re asked.
“Thank you for installing Anaconda!”
When your installation is complete, close your terminal window and open a new one for the changes to take effect. Want to see if it worked? In your terminal, type a command like
conda list
and see what happens. If it works, you’ll see something like this
This is a very useful command if you want to know which packages and versions you have installed in your environment!
If that worked, congratulations!!! You are now ready to start using Anaconda, Jupyter Notebooks, Spyder, and all of the other good stuff. Time to celebrate!
On the other hand, you might be seeing this:
The solution to this problem is actually very simple.
(I know I already said that, but the Anaconda team reached out and let me know that not refreshing your terminal is the most common reason that people have problems at this point.)
You’ll need to know where your Anaconda binary directory is and what your username is. If you were paying attention during your installation, you already have this information! You want the location that you specified during installation.
If not, there’s a very, very good chance that if you simply followed the installation instructions, your directory is /Users/ if you’re on a macOS. (Or /home/ on Linux or \Users\ on Windows10.)
If you don’t know your username, run
echo $USER
I recommended modifying PATH to fix the problem (you can see the original fix below), but the Anaconda team reached out with some incredibly helpful information!
They don’t recommend modifying PATH!
You can read the full comment below, but here is a condensed version of the information that you can find in the comments section from the amazing Michael Sarahan:
“Modifying PATH can cause problems if there are any other programs on your system that have the same names, that Anaconda then hides (shadows) by being found first. What “conda init” does is to set up a conda “shell function” and keep the other stuff off PATH. Nothing but conda is on PATH. It then defaults to activating your base environment at startup. The net effect is very much like your PATH addition, but has some subtle, but critically important differences:
activation ensures that anaconda’s PATH stuff is right up front. Putting anaconda at the front of PATH permanently is good in that it prevents confusion, but bad in that it shadows other stuff and can break things. Activation is a less permanent way to do this. You can turn off the automatic activation of the base environment using the “auto_activate_base” condarc setting.
activation does a bit more than just modifying PATH. It also sources any activate.d scripts, which may set additional environment variables. Some things, such as GDAL, require these. These packages will not work without activation.
So, rather than your tip on modifying PATH, what we recommend is following the directions at the end of the installer, should you choose not to run conda init:
...
that will look like 2 commands:
1. eval “$(/home/msarahan/mc3_dummy/bin/conda shell.bash hook)”
2. conda init
Note that in step 1, I changed the shell name from YOUR_SHELL_NAME to bash. You may need to adjust that yourself to your shell.”
Long story short, it’s entirely possible that modifying PATH might cause you some problems down the road. It’s smarter to go with activation because it isn’t permanent and will allow some packages to run that might not otherwise.
So, at the end of the installer, instead of running the original solution:
export PATH="/Users/myname/anaconda2/bin:$PATH"
You want to run
eval "$(/home/msarahan/mc3_dummy/bin/conda shell.bash hook)”conda init
(But make sure you change the directory location and shell to your own!)
***End of update***
All you need to do to make things work is run
export PATH="/Users/myname/anaconda3/bin:$PATH"
and you’re good to go!!!
(Definitely make sure you replace “myname” with your username and change the path to the directory if you need to. If you look at the line above, /Users/myname/anaconda3 would be replaced by /Users/anneb/anaconda3 from the installation location earlier. Keep everything else the same.)
If you didn’t install Anaconda3, you would need to run
export PATH="/Users/myname/anaconda2/bin:$PATH"
for Anaconda2 or
export PATH="/Users/myname/anaconda/bin:$PATH"
making sure to change “myname” and the directory location if necessary.
Now try conda list!
You did it!!!
You successfully installed Anaconda and are now able to run it from your command line!
Try typing
jupyter notebook
and see what happens!
Thanks for reading! If you want to reach out or find more cool articles, please come and join me at Content Simplicity!
Want to learn about the easiest way to create, run, publish, and even share Jupyter Notebooks?
towardsdatascience.com
Thanks for reading!