Every major operating system ships with Python already installed — but you should almost never use or modify that system Python. It’s there for the OS, not for you, and changing it can break system tools.
Here’s how to install Python properly on each platform.
The short answer
- Ubuntu/Linux → use
pyenvorapt(but not the system Python) - macOS → use
pyenvor Homebrew - Windows → use
wingetor the official installer (Python 3.x is fine here)
Install Python on Ubuntu / Debian Linux
Ubuntu ships with Python 3 pre-installed. Check the version:
python3 --version
If it’s outdated or you need a specific version for a project, use one of these methods.
Method 1: apt (install a specific version)
sudo apt update
sudo apt install python3.12 # or 3.11, 3.13, etc.
python3.12 --version
To make it the default python3:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1
Method 2: pyenv (recommended for developers)
pyenv lets you install and switch between multiple Python versions per project — essential when you work on projects with different Python requirements.
# Install build dependencies
sudo apt update
sudo apt install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
# Install pyenv
curl https://pyenv.run | bash
# Add to your shell config (~/.bashrc or ~/.zshrc)
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
# Reload
source ~/.bashrc
# Install Python
pyenv install 3.12.3
pyenv global 3.12.3
python --version
Install Python on macOS
macOS ships with Python 3 via Xcode CLT (or a stub that installs it). The system Python lives in /usr/bin/python3 — don’t touch it.
Method 1: pyenv (recommended)
# Install Homebrew first if needed
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install pyenv
brew install pyenv
# Add to ~/.zshrc (macOS default shell is zsh)
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
source ~/.zshrc
# Install a Python version
pyenv install 3.12.3
pyenv global 3.12.3
python --version
Method 2: Homebrew
brew install python
python3 --version
Homebrew installs Python at /opt/homebrew/bin/python3. It won’t conflict with the system Python.
Install Python on Windows
Method 1: winget (recommended)
winget install Python.Python.3.12
Restart your terminal and verify:
python --version
pip --version
Method 2: Official installer
- Go to python.org/downloads
- Download the latest Python 3.x installer for Windows
- Run it
Important: Check “Add Python to PATH” on the first screen of the installer. Without this, python won’t work from the command line.
After install:
python --version
pip --version
Verify pip is installed
pip comes with Python 3.4+. Check it:
pip --version
# or
pip3 --version
If it’s missing:
python -m ensurepip --upgrade
Virtual environments — always use them
Never install packages globally. Create a virtual environment per project:
cd my-project
# Create a virtual environment
python -m venv .venv
# Activate it
source .venv/bin/activate # Linux/macOS
.venv\Scripts\activate # Windows (Command Prompt)
.venv\Scripts\Activate.ps1 # Windows (PowerShell)
# Your prompt changes to show (.venv)
# Now install packages
pip install requests
# Deactivate when done
deactivate
The .venv directory stays in your project folder. Add it to .gitignore:
echo ".venv/" >> .gitignore
Managing dependencies
Save your project’s dependencies:
pip freeze > requirements.txt
Install on another machine:
pip install -r requirements.txt
For more modern dependency management, consider uv — a fast Rust-based Python package manager:
pip install uv
uv pip install requests # much faster than pip
Switching Python versions with pyenv
pyenv versions # list installed versions
pyenv install 3.11.9 # install another version
pyenv local 3.11.9 # set version for current directory only
pyenv global 3.12.3 # set global default
pyenv local creates a .python-version file in the directory — commit this to give teammates the same Python version automatically.
Working with API keys? Read How to Set Up a .env File and Stop Leaking Secrets.
Related Reading.
How to Install VS Code on Ubuntu, macOS and Windows
Install Visual Studio Code on any OS using apt, Snap, Homebrew, or winget — with the essential extensions every developer should add first.
How to Install Docker on Ubuntu, macOS and Windows
Install Docker Desktop or Docker Engine step-by-step on Ubuntu, macOS, and Windows — including post-install setup, running your first container, and Docker Compose.
How to Install Node.js on Ubuntu, macOS and Windows
Install Node.js using nvm, Homebrew, or the official installer — step-by-step for Ubuntu, macOS, and Windows. Includes switching versions and verifying your install.