Sklearn installs through pip, the Python package manager, in a single command
Sklearn (scikit-learn) is a Python library for machine learning. You install it by opening your terminal or command prompt and running a single line of code. The whole process takes a few minutes and works the same way on Windows, Mac, and Linux — the only difference is how you open the terminal.
Before you install sklearn, you need Python 3.9 or newer already on your computer, and you need pip (Python's package installer). Most Python installations include pip automatically. If you do not have Python yet, download it from python.org and run the installer — make sure to check the box that says "Add Python to PATH" on Windows.
Key Takeaways
- Sklearn installs in one command through pip: pip install scikit-learn
- You must have Python 3.9 or newer and pip already installed before you start.
- On Windows, open Command Prompt or PowerShell; on Mac and Linux, open Terminal.
- After installation finishes, test it by opening Python and typing import sklearn — if no error appears, the install worked.
- If the install fails, the most common cause is pip pointing to the wrong Python version or needing an upgrade itself.
Check that Python and pip are already installed
Open your terminal or command prompt and type this command, then press Enter:
python --version
You should see a version number like "Python 3.11.5" or higher. If you see "command not found" or "is not recognized", Python is not installed or not in your PATH. Go to python.org, download the latest version, and run the installer. On Windows, check the box labeled "Add Python to PATH" before you click Install.
Next, check that pip is installed by typing:
pip --version
You should see something like "pip 23.2.1 from /usr/local/lib/python3.11/site-packages/pip". If you see an error, pip is missing or your Python installation is broken. Reinstall Python and make sure the installer includes pip — it is checked by default.
Install sklearn with pip
Once you have confirmed Python 3.9+ and pip are working, type this command and press Enter:
pip install scikit-learn
Pip will download sklearn and all the libraries it depends on (numpy, scipy, and others). This usually takes one to three minutes depending on your internet speed. You will see lines of text scrolling past as packages download and install. When it finishes, you should see a line that says "Successfully installed scikit-learn" followed by a version number.
Do not close the terminal or interrupt the process while it is running. If your internet drops or you accidentally close the window, run the command again — pip will pick up where it left off.
Test that sklearn installed correctly
After the install finishes, verify it worked by opening Python in your terminal. Type:
python
You should see a prompt that looks like >>> and a line showing your Python version. Now type:
import sklearn
If nothing happens and you see the >>> prompt again, the install worked. If you see an error like "ModuleNotFoundError: No module named 'sklearn'", the install failed — scroll down to the troubleshooting section.
To exit Python, type exit() and press Enter, or press Ctrl+D on Mac and Linux (Ctrl+Z on Windows).
Install sklearn in a virtual environment (recommended for projects)
If you are working on a specific project, create a virtual environment first. This keeps sklearn and other libraries separate from your main Python installation, so different projects can use different versions without conflicts.
In your terminal, navigate to your project folder and type:
python -m venv sklearn_env
This creates a folder called sklearn_env that holds an isolated Python setup. Activate it by typing one of these commands depending on your operating system:
- Windows: sklearn_env\Scripts\activate
- Mac and Linux: source sklearn_env/bin/activate
After activation, your terminal prompt will show (sklearn_env) at the start. Now type pip install scikit-learn as before. When you are done working, type deactivate to exit the virtual environment.
Troubleshoot a failed install
If the install failed or sklearn does not import, the most common cause is pip pointing to the wrong Python version. Type this to see which Python pip is using:
pip --version
Look at the path shown — it should match the Python version you checked earlier. If it does not, or if you have multiple Python versions installed, use this command instead:
python -m pip install scikit-learn
This forces Python itself to run pip, which guarantees they match. If that still fails, upgrade pip first by typing:
python -m pip install --upgrade pip
Then try the sklearn install again. On Mac, if you installed Python from python.org but also have an older version from Homebrew, you may need to uninstall the Homebrew version or use the full path to the newer Python. On Linux, if you installed Python from your package manager, you may need to install python3-pip separately — check your distribution's documentation.
Verify the install with a simple test
After sklearn installs and imports without error, run a quick test to make sure it is fully functional. Open Python and type:
from sklearn import datasets
iris = datasets.load_iris()
print(iris.data.shape)
If you see (150, 4) printed, sklearn is working correctly. This test loads a built-in dataset and checks its size — if sklearn was broken, this would fail.
Frequently Asked Questions
Do I need to install numpy and scipy separately before sklearn?
No. When you run pip install scikit-learn, pip automatically downloads and installs numpy, scipy, and every other library sklearn needs. You do not have to do anything else.
What if I get a "permission denied" error on Mac or Linux?
This usually means pip is trying to install to a system folder you do not have permission to write to. Use a virtual environment instead (see the section above), or add --user to the command: pip install --user scikit-learn. The --user flag installs to your home folder instead.
Can I install sklearn with Anaconda instead of pip?
Yes. If you have Anaconda installed, open Anaconda Prompt and type conda install scikit-learn. Anaconda is another package manager that works the same way as pip. Use whichever one came with your Python installation.
How do I update sklearn to a newer version?
Type pip install --upgrade scikit-learn or pip install scikit-learn --upgrade. Pip will download the latest version and replace the old one. Your code will keep working unless the new version changed how a function works, which is rare.
Why does the install take so long?
Sklearn depends on several large libraries like numpy and scipy. The first install downloads all of them, which can take a minute or two on a slow connection. Future installs of other packages are usually faster because pip reuses libraries that are already there.