22

I am a newbie and learning python. Can someone help me- how to install xgboost in python. Im using Mac 10.11. I read online and did the below mentioned step, but not able to decode what to do next:

pip install xgboost - 
smci
  • 32,567
  • 20
  • 113
  • 146
aman sanduja
  • 221
  • 1
  • 2
  • 3
  • 3
    Can you explain what your exact problem is? For example, if you get any errors, can you document them here? – Timotheus.Kampik Sep 04 '16 at 10:04
  • i ran *pip install xgboost* it gave me an error _Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/hj/kdbpyx0s6y3flcbtj_j71gmc0000gn/T/pip-build-br5u893_/xgboost/_ Could you suggest me in steps how to install xgboost in python from scratch? Thanks (: – aman sanduja Sep 04 '16 at 19:06
  • Have you tried my answer? Could validate it? – Adrien Renaud Sep 08 '16 at 12:37

10 Answers10

39

It's a little more complicated if you want to use multi-threading. For the record, I am using a Mac with OS X 10.10 (Yosemite). It took me a while to work through the various issues, but it is now running nicely in my Anaconda (Py36) environment.

For multi-threading you need to do the following first (install homebrew if you have not done so):

brew install gcc --without-multilib

You might get some warnings to unlink directories or delete them if you have other versions installed; follow the warnings/instructions.

Next get the xgboost files from Github. I downloaded it to Anaconda/pkgs directory.

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

The next series of steps differ from the documentation on the xgboost site, and I cobbled it together from lots of sources and also experimenting. The problem is that some key lines in the make files are commented out and also not fully specified.

cd xgboost; cp make/config.mk ./config.mk

Now, use your favorite editor (I used vi), and go into the file that you copied from /make to /xgboost

vi config.mk

Uncomment the lines near the top of the file:

export CC = gcc

export CXX = g++

Change them to the following:

export CC = gcc-6

export CXX = g++-6

It is possible that simply uncommenting the lines solves the problem. It did not for me; I needed to add the -6 to both lines. Save the file.

Also, make changes to the file xgboost/Makefile; change lines:

export CC = $(if $(shell which clang), clang, gcc)
...
...
export CXX = $(if $(shell which clang++), clang++, g++)

to the following:

export CC = $(if $(shell which clang), clang, gcc-6)
...
...
export CXX = $(if $(shell which clang++), clang++, g++-6)

Again, I used vi for this editing.

Save the file and now you need to run a cleaning step since you changed the Makefile.

make clean_all && make -j4

This should configure it cleanly and build the library. You still need to install it.

cd python-package; python setup.py install

Now restart Python/Anaconda and you should be able to import the library.

heisen
  • 1,067
  • 3
  • 9
  • 25
Bryan Butler
  • 1,750
  • 1
  • 19
  • 19
  • Thanks so much for this, I have had a nightmare with it all day until finding this thorough explanation of yours. Thanks again! :) – George May 04 '17 at 16:05
  • Hi, https://github.com/dmlc/xgboost/issues/1501 , It`s another solution. – xu2mao May 18 '17 at 03:59
  • 7
    Running OSX Sierra here, it looks like brew is installing gcc 7 now. I had to change gcc-7 and g++-7. – BenDundee Jun 06 '17 at 15:37
  • Did these steps work with 7 BenDundee? A colleague of mine tried it with 7 and it seemed to go well. – Bryan Butler Jun 07 '17 at 16:20
  • Thank you so much ! I was struggling with the install until I found your answer ! – Clement T. Jan 15 '18 at 09:21
  • Thanks to this answer I could finally install it in High Sierra and with gcc-8 – heisen May 07 '18 at 20:58
  • Just to confirm this _does_ work on High Sierra, substitute 7 with 8. Thanks for the writeup! – Josh Voigts Oct 10 '18 at 13:38
  • Works for me, just check gcc version (for me gcc 9) and I'm able to parallelize xgb. – deltascience Jul 05 '19 at 10:08
  • worked for gcc@8 too. just change the 6 to 8. also i need to export path `export PATH=/usr/bin:$PATH` – grandia Jul 18 '19 at 04:45
  • I wrote this a while back, so the gcc versions have likely changed as indicated above, just make sure you have the right version installed. In newer MacOS/Xcode, it installs a "gcc" that is instead a link to Clang. You can check it out here: https://stackoverflow.com/questions/20410587/how-to-find-gcc-version-on-mac – Bryan Butler Nov 23 '20 at 18:24
