How to Install Python 3 on Mac M1 (with pyenv)
Open up your terminal, because we will do everything with command line. Don’t be intimidated by it. Just follow the steps. And it will be done in no time. Very easy and straightforward.
1. Install Homebrew
Run the command bellow to install homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
You might be prompted to install command line tools for Xcode, and it will take a few moment to download and install them.
After the installation is completed, you need to set the PATH so that you can run homebrew with the command brew
from your terminal. If your homebrew is installed successfully it will show you how to do it. Just follow the steps or you can just copy paste the commands.
2. Install pyenv and python 3
Run these commands:
- brew install pyenv
- pyenv install 3.10.2
(to install the latest python 3.10)
If you run pyenv versions
it will show you the installed python in your system.
Here is how to set the python 3.10.2 as the default:
- pyenv global 3.10.2
Or if you want to set it run locally in the current folder:
- pyenv local 3.10.2
Now, run python on your terminal. You will notice that you are still running python 2.7, right? It is because you still need to set the PATH so that pyenv can run properly. Type this command:
PATH=$(pyenv root)/shims:$PATH
Now, run python again! It has changed to python 3.10 right? But if you close the terminal and rerun python it will be back to default python 2.7. Why? Because, you need to set the PATH permanently. Here is how:
echo 'PATH=$(pyenv root)/shims:$PATH' >> ~/.zshrc
Done! Easy right? Now you can run any python version in your Mac.