Getting Started with Anaconda on Macs
The data science community developed Conda to make life with Python easier. Conda handles Python language versions, Python packages, and associated native libraries. It’s both an environment manager and a package manager. And, if you need a package that Conda doesn’t know about, you can use pip
within a conda
environment to grab the package.
This site will primarily use Python environments in its tutorial. Therefore having some basic instructions on how to configure your environment is important. In this tutorial, you'll learn how to:
- Unstall Anaconda on your Mac
- How to create an environment
- How to manage your environment
Conda, itself, comes in two distributions:
- Miniconda: Includes only the packages needed to run Conda. (400 MB)
- Anaconda: Includes all of the standard packages needed for machine learning. (2 GB)
For all the Python projects on this site, it'll be assumed Anaconda is installed on your machine unless otherwise stated. It doesn’t take long to install, and it’s way more convenient than trying to install Python directly!
Installing Anaconda
In a browser, navigate to https://www.anaconda.com/download/#macos, and download the 64-bit Command Line installer with the latest Python available.
At the time of writing, the filename downloaded is called Anaconda3-2019.10-MacOSX-x86_64.sh
. After downloading is complete, open up a Terminal and navigate to the directory in which you downloaded the installer. You can run the installer by running the following command in the terminal:
sh Anaconda3-2019.10-MacOSX-x86_64.sh
You’ll have accept the licence agreement, and then give the installer a directory to install Anaconda (or accept the default location if that works for you). Once the installation starts, it may take a while.
If you're asked to run conda init
, type yes
. Once installation is complete, restart Terminal. Once restarted, you can try to run the following command to check that the installation succeeded.
conda --version
If the above command fails with a command not found
message, chances are you'll need to add the Anaconda install path to to your global path environment variable. This means, you'll have to edit the .bashrc
or .zshrc
file in your home directory (usually found /Users/<username>/
). If the file doesn't exist, you'll have to create based on the shell your terminal is currently using. If you're running Catalina or later, this mean your current shell is most likely Zsh
. You can run the following command to see which shell you're currently using:
echo $SHELL
In either case, open or create a your .zshrc
or .bashrc
and either find or add a line that resembles the one given below. Assuming you installed Anaconda in your home directory, the line could look:
export PATH="/Users/<username>/anaconda3/bin":"${PATH}"
In this line, you prepend the path to the anaconda installation to your existing path. Two important things to note include the inclusion of the bin
directory to the path, and the colon separating the installation path with the existing PATH variable.
Close any existing Terminal windows, and open a new one. Try running the conda --version
command again in the new terminal. Opening a new window will pick up any changes to environment in the .zshrc
or .bashrc
file. You should have a working Anaconda installation at this point.
Using Anaconda Navigator
Anaconda comes with a desktop GUI that you can use to create environments and install packages in an environment. However, on this site, you’ll do everything from the command line unless otherwise stated. Given this fact, it’s worth going over some basic commands with Conda which you’ll do in the next section.
Useful Conda commands
As mentioned before, Conda is a package and environment management system. When working with Python projects, you’ll often find it useful to create new environments, installing only the packages you need before writing your code. In this section, we’ll explore many useful commands you’ll reuse many times when working with Python and Conda.
Below are the commands used in this chapter, along with some other useful commands.
Note: Some command options use two dashes. One-dash options are often abbreviations of two-dash options, for example,
-n
is short for--name
.
Basic workflow
Create a new environment:
conda create -n <env name>
Clone an existing environment to create a new environment:
conda create -n <new env name> --clone <existing env name>
Create a new environment from a YAML file:
conda env create -f <.yaml file>
The first line of the YAML file sets the new environment’s name. The starter folder for this chapter contains YAML files for mlenv
and turienv
. If you prefer the GUI to the command line, you can also import these into Anaconda Navigator.
Activate an environment:
conda activate <env name>
Once you’ve activated an environment, the Terminal prompt shows the name of the active environment in parenthesis, like so:
(envname) $
That way it’s always obvious what environment you’re currently using.
Install packages in an active environment:
conda install <pkg names>
Install packages in a non-active environment:
conda install -n <env name> <pkg names>
Note: A message from conda about installing multiple packages: It is best to install all packages at once so that all of the dependencies are installed at the same time.
Install non-conda packages or TensorFlow and Keras in an active environment: Use pip install
instead of conda install
. To install multiple packages, create a requirements.txt file listing the packages, one per line, then run this command:
pip install -r requirements.txt
Start Jupyter from the active environment [in a specific directory]:
jupyter notebook <directory path>
Shutdown Jupyter: Logout in the Jupyter web pages, then press Control-C-C in terminal window where server is running. (That’s not a typo, you have to press C twice.)
Deactivate an environment: Run this command in the terminal window where you activated the environment:
conda deactivate
Remove an environment:
conda remove -n <env name> --all
Or
conda env remove -n <env name>
Listing environments or packages
List the environments you’ve created; the one with the * is the currently active environment:
conda info --envs
Or:
conda env list
List packages or a specific package in the active environment:
(activeenv) $ conda list
(activeenv) $ conda list <package name>
In a non-active environment:
conda list -n <env name>
conda list -n <env name> <package name>
OK, that was a lot of commands to throw at you. However, the more Python you work with, the more these commands will come in handy. Just having them in the back of your mind will help you move more quickly. If you ever need need a quick refresher, checkout this printable Conda cheat sheet.