"
This article is part of in the series

Installing Scikit Learn Using pip

If you're working on building machine learning applications, coding from scratch isn't such a good idea. 

Relying on libraries such as the popular open-source scikit-learn library is an excellent way to get started – whether you're a novice or an experienced Python programmer.

But how do you install scikit-learn on your machine? Turns out, there are a few different ways of going about it.

In this guide, we will discuss the different methods of installing scikit-learn and walk you through the fastest and simplest installation method.

How to Install Scikit Learn?

If you're using a Python distribution or an operating system that distributes the scikit-learn package, you can install it directly. 

However, installing it from the OS or distribution might result in an older version of the library being installed. So, while this method is easy, it's not something we would recommend.

The second method, as you might have guessed, is manually building the package from the source. If you're looking to use the newest features and aren't afraid to get your hands dirty by compiling the package by hand, this is the right option. 

Experienced developers who contribute to the scikit-learn project rely on this method. But needless to say, it's not for everyone. 

This brings us to the third and recommended installation method, which involves using the stable pre-built package available for most platforms.

Installing Scikit Learn Using pip

Installing On Windows 

Before you begin, you must install the newest version of Python 3 on your Windows computer, preferably from the official site.

Then, you can run the following in the command prompt to install the library:

pip install -U scikit-learn

The command should complete the installation without errors in most cases. However, it's best to double-check that the package is correctly installed. 

You can do this by running:

python -m pip show scikit-learn  

The command will show you which package version has been installed and where. Alternatively, you can run the following command to view the installed packages in the active virtual environment: 

python -m pip freeze
python -c "import sklearn; sklearn.show_versions()"

Installing On macOS

The first step of installation is only slightly different from that of Windows. You can visit the official site to install Python. But it's faster to run the following command on a terminal and install Python 3 with homebrew:

brew install python

When Python 3 is ready on your machine, you can run this on the terminal:

pip install -U scikit-learn

Now, it's time to double-check that the package was installed correctly. The following command will display what version of scikit learn was installed and where:

python -m pip show scikit-learn

To see the installed packages in an active virtual environment, run this:

python -m pip freeze  
python -c "import sklearn; sklearn.show_versions()"

Installing On Linux

Before installing Scikit Learn on Linux, you must install python3 and python3-pip on your machine. You can then run this command without any errors to install the package:

pip3 install -U scikit-learn

The following command will show you where your scikit-learn package was installed and also show you its version information:

python3 -m pip show scikit-learn

To check the installed packages in an active virtual environment, you can run:

python3 -m pip freeze 
python3 -c "import sklearn; sklearn.show_versions()"

It's best to use a conda environment or a virtual environment when using scikit-learn. If you don't use either, conflicts may arise between scikit-learn and other packages installed on your machine. 

Moreover, if you use an isolated environment, you can install specific versions of the package with conda or pip and then install its dependencies separately. This allows you to prevent your newly installed package from using previously-installed dependencies.

Doing this is especially important on Linux, as installing pip packages alongside the distro's package manager, be it apt, pacman, or anything else, can break the software.

Bear in mind that you must activate the virtual environment (or conda environment) before running any Python commands on new terminal sessions. 

It's also important to remember to install Matplotlib on your machine. The plotting features of Scikit Learn will only work if the Matplotlib is installed on your machine and working correctly. 

Installing Scikit Learn Using conda

If you prefer using the Anaconda installer to install new packages on your computer, you can run the following commands to install scikit-learn:

conda create -n sklearn-env -c conda-forge scikit-learn
conda activate sklearn-env

As you can tell, the second command activates the conda installation. 

Bear in mind that you do not require administrator permission to run these commands. However, you do need to check that the installation was completed correctly.

You can do this by running the following to check the version details of your Anaconda installation:

conda list scikit-learn

You can also check the installed packages by running this command in an active conda environment:

conda list
python -c "import sklearn; sklearn.show_versions()"

Installing Scikit Learn via Third-Party Distributions

One of the several advantages of doing your Python programming on Linux is that many of the distributions come with their own versions of Scikit Learn. 

We'd touched on this briefly earlier. In this section, we will explore how you can install versions of scikit-learn that come integrated into the package managers of some distros. 

Before we begin, it's worth noting that using these integrated versions of scikit-learn makes both installation and upgrading more straightforward for Python programmers. This is because these integrated packages automatically install the required dependencies.

Arch Linux, Fedora, and all Debian-based distros come with such packages integrated into their package managers. If you're using Ubuntu or Debian, it's worth learning that the Scikit Learn package is split into three separate packages:

  • The python3-sklearn package, which comprises Python modules;
  • The python3-sklearn-lib package, which has bindings and low-level implementations; and
  • The python3-sklearn-doc package, which contains the documentation for Scikit Learn. 

Interestingly, you can install all three with a single command:

sudo apt-get install python3-sklearn python3-sklearn-lib python3-sklearn-doc

On Arch Linux, you can install the package with this command:

sudo pacman -S python-scikit-learn

And finally, on Fedora, you can install the package by running:

sudo dnf install python3-scikit-learn