Installing Scikit-learn in MacOS

August 2018 | Dios Kurniawan 

Scikit-learn is an interesting library for doing machine learning work on Python. It offers regression, classification and much more, and the good thing is it’s free. However, I found it a bit challenging to install scikit-learn on Mac OS X. The documentation on scikit-learn.org is not enough to get going. I knew I could use Anaconda distribution, but I was looking for a more ‘manual’ way.

After some try-and-error, I have successfully installed scikit-learn on my MacBook. For those who might have the same problem, I am sharing the installation steps in this blog post. Just follow the steps below. This assumes you don’t have Python installed on your Mac yet.

  1. Install XCode
    To begin with, download the latest XCode from the Apple App Store if you haven’t done it. XCode is a strong IDE, but some will say, we want to run Python, why do we need XCode? Well, for some reason, XCode is needed for its command line tool for the subsequent steps below. I don’t know why, but it just works.After XCode is successfully installed in your Mac, you must install the command line tool. Open a Terminal and issue this command:

    xcode-select --install
    

    Follow the instructions until finish. Then, continue with this command to deal with the licence agreement:

    sudo xcodebuild -license
  2. Install Macports
    Apart from XCode, you will need Macports to install development tool packages. You can download it from macports.org. Just follow the installation instruction. After installation is finished, optionally execute the following command to check for updates:

    sudo port selfupdate
  3. Install Python using Macports
    Now comes the Python package itself. Using Macports, you can easily download and install Python. At the time of writing, the current version is 3.7, so use “python37” as the argument for the following command:

    sudo port install python37

    To make the default Python is set to the latest version, execute these commands:

    sudo port select --set python python37
    sudo port select --set python3 python37

    After that you may want to verify the installation. Open a new Terminal and execute:

    python -V
    

    Check if the default version is correct.

  4. Install Numpy, Scipy, Pandas and other libraries
    Because scikit-learn is built on other libraries like Scipy and Numpy, you will need installing these packages too:

    sudo port install py37-numpy 
    sudo port install spy37-scipy 
    sudo port install py37-matplotlib 
    sudo port install py37-pandas 
    sudo port install py37-statsmodels 
    sudo port install py37-pip
    sudo port select --set pip pip37
    
  5. Finally, install the scikit-learn package
    Use pip to download and install. Execute:

    sudo pip install -U scikit-learn

Check if it is correctly installed by running this in Python:

import sklearn
print(sklearn.__version__)

It should return the installed version like this:

0.19.2

That’s it! Now you can use scikit-learn in your Python program.