Tweet Share Share

Last Updated on August 21, 2019

XGBoost is a library for developing very fast and accurate gradient boosting models.

It is a library at the center of many winning solutions in Kaggle data science competitions.

In this tutorial, you will discover how to install the XGBoost library for Python on macOS.

Kick-start your project with my new book XGBoost With Python, including step-by-step tutorials and the Python source code files for all examples.

Let’s get started.

Tutorial Overview

This tutorial is divided into 3 parts; they are:

Install MacPorts Build XGBoost Install XGBoost

Note: I have used this procedure for years on a range of different macOS versions and it has not changed. This tutorial was written and tested on macOS High Sierra (10.13.1).

1. Install MacPorts

You need GCC and a Python environment installed in order to build and install XGBoost for Python.

I recommend GCC 7 and Python 3.6 and I recommend installing these prerequisites using MacPorts.

1. For help installing MacPorts and a Python environment step-by-step, see this tutorial:

>> How to Install a Python 3 Environment on Mac OS X for Machine Learning and Deep Learning

2. After MacPorts and a working Python environment are installed, you can install and select GCC 7 as follows:

sudo port install gcc7 sudo port select --set gcc mp-gcc7 1 2 sudo port install gcc7 sudo port select --set gcc mp-gcc7

3. Confirm your GCC installation was successful as follows:

gcc -v 1 gcc -v

You should see the version of GCC printed; for example:

.. gcc version 7.2.0 (MacPorts gcc7 7.2.0_0) 1 2 .. gcc version 7.2.0 (MacPorts gcc7 7.2.0_0)

What version did you see?

Let me know in the comments below.

2. Build XGBoost

The next step is to download and compile XGBoost for your system.

1. First, check out the code repository from GitHub:

git clone --recursive https://github.com/dmlc/xgboost 1 git clone --recursive https://github.com/dmlc/xgboost

2. Change into the xgboost directory.

cd xgboost/ 1 cd xgboost/

3. Copy the configuration we intend to use to compile XGBoost into position.

cp make/config.mk ./config.mk 1 cp make/config.mk ./config.mk

4. Compile XGBoost; this requires that you specify the number of cores on your system (e.g. 8, change as needed).

make -j8 1 make -j8

The build process may take a minute and should not produce any error messages, although you may see some warnings that you can safely ignore.

For example, the last snippet of the compilation might look as follows:

