Install XGBoost and LightGBM on Apple M1 Macs | by Fabrice Daniel | Towards Data Science

In this previous article I explained how to install TensorFlow, Scikit-Learn and several other packages natively compiled for Apple M1 (arm64).
Here I explain step by step how to install two of the most powerful Gradient Boosting packages: XGBoost and LightGBM. Please note that at the time of writing this article CatBoost cannot yet be installed on M1.
LightGBM can directly be installed from Conda miniforge but XGBoost does not yet exists as a native release. The following steps enables compiling it properly.
Install Xcode Command Line Tools by downloading it from Apple Developer or by typing:
xcode-select --install
Install miniforge for arm64 (Apple Silicon) from miniforge github.
Miniforge enables installing python packages natively compiled for Apple Silicon including scikit-learn.
Brew is now compatible with M1 and installs native packages when they exist. Go to Homebrew site and copy/paste the installation command to your terminal.
Two libs must be installed from Brew to enable compiling XGBoost.
brew install cmake libomp
Don’t forget to open a new session or to source your .zshrc after miniforge install and before going through this step.
Create an empty Conda environment, then activate it and install python 3.8 and all the needed packages.
conda create -n boostconda activate boostconda install python=3.8.8conda install numpy scipy scikit-learn
Note that numpy and scipy are dependencies of XGBoost. Installing them from Conda (conda-forge) before to install XGBoost from pip is very important as it makes sure of having arm64 versions of these packages in the environment.
Trying to directly install XGBoost from pip fails when it loads and compile pip version of scipy.
LightGBM already has a pre-compiled arm64 version under conda-forge.
conda install lightgbm
As XGBoost native arm64 version is not yet available in conda-forge, it must be installed from pip. All dependencies are already installed in native version after Step 5.
pip install xgboost
This compile and install XGBoost under the environment.
The following script trains and test a binary classifier with LightGBM and XGBoost.
import numpy as npfrom sklearn.datasets import make_classificationfrom sklearn.model_selection import train_test_splitfrom sklearn.metrics import accuracy_scoreimport timefrom lightgbm import LGBMClassifierfrom xgboost import XGBClassifierprint('make classification ...')X,y = make_classification(n_samples=1000000,                         n_features=50,                         n_informative=30,                         n_redundant=5,                         n_repeated=0,                         n_classes=2,                         n_clusters_per_class=2,                         class_sep=1,                         flip_y=0.01,                         weights=[0.5,0.5],                         random_state=17)X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.3,random_state=1000)print(f'X_train shape: {X_train.shape}')print(f'Train LGBM classifier ...')clf = LGBMClassifier(n_estimators=100,                     num_leaves=64,                     max_depth=5,                     learning_rate=0.1,                     random_state=1000,                     n_jobs=-1)start = time.time()clf.fit(X_train,y_train)elapsed = time.time() - startprint(f'LGBM Training ran in {elapsed:.5f} seconds')y_pred = clf.predict(X_test)print(f'Test Accuracy: {accuracy_score(y_test,y_pred):.2f}')print(f'Train XGB classifier ...')clf = XGBClassifier(n_estimators=100,                     max_depth=5,                     max_leaves=64,                     eta=0.1,                     reg_lambda=0,                     tree_method='hist',                     eval_metric='logloss',                     use_label_encoder=False,                     random_state=1000,                     n_jobs=-1)start = time.time()clf.fit(X_train,y_train)elapsed = time.time() - startprint(f'XGB Training ran in {elapsed:.5f} seconds')y_pred = clf.predict(X_test)print(f'Test Accuracy: {accuracy_score(y_test,y_pred):.2f}')
This very simple test gives the following training time on a MacBook Air M1 compared to a Core i5 iMac and a Xeon Platinum instance.
MacBook Air M1 2020 8GB / 512 GB
LightGBM Training : 3.44 secondsXGBoost Training : 4.59 seconds
2017 iMac 27" Core i5 @ 3.8GHz / 40GB RAM
LightGBM Training : 5.03 secondsXGBoost Training : 6.90 seconds
8 cores Intel(R) Xeon(R) Platinum @ 2.5 GHz instance
LightGBM Training : 5.27 secondsXGBoost Training : 6.26 seconds
XGBoost and LightGBM work and have excellent performances on a Mac M1.
Please note that as the development teams of these packages did not yet release an official Apple Silicon version, there is no optimizations, and the GPU is not used.
Thank you for reading.