Install Python by Homebrew
To install a specific version of Python using Homebrew, follow these steps:
1. Update Homebrew
First, ensure that Homebrew is up to date by running:
brew update
2. Check Available Versions:
Use the below command to see the available Python versions in Homebrew:
brew search python@
3. Install the Desired Version
You can install a specific version of Python using Homebrew by specifying the formula. For Python 3.10, you can use the following command:
brew install python@3.10
Homebrew will install the latest version of Python 3.10.x.
4. Verify the Installation
After the installation is complete, verify that Python 3.10 is installed correctly by checking the version:
python3.10 --version
This should output like this:
Python 3.10.8
5. Set Python 3.10 as the Default (Optional)
If you want to make Python 3.10 the default version of Python on your system, you can add it to your PATH. You can do this by exporting the Python path to your shell configuration file (e.g., .bashrc
, .zshrc
, etc.).
You can find the path to the Python executable by running:
which python3.10
This should output like this:
/opt/homebrew/bin/python3.10
Then you can run the following command to add the path to your shell configuration file:
echo 'export PATH="/usr/local/opt/python@3.10/bin:$PATH"' >> ~/.zshrc
Then, reload your shell configuration:
source ~/.zshrc
Now, when you run python3 --version
, it should show Python 3.10.8.
6. Create a Symlink for python (Optional)
If you want to use the command python
(instead of python3
) to refer to Python 3.10, you can create a symlink:
ln -s /opt/homebrew/bin/python3.10 /opt/homebrew/bin/python
Then, verify it:
python --version
7. Install pip (if not already installed)
Python 3.10.8 should come with pip
pre-installed. You can verify this by running:
python3.10 -m ensurepip --upgrade
If pip
is not installed, you can install it using:
python3.10 -m ensurepip
8. Upgrade pip (Optional)
It’s a good idea to upgrade pip
to the latest version:
python3.10 -m pip install --upgrade pip
9. Install Virtual Environment (Optional)
You can create a virtual environment using Python 3.10.8:
python3.10 -m venv myenv
Activate the virtual environment:
source myenv/bin/activate
Now, you can install packages within this virtual environment.
Conclusion
You have successfully installed Python 3.10 using Homebrew. You can now use it for your projects, either globally or within a virtual environment.