7

If you have Conda installed, this is what worked for me:

Simply type in the terminal:

conda install -c conda-forge xgboost
merv
  • 67,214
  • 13
  • 180
  • 245
rossinnes
  • 71
  • 1
  • 1
6

For a newbie learning python and Machine Learning on Mac, I would strongly recommand to install Anaconda first (install doc).

Anaconda is a freemium open source distribution of the Python and R programming languages for large-scale data processing, predictive analytics, and scientific computing, that aims to simplify package management and deployment.

If you installed Anaconda for Python 2.7, then you should have no troubles installing XGBoost with:

conda install -c aterrel xgboost=0.4.0

If you already have Anaconda installed and that your pip XGBoost installation failed, you should try:

conda remove xgboost
conda install -c aterrel xgboost=0.4.0
Adrien Renaud
  • 2,439
  • 18
  • 22
  • 2
    amans-air-2:~ amansanduja$ conda **install -c aterrel xgboost=0.4.0** Use "conda info " to see the dependencies for each package. _I ran the command in bold on terminal and got the following output_: Using Anaconda Cloud api site https://api.anaconda.org Fetching package metadata ......... Solving package specifications: .... The following specifications were found to be in conflict: - python 3.5* - xgboost 0.4.0* Could you help what to DO? thanks in advance – aman sanduja Sep 04 '16 at 18:48
  • Python mainly comes in two versions: 2.7 and 3.5. You should install Anaconda for python 2.7. Everything should go fine then. Sorry. – Adrien Renaud Sep 04 '16 at 19:35
6

FOR PYTHON 2.7

$ conda install -c aterrel xgboost=0.4.0

OR

$ conda install -c biconda xgboost=0.6a2

FOR PYTHON 3.6

$ brew install gcc@5
$ pip install xgboost
PritamJ
  • 337
  • 4
  • 10
5

For Python-3.x, do the following in Mac

Make sure gcc-6 (and g++-6) is installed, if not do so with

brew install gcc

Then, do the following

git clone --recursive https://github.com/dmlc/xgboost
cd xgboost/
make -j4
cd python-package
python3 setup.py install

If you are using Anaconda and haven't yet configured your path to use the binaries in ~/anaconda/bin, then run the last line as

/path/to/anaconda/bin/python3 setup.py install

Ébe Isaac
  • 11,563
  • 17
  • 64
  • 97
4

I followed Bryan Butler's answer and it worked, I just needed to make some changes:

  • gcc-7/g++-7 instead of gcc-6/g++-6.
  • While running make clean_all && make -j4 I had an error with as. So, I just had to run export PATH=/usr/bin:$PATH and it worked!
A. Attia
  • 1,630
  • 3
  • 20
  • 29
2

Im running Mac OS Mojave 10.14.5 and following the "advanced method" instructions for Mac OS at https://xgboost.readthedocs.io/en/latest/build.html# worked for me. In short:

brew install cmake 
brew install gcc@8
git clone --recursive https://github.com/dmlc/xgboost
mkdir xgboost/my_build
cd xgboost/my_build
CC=gcc-8 CXX=g++-8 cmake ..
make -j4
cd ../python_package
python3 setup.py install
1

You can pip install catboost. It is a recently open-sourced gradient boosting library, it has similar interfaces and is more accurate, than XGBoost, faster and has categorical features support out of the box. Here is the site of the library: https://catboost.yandex

1

All the other ways described here failed in my case. I managed to install by following the official installation described here: http://xgboost.readthedocs.io/en/latest/build.html#building-on-macos

My system is MacOS Serria so I followed the instruction of "Building on macOS".

However, instead of "replacing these two lines into(5 or 6 or 7; depending on your gcc-version" w.r.t. the config.mk file, I did:

export CC = gcc-5
export CXX = g++-5

Even though gcc-version showed Apple LLVM version 9.0.0.

After that by following the official instruction of "Python Package Installation" I was able to run the package in Python.

0

March 2021 - No problem installing XGBoost 1.3.3 using pip

> pip3 install xgboost

>>> import xgboost as xgb
>>> xgb.__version__
'1.3.3'
Claude COULOMBE
  • 3,434
  • 2
  • 36
  • 39