... a - build/learner.o a - build/logging.o a - build/c_api/c_api.o a - build/c_api/c_api_error.o a - build/common/common.o a - build/common/hist_util.o a - build/data/data.o a - build/data/simple_csr_source.o a - build/data/simple_dmatrix.o a - build/data/sparse_page_dmatrix.o a - build/data/sparse_page_raw_format.o a - build/data/sparse_page_source.o a - build/data/sparse_page_writer.o a - build/gbm/gblinear.o a - build/gbm/gbm.o a - build/gbm/gbtree.o a - build/metric/elementwise_metric.o a - build/metric/metric.o a - build/metric/multiclass_metric.o a - build/metric/rank_metric.o a - build/objective/multiclass_obj.o a - build/objective/objective.o a - build/objective/rank_obj.o a - build/objective/regression_obj.o a - build/predictor/cpu_predictor.o a - build/predictor/predictor.o a - build/tree/tree_model.o a - build/tree/tree_updater.o a - build/tree/updater_colmaker.o a - build/tree/updater_fast_hist.o a - build/tree/updater_histmaker.o a - build/tree/updater_prune.o a - build/tree/updater_refresh.o a - build/tree/updater_skmaker.o a - build/tree/updater_sync.o c++ -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -o xgboost build/cli_main.o build/learner.o build/logging.o build/c_api/c_api.o build/c_api/c_api_error.o build/common/common.o build/common/hist_util.o build/data/data.o build/data/simple_csr_source.o build/data/simple_dmatrix.o build/data/sparse_page_dmatrix.o build/data/sparse_page_raw_format.o build/data/sparse_page_source.o build/data/sparse_page_writer.o build/gbm/gblinear.o build/gbm/gbm.o build/gbm/gbtree.o build/metric/elementwise_metric.o build/metric/metric.o build/metric/multiclass_metric.o build/metric/rank_metric.o build/objective/multiclass_obj.o build/objective/objective.o build/objective/rank_obj.o build/objective/regression_obj.o build/predictor/cpu_predictor.o build/predictor/predictor.o build/tree/tree_model.o build/tree/tree_updater.o build/tree/updater_colmaker.o build/tree/updater_fast_hist.o build/tree/updater_histmaker.o build/tree/updater_prune.o build/tree/updater_refresh.o build/tree/updater_skmaker.o build/tree/updater_sync.o dmlc-core/libdmlc.a rabit/lib/librabit.a -pthread -lm -fopenmp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ... a - build/learner.o a - build/logging.o a - build/c_api/c_api.o a - build/c_api/c_api_error.o a - build/common/common.o a - build/common/hist_util.o a - build/data/data.o a - build/data/simple_csr_source.o a - build/data/simple_dmatrix.o a - build/data/sparse_page_dmatrix.o a - build/data/sparse_page_raw_format.o a - build/data/sparse_page_source.o a - build/data/sparse_page_writer.o a - build/gbm/gblinear.o a - build/gbm/gbm.o a - build/gbm/gbtree.o a - build/metric/elementwise_metric.o a - build/metric/metric.o a - build/metric/multiclass_metric.o a - build/metric/rank_metric.o a - build/objective/multiclass_obj.o a - build/objective/objective.o a - build/objective/rank_obj.o a - build/objective/regression_obj.o a - build/predictor/cpu_predictor.o a - build/predictor/predictor.o a - build/tree/tree_model.o a - build/tree/tree_updater.o a - build/tree/updater_colmaker.o a - build/tree/updater_fast_hist.o a - build/tree/updater_histmaker.o a - build/tree/updater_prune.o a - build/tree/updater_refresh.o a - build/tree/updater_skmaker.o a - build/tree/updater_sync.o c++ -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -o xgboost build/cli_main.o build/learner.o build/logging.o build/c_api/c_api.o build/c_api/c_api_error.o build/common/common.o build/common/hist_util.o build/data/data.o build/data/simple_csr_source.o build/data/simple_dmatrix.o build/data/sparse_page_dmatrix.o build/data/sparse_page_raw_format.o build/data/sparse_page_source.o build/data/sparse_page_writer.o build/gbm/gblinear.o build/gbm/gbm.o build/gbm/gbtree.o build/metric/elementwise_metric.o build/metric/metric.o build/metric/multiclass_metric.o build/metric/rank_metric.o build/objective/multiclass_obj.o build/objective/objective.o build/objective/rank_obj.o build/objective/regression_obj.o build/predictor/cpu_predictor.o build/predictor/predictor.o build/tree/tree_model.o build/tree/tree_updater.o build/tree/updater_colmaker.o build/tree/updater_fast_hist.o build/tree/updater_histmaker.o build/tree/updater_prune.o build/tree/updater_refresh.o build/tree/updater_skmaker.o build/tree/updater_sync.o dmlc-core/libdmlc.a rabit/lib/librabit.a -pthread -lm -fopenmp

Did this step work for you?

Let me know in the comments below.

3. Install XGBoost

You are now ready to install XGBoost on your system.

1. Change directory into the Python package of the xgboost project.

cd python-package 1 cd python-package

2. Install the Python XGBoost package.

sudo python setup.py install 1 sudo python setup.py install

The installation is very fast.

For example, at the end of the installation, you may see messages like the following:

... Installed /opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/xgboost-0.6-py3.6.egg Processing dependencies for xgboost==0.6 Searching for scipy==1.0.0 Best match: scipy 1.0.0 Adding scipy 1.0.0 to easy-install.pth file Using /opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages Searching for numpy==1.13.3 Best match: numpy 1.13.3 Adding numpy 1.13.3 to easy-install.pth file Using /opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages Finished processing dependencies for xgboost==0.6 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ... Installed /opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/xgboost-0.6-py3.6.egg Processing dependencies for xgboost==0.6 Searching for scipy==1.0.0 Best match: scipy 1.0.0 Adding scipy 1.0.0 to easy-install.pth file Using /opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages Searching for numpy==1.13.3 Best match: numpy 1.13.3 Adding numpy 1.13.3 to easy-install.pth file Using /opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages Finished processing dependencies for xgboost==0.6

3. Confirm that the installation was successful by printing the xgboost version, which requires the library to be loaded.

Save the following code to a file called version.py.

import xgboost print("xgboost", xgboost.__version__) 1 2 import xgboost print ( "xgboost" , xgboost . __version__ )

Run the script from the command line:

python version.py 1 python version.py

You should see the XGBoost version printed to screen:

xgboost 0.6 1 xgboost 0.6

How did you do?

Post your results in the comments below.

Further Reading

This section provides more resources on the topic if you are looking to go deeper.

Summary

In this tutorial, you discovered how to install XGBoost for Python on macOS step-by-step.

Do you have any questions?

Ask your questions in the comments below and I will do my best to answer.

Discover The Algorithm Winning Competitions! Develop Your Own XGBoost Models in Minutes ...with just a few lines of Python Discover how in my new Ebook:

XGBoost With Python It covers self-study tutorials like:

Algorithm Fundamentals, Scaling, Hyperparameters, and much more... Bring The Power of XGBoost To Your Own Projects Skip the Academics. Just Results. See What's Inside