Last modified: Mar 25, 2025 By Alexander Williams
How to Install Scikit-learn in Python Step by Step
Scikit-learn is a powerful Python library for machine learning. It provides tools for data analysis and modeling. Installing it is easy with the right steps.
Prerequisites for Installing Scikit-learn
Before installing Scikit-learn, ensure you have Python installed. Python 3.6 or later is recommended. You also need pip, Python's package manager.
Check your Python version by running:
import sys
print(sys.version)
If Python is not installed, download it from the official website. For pip, run:
python -m ensurepip --upgrade
Install Scikit-learn Using pip
The easiest way to install Scikit-learn is with pip. Open your terminal or command prompt. Run the following command:
pip install scikit-learn
This will download and install the latest version. Wait for the process to complete. Verify the installation with:
import sklearn
print(sklearn.__version__)
If you encounter a ModuleNotFoundError, check our guide on how to solve ModuleNotFoundError.
Install Scikit-learn in a Virtual Environment
Using a virtual environment is best practice. It keeps dependencies isolated. Create one with:
python -m venv myenv
Activate the environment:
# On Windows
myenv\Scripts\activate
# On macOS/Linux
source myenv/bin/activate
Now install Scikit-learn inside the environment:
pip install scikit-learn
Verify Scikit-learn Installation
After installation, verify it works. Run a simple example:
from sklearn import datasets
iris = datasets.load_iris()
print(iris.data.shape)
The output should show:
(150, 4)
This confirms Scikit-learn is installed correctly.
Upgrade Scikit-learn to the Latest Version
To upgrade Scikit-learn, use pip with the --upgrade
flag:
pip install --upgrade scikit-learn
Check the version again to confirm the upgrade.
Common Installation Issues
Some users face issues during installation. Here are common fixes:
1. Permission Errors: Use --user
flag to install locally:
pip install --user scikit-learn
2. Outdated pip: Upgrade pip first:
pip install --upgrade pip
3. Missing Dependencies: Scikit-learn needs NumPy and SciPy. Install them first:
pip install numpy scipy
Conclusion
Installing Scikit-learn is simple with pip. Always use a virtual environment for best results. Verify the installation with a quick test.
Now you're ready to start machine learning with Scikit-learn. For more help, check our guide on solving ModuleNotFoundError.