Install and download Python in Mac
This article is about downloading and installing Python for Mac OS users. It should take you less than five minutes, and those with M1 CPUs should be able to do the same.
There are several ways to install Python. Today we'll cover three of them
- The official Python installer
- Homebrew
- Conda or Mamba (Anaconda package manager)
If you're using Windows, click on Download and Install (Win).
When you install Python, you install a program that interprets and executes your Python code. With that in mind, let's walk through the process of downloading the installation file and installing the Python executable.
1. Check for installed Python
Python is actually installed by default on the Mac OS. Let's check it by typing the following command in the default terminal or another terminal emulator.
python3
If you happen to get an error that starts with xcrun:
, you can fix it like this.
If all went well, the Python REPL will run as shown above. Note that the built-in version of Python is relatively older than the latest version distributed from the official website.
Older versions of Python are more stable, even if they lack some of the latest features. Since this is the version that Mac OS and Xcode run on, it is installed with stability in mind. If you want the latest version, you'll need to download and install it.
2. How to download and install the official Python installer
You can download the official Python installer from the official site. Click here to visit it.
Official website: https://www.python.org (opens in a new tab)
On the screen you see when you arrive, you should see a series of blue menus. Hover your mouse over Downloads and a window like the one above will open. The site will automatically detect your operating system and show you a button to download the latest version.
For me, version 3.11.2
is the latest. For reference, this is the 64-bit version. Click on the gray button to download the file.
You can also download earlier versions by clicking the View the full list of downloads
link below the button.
Open the downloaded .pkg
file to proceed with the installation.
If you continue from here...
At this point, the installation is complete,
This will open the folder with the latest version of Python installed.
3. Download and install Python using Homebrew
Homebrew (opens in a new tab) is a popular Mac OS package manager. Homebrew allows you to install, uninstall, and manage applications using CLI tools such as the Terminal.
Typical Mac users install applications by downloading installation files with extensions like .pkg
or .dmg
from websites, like the installer in Section 2.
This is a GUI-based package installation method, which has the advantage of making application installation easy and intuitive.
On the other hand, CLI-based tools like Homebrew lack visual resources and can feel like a barrier to entry when you first get started. However, once you get the hang of it, you'll find many benefits, including faster application installation times and the ability to install multiple applications at once using scripts.
To install Python using Homebrew, you'll first need to install Homebrew. Type the command below.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
If it installed successfully, you can install the latest version of the Python executable using the command below.
brew install python
The brew install
command first updates the homebrew package list to the latest version, then installs all the necessary dependencies for installing Python.
Finally, it installs the latest version of Python.
Once the installation is complete, use the verify version command to make sure it's working properly.
$ python3 --version
Python 3.11.3
The executable and symlink paths for Python installed with Homebrew are shown below.
$ which python3
/usr/local/bin/python3
$ ls -l /usr/local/bin/python3
lrwxr-xr-x@ 1 baeseongho admin 40 Jun 9 22:35 /usr/local/bin/python3 -> ../Cellar/python@3.11/3.11.3/bin/python3
This means that a symlink to the actual executable in the path /usr/local//Cellar/python@3.11/3.11.3/bin/python3
is stored in /usr/local/bin/python3
,
we can run this REPL when we type the command python3
.
4. How to download and install Conda / Mamba
The Anaconda distribution is synonymous with data science and is one of the most popular distributions. Used by countless developers and researchers, the Anaconda distribution is based on the Conda package manager.
The Conda package manager has many more features than the traditional Python package manager, Pip. It supports easy installation of other language-based packages such as NumPy, SciPy, and Pandas, which are difficult to install, and manages its own virtualization environment, channel features, package extensions, and more. This makes setting up a development environment faster and more convenient.
Installing Python is also easy with Conda.
First, you'll need to install the Anaconda or Miniconda distributions. The download sites are as follows
- Anaconda: download page (opens in a new tab)
- Miniconda: download page (opens in a new tab)
From either site, you can download and install the appropriate installation files for your operating system. Miniconda is a distribution with only Python and Conda installed, without any other data science libraries.
4.1. Download and install Miniconda
You can use the install file, but we'll install it using homebrew for a more organized package management. Check if you can install Miniconda with the following command.
$ brew search miniconda
==> Formulae
minica minicom minidlna
==> Casks
miniconda minizincide
You can see that it's included with Cask, which is a homebrew extension.
It is installed by adding the --cask
option.
brew install --cask miniconda
If it completes successfully, check that Conda is running.
$ conda --version
conda 23.3.1
4.2. Download and install Micromamba
There is a package manager that does the same job as Conda, uses the same commands, and is much faster. It's called Mamba (opens in a new tab).
Mamba achieves its performance gains over Conda by using faster dependency management algorithms and taking full advantage of concurrent package downloads. Among the Mamba executables, Micromamba, which is entirely in C++, has the advantage of being the smallest, fastest, and Python-independent executable.
So let's install Micromamba using Homebrew.
brew install micromamba
After installation, use the following command to register your shell environment variable. Enter your shell name in the <shell_name>
location, such as bash
, zsh
, fish
, etc.
/usr/local/opt/micromamba/bin/micromamba shell init -s <shell_name> -p ~/micromamba
After restarting the terminal, verify that you installed it with the command micromamba
instead of conda
.
$ micromamba --version
1.4.4
4.3. Installing Python with the Conda / Mamba package manager
To use Python executables with Conda or Mamba, you need to create a virtual environment. A virtual environment is a way to install Python and other packages in a specific directory and only load them when you want to use them. Let's walk through the installation process to understand it.
To create a new VM, we use the create
command. It uses the following options
n (--name)
: Set the name of the environment.c (--channel)
: Set the repository (channel) to download packages to install in the VM (e.g.defaults
,conda-forge
...)python=
syntax: Suggests the version of Python to install in the VM. Currently it can be set up to3.10
.
Micromamba is used, but
conda
andmamba
commands are also acceptable.
$ micromamba create -n global_env -c conda-forge python=3.10
__
__ ______ ___ ____ _____ ___ / /_ ____ _
/ / / / __ `__ \/ __ `/ __ `__ \/ __ \/ __ `/
/ /_/ / / / / / / /_/ / / / / / / /_/ / /_/ /
/ .___/_/ /_/ /_/\__,_/_/ /_/ /_/_.___/\__,_/
/_/
conda-forge/noarch 12.5MB @ 2.4MB/s 5.8s
conda-forge/osx-64 28.8MB @ 3.5MB/s 9.5s
...
After setting up Anaconda's license-free conda-forge
channel, I created a virtual environment.
Python and packages installed in this environment are stored in the ~/micromamba/envs/global_env
directory.
If you used conda
or mamba
, you can find this directory in the output of the cd && ls -al
command.
Depending on how you installed it, you may find directories like anaconda3
, miniconda3
, miniforge3
, etc.
Now enable the newly created virtual environment.
$ micromamba activate global_env
$ python3 --version
Python 3.10.11
If you use the Starship (opens in a new tab) prompt like I do, it intuitively displays the current virtual environment like this
5. Conclusion
There you have it, three ways to install Python on Mac OS.
I recommend the official installer if you're not already familiar with the CLI environment, homebrew if you are, and Conda/Mamba if you're using multiple virtual environments.
Personally, I'm using the Micromamba package manager :)
