Enable OpenMP on macOS For Building scikit-learn#

The default C compiler on macOS, Apple clang (confusingly aliased as /usr/bin/gcc), does not directly support OpenMP.

Below we present a solution to this issue: install libomp with Homebrew to extend the default Apple clang compiler.

Please note that if you are using Apple Silicon M1 hardware, this solution might not work. You may need to refer to Scikit-learn Official Documentation for another solution.


First, install the macOS command line tools:

$ xcode-select --install 

Install the Homebrew package manager for macOS.

Install the LLVM OpenMP library.

$ brew install libomp

Set the following environment variables:

$ export CC=/usr/bin/clang
$ export CXX=/usr/bin/clang++
$ export CPPFLAGS="$CPPFLAGS -Xpreprocessor -fopenmp"
$ export CFLAGS="$CFLAGS -I/usr/local/opt/libomp/include"
$ export CXXFLAGS="$CXXFLAGS -I/usr/local/opt/libomp/include"
$ export LDFLAGS="$LDFLAGS -Wl,-rpath,/usr/local/opt/libomp/lib -L/usr/local/opt/libomp/lib -lomp"

Now, you should be able to compile your scikit-learn properly. For further questions, please refer to this or Scikit-learn Official Documentation.