To install Python via the terminal, use brew install python on macOS, sudo apt update && sudo apt install python3 on Ubuntu/Debian Linux, or winget install Python.Python.3 on Windows. This process automates the download and configuration of the Python interpreter, allowing you to run scripts and install libraries without manually navigating website download pages.
Installing Python through a command-line interface (CLI) is the preferred method for developers because it allows for easier version management and integration with other development tools. Depending on your operating system, the terminal application you use will differ: Windows users use PowerShell or Command Prompt, macOS users use Terminal, and Linux users use their distribution's default shell (such as Bash or Zsh).
What You'll Need
- A computer with an active internet connection.
- Administrative privileges (Administrator access on Windows or sudo access on macOS/Linux) to authorize the installation.
- A terminal application (PowerShell, Terminal.app, or a Linux shell).
- For macOS: Homebrew installed (the standard package manager for Mac).
Step-by-Step
Option 1: Windows (using winget)
- Right-click the Start button and select Terminal (Admin) or PowerShell (Admin). Administrative rights are required to write to the system folders.
- Type the following command and press Enter:
winget install Python.Python.3 - If prompted to agree to the source terms, type
yand press Enter. - Wait for the progress bar to complete. The system will automatically download the latest stable version of Python 3 from the official source.
- Close the terminal and reopen it to refresh the system environment variables.
- Verify the installation by typing
python --version.
Option 2: macOS (using Homebrew)
- Open Terminal from Applications > Utilities.
- Ensure Homebrew is installed. If you don't have it, paste the installation script from the official brew.sh site first.
- Type the following command and press Enter:
brew install python - Homebrew will download Python and its dependencies, including
pip(the Python package manager). - Once the process finishes, verify the installation by typing
python3 --version. - Note that on macOS, you must use
python3instead ofpythonin the terminal to ensure you are using the version you just installed rather than the legacy system version.
Option 3: Linux (Ubuntu/Debian)
- Open your terminal.
- Update your local package index to ensure you get the latest version available in the repositories. Type:
sudo apt updateand press Enter. - Enter your user password when prompted.
- Install Python by typing:
sudo apt install python3and press Enter. - When asked if you want to continue, type
yand press Enter. - Verify the installation by typing
python3 --version.
Common Mistakes
- Forgetting Administrative Privileges: Trying to run
sudoorwingetwithout an admin terminal often results in a "Permission Denied" or "Access Denied" error. - Confusing 'python' and 'python3': On many systems (especially Linux and macOS), the command
pythonrefers to the outdated Python 2.x. Always usepython3to ensure you are running the modern version. - Skipping the Path Update: On Windows, if you install Python manually rather than via winget, forgetting to check the "Add Python to PATH" box prevents the terminal from recognizing the
pythoncommand. - Not Updating Package Lists: On Linux, attempting to install Python without running
apt updatefirst can lead to "Package not found" errors because the system is looking for a version that has been replaced in the repository.
Tips
- Install pip: Python's package manager,
pip, is usually installed automatically with these methods. Check it by typingpip3 --version. You will need this to install external libraries like NumPy or Pandas. - Use Virtual Environments: To avoid cluttering your system Python installation, use
python3 -m venv myenvto create a project-specific environment. - Check for Pre-installs: Many Linux distributions and macOS versions come with Python pre-installed. Always run
python3 --versionbefore installing to see if you already have it.
Troubleshooting
If the installation does not go to plan, follow these steps in order:
- Restart the Terminal: The most common issue is that the terminal does not yet "know" where Python was installed. Closing and reopening the window refreshes the PATH environment variables.
- Check your Internet Connection: Terminal installers rely on stable connections to fetch packages from remote servers. If the download hangs, check your firewall or proxy settings.
- Verify Admin Rights: If you see "Permission Denied," ensure you are using
sudoon Unix-based systems or running the terminal "As Administrator" on Windows. - Reinstall the Package Manager: If
breworwingetis not recognized, the issue is with the manager, not Python. Reinstall Homebrew (Mac) or ensure the App Installer is updated via the Microsoft Store (Windows).
Frequently Asked Questions
Do I need to restart my computer after installing?
Generally, no. You only need to restart the terminal application to update the environment variables so the python command is recognized.
What is the difference between Python 2 and Python 3?
Python 2 is legacy software and is no longer supported. Python 3 is the current standard and contains critical security updates and new features; you should always install Python 3.
How do I know if Python is already installed?
Open your terminal and type python3 --version. If it returns a version number (e.g., Python 3.11.2), it is already installed.
Can I install multiple versions of Python?
Yes. Tools like pyenv (for Mac/Linux) or the Python Launcher for Windows allow you to manage and switch between different versions of Python on the same machine.
Is it safe to use sudo to install Python?
Yes, sudo is required on Linux and macOS to install software into system-wide directories. However, for installing specific libraries later, it is safer to use a virtual environment than to use sudo pip.