How to Install scikit-learn (sklearn) on Windows, macOS, and Linux
TutorialsSafely install scikit-learn on Windows, macOS, or Linux with pip or conda to avoid dependency issues.

Justas Vitaitis
Key Takeaways
-
Use virtual environments to isolate your dependencies and prevent library updates from breaking your code.
-
Use pip (which defaults to pre-compiled wheels) or conda-forge to avoid compiler errors during installation.
-
Verify your setup immediately by running import sklearn to ensure the underlying math libraries are linked correctly.
As a core piece of the data science toolkit, scikit-learn (sklearn) requires a clean environment setup to function correctly. Using pip or conda inside a virtual environment is the most reliable way to prevent dependency conflicts as your projects grow.
This guide covers how to install prerequisites, set up virtual environments, test your installation, and troubleshoot common configuration errors.
scikit-learn System Requirements and Prerequisites
Before typing any installation commands, you need to ensure your base system has the right foundational pieces in place: a modern Python environment and a compatible Python interpreter.
As of 2026, you need Python 3.11 or newer to install scikit-learn. To check your current version, open up the terminal and run:
python --version
Modern package managers handle dependency resolution automatically. When you install scikit-learn, pip or conda will automatically download the necessary libraries:
- NumPy handles heavy numerical arrays
- SciPy manages advanced mathematical operations
- joblib takes care of parallel processing tasks
- threadpoolctl limits thread usage to prevent CPU stalling
- narwhals provides a lightweight DataFrame compatibility layer
Letting your package manager automatically resolve and install these dependencies ensures version compatibility and saves you from complex debugging.
How to Set Up a Virtual Environment for scikit-learn
Installing libraries globally can cause dependency conflicts, and you may end up breaking one tool while updating the other. If you use a virtual environment, you get to isolate your dependencies so projects don’t interfere with each other.
venv on Windows
Python’s standard library includes venv, allowing you to create environments directly from your terminal without third-party tools.
python -m venv myenv
myenv\Scripts\activate
(Note: If you are using PowerShell, use .\myenv\Scripts\Activate.ps1)
venv on macOS/Linux
Unix-based systems use a slightly different folder structure and activation command:
python3 -m venv myenv
source myenv/bin/activate
conda Environments
Conda is highly recommended for data science and machine learning because it manages both versions and underlying C/C++ binary libraries. As a result, it prevents compilation errors when installing complex packages.
conda create -n myenv python=3.12
conda activate myenv
How to Install scikit-learn With pip or conda
Once your virtual environment is active, the process for how to install sklearn is straightforward using either pip or conda.
Install scikit-learn Using pip
Using pip fetches pre-compiled wheels from the Python Package Index (PyPI). It automatically installs the necessary mathematical dependencies without requiring you to compile anything from source.
pip install scikit-learn
You can pin a specific version (e.g., ==1.9.0) to ensure future environment rebuilds don't introduce breaking changes from library updates:
pip install scikit-learn==1.9.0
Install scikit-learn Using conda
If you’re using Anaconda or Miniconda, make sure you install from the community-driven conda-forge channel (it’s the recommended approach). Conda-forge is particularly useful if your project requires complex non-Python dependencies (like system-level libraries) alongside scikit-learn.
conda install -c conda-forge scikit-learn
Alternative scikit-learn Installation Methods
While virtual environments are the standard, you can also get scikit-learn through system package managers or pre-packaged distributions.
OS-Level Package Managers (Linux)
While you can use OS-level package managers, we’d recommend against it. System repositories are often several versions behind, and installing Python modules and packages globally can conflict with your operating system's internal tools. But if you must use them:
- Debian/Ubuntu. sudo apt install python3-sklearn
- Fedora. sudo dnf install python3-scikit-learn
Pre-Packaged Distributions
Scientific Python distributions like the full Anaconda package or WinPython come with scikit-learn pre-installed. These distributions provide a complete data science environment out of the box, so you bypass the need for manual installation entirely.
scikit-learn Core and Optional Dependencies
The default installation includes the core libraries needed to run standard machine learning algorithms. To add plotting or advanced data frame support, you’ll need to install optional dependencies.
| Core dependency | Role in the ecosystem |
|---|---|
| NumPy | Powers the fundamental multi-dimensional array structures. |
| SciPy | Provides advanced linear algebra and sparse matrix routines. |
| joblib | Drives parallel computing features for faster model training. |
| threadpoolctl | Limits thread usage to prevent CPU stalling. |
| narwhals | Provides a lightweight DataFrame compatibility layer. |
Optional dependencies provide visualization and enhanced data handling:
- matplotlib enables you to plot decision boundaries and performance metrics natively
- pandas lets you feed tabular structures directly into your pipelines without manual conversion
- seaborn makes generating visually appealing correlation matrices incredibly simple
- scikit-image provides image processing tools for preparing photographs or scans before classification
How to Verify Your scikit-learn Installation
To verify your installation, you don’t need to train a model . Simply use scikit-learn's built-in utility to check the status of the scikit-learn library and all of its core dependencies.
You can run this command directly from your active terminal:
python -m sklearn.show_versions
This will output a detailed list showing your operating system, your Python version, and the specific versions of NumPy, SciPy, joblib, and threadpoolctl that scikit-learn is currently using.
Once the output confirms your core libraries are present and correctly versioned, your environment is ready to use.
Installing scikit-learn From Source or Nightly Builds
If you need an unreleased bug fix or want to contribute to the project, you must build scikit-learn directly from its source code. Building from source requires a C/C++ compiler and Cython installed on your system.
First, clone the repository via HTTPS and run an editable installation:
git clone https://github.com/scikit-learn/scikit-learn.git
cd scikit-learn
pip install --verbose --no-build-isolation --editable .
Using Nightly Builds
To access unreleased features without compiling from source, you can install the nightly wheels. Keep in mind that these builds are intended for testing and may contain bugs not present in stable releases.
You can install the nightly build using the scientific-python index:
pip install --pre --extra-index https://pypi.anaconda.org/scientific-python-nightly-wheels/simple scikit-learn
Troubleshooting scikit-learn Installation Errors
Even with a proper setup, you might encounter installation errors. Here are a few common issues and how to resolve them:
- Windows path limits. Windows users with deeply nested project folders might hit the OS's 260-character path limit. You can prevent extraction errors by enabling Long Paths in the Windows Registry.
- Missing compilers. If building from source, you must install Visual Studio Build Tools (Windows) or GCC (macOS/Linux) to avoid C++ compilation errors.
- Version conflicts. If Python imports an older version of scikit-learn than the one you just installed, you likely have a conflicting global installation. Uninstall the global package and ensure your virtual environment is activated.
- Permission errors. Running pip install scikit-learn globally on macOS or Linux can result in "permission denied" errors. Always ensure your virtual environment is active before installing.
If you encounter an undocumented error, checking the scikit-learn GitHub issues page is the best way to find community-tested workarounds.
FAQ
Can I install scikit-learn without installing NumPy and SciPy manually?
Yes. Package managers like pip and conda automatically resolve and install all required dependencies, including NumPy and SciPy.
Is it safe to use the version of scikit-learn that comes with my Linux distribution?
It’s not recommended. System repositories are often outdated, and installing Python libraries globally can break your Linux distribution's internal tools. Always use a virtual environment instead.
How do I install multiple versions of scikit-learn on the same computer?
Create a separate virtual environment for each project, which isolates dependencies so you can run different versions of scikit-learn on the same machine without conflict.
Do I need GPU support or special hardware to use scikit-learn?
No. Scikit-learn is primarily designed for standard CPU processing, so no special hardware is required. However, modern versions do support GPU acceleration for certain estimators via the Array API if you choose to configure it.
How can I keep scikit-learn up to date in an existing project?
Run pip install --upgrade scikit-learn or conda update scikit-learn. Always check the release notes and test your code after upgrading, as major updates occasionally deprecate older functions.