How to Install XGBoost for Python on macOS

Last Updated on

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.

Discover how to configure, fit, tune and evaluation gradient boosting models with XGBoost in my new book, with 15 step-by-step tutorial lessons, and full python code.

Let’s get started.

How to Install XGBoost for Python on macOS

How to Install XGBoost for Python on macOS
Photo by auntjojo, some rights reserved.

Tutorial Overview

This tutorial is divided into 3 parts; they are:

  1. Install MacPorts
  2. Build XGBoost
  3. 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:

  • 3. Confirm your GCC installation was successful as follows:

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

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:

  • 2. Change into the xgboost directory.

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

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

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:

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.

  • 2. Install the Python XGBoost package.

The installation is very fast.

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

  • 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

Run the script from the command line:

You should see the XGBoost version printed to screen:

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.


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!

XGBoost With Python

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

32 Responses to How to Install XGBoost for Python on macOS

  1. Abhishek Dalakoti January 17, 2018 at 8:36 pm #

    can you put for windows?

  2. Juliano Petronetto January 19, 2018 at 5:15 am #

    Or can simply use it in Docker:

  3. Gabriel Kreplak January 19, 2018 at 6:45 am #


    $ gcc -v

    I did not get gcc version but:

    Configured with: –prefix=/Library/Developer/CommandLineTools/usr –with-gxx-include-dir=/usr/include/c++/4.2.1
    Apple LLVM version 9.0.0 (clang-900.0.39.2)
    Target: x86_64-apple-darwin17.3.0
    Thread model: posix
    InstalledDir: /Library/Developer/CommandLineTools/usr/bin

    I didn’t try further (still)

    • Jason Brownlee January 20, 2018 at 8:11 am #

      Looks like you are still using the apple compiler.

      You may need to tell macports that you want to use the newly installed compiler.

    • Steven Limpert June 16, 2018 at 8:00 pm #

      I had the same issue. However, I resolved it by using the command “hash -r” to get Terminal to recognize the change. According to this,, opening a new Terminal window would have worked as well. After using “hash -r”, “gcc -v” gave the same result shown in this tutorial.

  4. Sergey January 20, 2018 at 11:09 pm #

    Hi Jason

    All worked great till

    make -j8

    I received fatal error and then – some errors

    /opt/local/include/gcc7/c++/cwchar:44:10: fatal error: wchar.h: No such file or directory

    Do you have any ideas?

    • Jason Brownlee January 21, 2018 at 9:11 am #

      Oh dear. No good ideas sorry.

      Perhaps try posting to stack overflow or the xgboost issue tracker:

      Or try checking out the v0.6 version in case something changed in the recent 0.7 release?

    • MC February 17, 2018 at 6:30 am #

      Same error

  5. cgironda January 23, 2018 at 12:13 am #

    Hi Jason,

    I just used,

    conda install -c conda-forge xgboost

    So I did not follow steps 1, 2 and 3. However, it depends on tastes.


  6. Rajat Sharma January 30, 2018 at 11:23 pm #

    Why do you think that python is a better option for machine learning than R or Matlab?

  7. Arvind Ravichandran February 9, 2018 at 3:07 am #

    Thank you very much! Worked brilliantly.

  8. Matt Smith February 22, 2018 at 4:23 am #

    Brilliant! I was previously trying to use homebrew to install gcc and then editing the file, as basically all other tutorials tell you to, but was constantly getting errors. This is the first method that worked for me (on mac OS Sierra 10.12.6); thank you!

  9. Gary March 19, 2018 at 7:29 am #

    cgironda you are a star! You could spend a day trying to answer all the questions out there related to unsupported option '-fopenmp' and you wouldn’t even come close to getting to all of them. You rescued me after about 4 hours of fussing with this. Thanks!

  10. Alvic May 15, 2018 at 12:00 pm #

    Hi Jason,

    Have followed your instructions up to step 2 build XG boost and this is the message I saw on my terminal.

    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
    ld: can’t open output file for writing: xgboost, errno=21 for architecture x86_64
    collect2: error: ld returned 1 exit status
    make: *** [xgboost] Error 1
    make: *** Waiting for unfinished jobs….
    Alvics-MacBook-Pro-2:xgboost alviceugenejosol$ a – b

    • Jason Brownlee May 15, 2018 at 2:45 pm #

      Ouch. I have not seen this.

      Perhaps try a make clean, then try building again?
      Perhaps try posting the error to the xgboost user group?

  11. Akeel September 20, 2018 at 2:47 am #

    Hi Jason,
    Worked like a charm. Thanks for your detailed instructions.

  12. Paul M Washburn November 4, 2018 at 7:36 am #

    Step 2 did not work …

    Pauls-MacBook-Pro:xgboost pmw$ make -j8
    Makefile:31: MAKE [/Library/Developer/CommandLineTools/usr/bin/make] – checked OK
    c++ -DDMLC_LOG_CUSTOMIZE=1 -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -MM -MT build/c_api/c_api.o src/c_api/ >build/c_api/c_api.d
    c++ -DDMLC_LOG_CUSTOMIZE=1 -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -MM -MT build/logging.o src/ >build/logging.d
    c++ -DDMLC_LOG_CUSTOMIZE=1 -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -MM -MT build/learner.o src/ >build/learner.d
    c++ -DDMLC_LOG_CUSTOMIZE=1 -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -MM -MT build/c_api/c_api_error.o src/c_api/ >build/c_api/c_api_error.d
    c++ -DDMLC_LOG_CUSTOMIZE=1 -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -MM -MT build/common/common.o src/common/ >build/common/common.d
    c++ -DDMLC_LOG_CUSTOMIZE=1 -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -MM -MT build/common/hist_util.o src/common/ >build/common/hist_util.d
    c++ -DDMLC_LOG_CUSTOMIZE=1 -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -MM -MT build/common/host_device_vector.o src/common/ >build/common/host_device_vector.d
    c++ -DDMLC_LOG_CUSTOMIZE=1 -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -MM -MT build/data/data.o src/data/ >build/data/data.d
    clang: clangclang: : error: errorerrorunsupported option ‘-fopenmp’: : unsupported option ‘-fopenmp’unsupported option ‘-fopenmp’

    clang: clang: error: unsupported option ‘-fopenmp’error:
    unsupported option ‘-fopenmp’
    clang: error: unsupported option ‘-fopenmp’
    clang: error: unsupported option ‘-fopenmp’
    clang: error: unsupported option ‘-fopenmp’
    make: *** [build/common/common.o] Error 1
    make: *** Waiting for unfinished jobs….
    make: *** [build/c_api/c_api_error.o] Error 1
    make: *** [build/learner.o] Error 1
    make: *** [build/logging.o] Error 1
    make: *** [build/c_api/c_api.o] Error 1
    make: *** [build/common/host_device_vector.o] Error 1
    make: *** [build/common/hist_util.o] Error 1
    make: *** [build/data/data.o] Error 1

    • Jason Brownlee November 5, 2018 at 6:06 am #

      Prhaps try installing with pip on your workstation instead?


  13. Chris February 20, 2019 at 1:35 am #

    After installing MacPorts, I tried port install gcc but got an error that no port named gcc7 existed. Tried several variants. Finally had to do a port install self update, and then the rest worked like a charm. So maybe add the self update as step one in the Installing MacPorts section. Everything else worked great. Thanks for this and all your other articles.

Leave a Reply