Building From Source

This page gives instructions on how to build and install XGBoost from the source code onvarious systems. If the instructions do not work for you, please feel free to askquestions atGitHub.

Note

Pre-built binary is available: now with GPU support

Consider installing XGBoost from a pre-built binary, to avoid the trouble of building XGBoost from the source. CheckoutInstallation Guide.

Obtaining the Source Code

To obtain the development repository of XGBoost, one needs to usegit. XGBoost usesGit submodules to manage dependencies. So when you clone the repo, remember to specify--recursive option:

gitclone--recursivehttps://github.com/dmlc/xgboost

Building the Shared Library

This section describes the procedure to build the shared library and CLI interfaceindependently. For building language specific package, see corresponding sections in thisdocument.

  • On Linux and other UNIX-like systems, the target library islibxgboost.so

  • On MacOS, the target library islibxgboost.dylib

  • On Windows the target library isxgboost.dll

This shared library is used by different language bindings (with some additions dependingon the binding you choose). The minimal building requirement is

  • A recent C++ compiler supporting C++17. We use gcc, clang, and MSVC for dailytesting. Mingw is only used for the R package and has limited features.

  • CMake 3.18 or higher.

For a list of CMake options like GPU support, see#--Options in CMakeLists.txt on toplevel of source tree. We useninja for build in this document, specified via the CMakeflag-GNinja. If you prefer other build tools likemake orVisualStudio172022, please change the corresponding CMake flags. Consult theCMake generator document whenneeded.

Running CMake and build

After obtaining the source code, one builds XGBoost by running CMake:

cdxgboostcmake-Bbuild-S.-DCMAKE_BUILD_TYPE=RelWithDebInfo-GNinjacdbuild&&ninja

The same command applies for both Unix-like systems and Windows. After running thebuild, one should see a shared object under thexgboost/lib directory.

  • Building on MacOS

    On MacOS, one needs to obtainlibomp fromHomebrew first:

    brewinstalllibomp
  • Visual Studio

    The latest Visual Studio has builtin support for CMake projects. If you prefer using anIDE over the command line, you can use theopenwithvisualstudio option in theright-click menu under thexgboost source directory. Consult the VSdocumentfor more info.

Building with GPU support

XGBoost can be built with GPU support for both Linux and Windows using CMake. SeeBuilding R package with GPU support for special instructions for R.

An up-to-date version of the CUDA toolkit is required.

Note

Checking your compiler version

CUDA is really picky about supported compilers, a table for the compatible compilersfor the latest CUDA version on Linux can be seenhere.

Some distros package a compatiblegcc version with CUDA. If you run into compilererrors withnvcc, try specifying the correct compiler with-DCMAKE_CXX_COMPILER=/path/to/correct/g++-DCMAKE_C_COMPILER=/path/to/correct/gcc. OnArch Linux, for example, both binaries can be found under/opt/cuda/bin/. In addition,theCMAKE_CUDA_HOST_COMPILER parameter can be useful.

From the command line on Linux starting from the XGBoost directory, add theUSE_CUDAflag:

cmake-Bbuild-S.-DUSE_CUDA=ON-GNinjacdbuild&&ninja

To speed up compilation, the compute version specific to your GPU could be passed to cmakeas, e.g.,-DCMAKE_CUDA_ARCHITECTURES=75. A quick explanation and numbers for somearchitectures can be foundin this page.

  • Faster distributed GPU training with NCCL

    By default, distributed GPU training is enabled with the optionUSE_NCCL=ON. Distributed GPU training depends on NCCL2, available atthis link. Since NCCL2 is only available for Linux machines,Distributed GPU training is available only for Linux.

    cmake-Bbuild-S.-DUSE_CUDA=ON-DUSE_NCCL=ON-DNCCL_ROOT=/path/to/nccl2-GNinjacdbuild&&ninja

    Some additional flags are available for NCCL,BUILD_WITH_SHARED_NCCL enablesbuilding XGBoost with NCCL as a shared library, whileUSE_DLOPEN_NCCL enablesXGBoost to load NCCL at runtime usingdlopen.

Federated Learning

The federated learning plugin requiresgrpc andprotobuf. To install grpc, referto theinstallation guide from the gRPC website. Alternatively, one can use thelibgrpc and theprotobuf package from conda forge if conda is available. Afterobtaining the required dependencies, enable the flag:-DPLUGIN_FEDERATED=ON whenrunning CMake. Please note that only Linux is supported for the federated plugin.

cmake-Bbuild-S.-DPLUGIN_FEDERATED=ON-GNinjacdbuild&&ninja

Building Python Package from Source

The Python package is located atpython-package/.

Building Python Package with Default Toolchains

There are several ways to build and install the package from source:

  1. Build C++ core with CMake first

You can first build C++ library using CMake as described inBuilding the Shared Library.After compilation, a shared library will appear inlib/ directory.On Linux distributions, the shared library islib/libxgboost.so.The install scriptpipinstall. will reuse the shared library instead of compilingit from scratch, making it quite fast to run.

$cdpython-package/$pipinstall.# Will re-use lib/libxgboost.so
  1. Install the Python package directly

If the shared object is not present, the Python project setup script will try to run theCMake build command automatically. Navigate to thepython-package/ directory andinstall the Python package by running:

$cdpython-package/$pipinstall-v.# Builds the shared object automatically.

which will compile XGBoost’s native (C++) code using default CMake flags. To enableadditional compilation options, pass corresponding--config-settings:

$pipinstall-v.--config-settingsuse_cuda=True--config-settingsuse_nccl=True

Use Pip 22.1 or later to use--config-settings option.

Here are the available options for--config-settings:

@dataclasses.dataclassclassBuildConfiguration:# pylint: disable=R0902"""Configurations use when building libxgboost"""# Whether to hide C++ symbols in libxgboost.sohide_cxx_symbols:bool=True# Whether to enable OpenMPuse_openmp:bool=True# Whether to enable CUDAuse_cuda:bool=False# Whether to enable NCCLuse_nccl:bool=False# Whether to load nccl dynamicallyuse_dlopen_nccl:bool=False# Whether to enable federated learningplugin_federated:bool=False# Whether to enable rmm supportplugin_rmm:bool=False# Special option: See explanation belowuse_system_libxgboost:bool=False

use_system_libxgboost is a special option. See Item 4 below fordetailed description.

Note

Verbose flag recommended

Aspipinstall. will build C++ code, it will take a while to complete.To ensure that the build is progressing successfully, we suggest thatyou add the verbose flag (-v) when invokingpipinstall.

  1. Editable installation

To further enable rapid development and iteration, we provide aneditableinstallation. In an editable installation, the installed package is simply a symboliclink to your working copy of the XGBoost source code. So every changes you make to yoursource directory will be immediately visible to the Python interpreter. To installXGBoost as editable installation, first build the shared library as previously describedinRunning CMake and build, then install the Python package with the-e flag:

# Build shared library libxgboost.socmake-Bbuild-S.-GNinjacdbuild&&ninja# Install as editable installationcd../python-packagepipinstall-e.
  1. Reuse thelibxgboost.so on system path.

This option is useful for package managers that wish to separately packagelibxgboost.so and the XGBoost Python package. For example, Condapublisheslibxgboost (for the shared library) andpy-xgboost(for the Python package).

To use this option, first make sure thatlibxgboost.so exists in the system library path:

importsysimportpathliblibpath=pathlib.Path(sys.base_prefix).joinpath("lib","libxgboost.so")assertlibpath.exists()

Then passuse_system_libxgboost=True option topipinstall:

cdpython-packagepipinstall.--config-settingsuse_system_libxgboost=True

Note

SeeNotes on packaging XGBoost’s Python package for instructions on packaging and distributingXGBoost as Python distributions.

Building R Package From Source

By default, the package installed by runninginstall.packages is built from sourceusing the package fromCRAN. Here we list some otheroptions for installing development version.

Installing the development version (Linux / Mac OSX)

Make sure you have installed git and a recent C++ compiler supporting C++11 (See abovesections for requirements of building C++ core).

Due to the use of git-submodules,remotes::install_github() cannot be used toinstall the latest version of R package. Thus, one has to run git to check out the codefirst, seeObtaining the Source Code on how to initialize the git repository for XGBoost. Thesimplest way to install the R package after obtaining the source code is:

cdR-packageRCMDINSTALL.

Use the environment variableMAKEFLAGS=-j$(nproc) if you want to speedup the build. Asan alternative, the package can also be loaded throughdevtools::load_all() from thesame subfolderR-package in the repository’s root, and by extension, can be installedthrough RStudio’s build panel if one adds that folderR-package as an R packageproject in the RStudio IDE.

library(devtools)devtools::load_all(path="/path/to/xgboost/R-package")

On Linux, if you want to use the CMake build for greater flexibility around compile flags,the earlier snippet can be replaced by:

cmake-Bbuild-S.-DR_LIB=ON-GNinjacdbuild&&ninjainstall

Warning

MSVC is not supported for the R package as it has difficulty handling R Cheaders. CMake build is not supported either.

Note in this case thatcmake will not take configurations from your regularMakevars file (if you have such a file under~/.R/Makevars) - instead, customconfigurations such as compilers to use and flags need to be set through CMake variableslike-DCMAKE_CXX_COMPILER.

Building R package with GPU support

The procedure and requirements are similar as inBuilding with GPU support, so make sure to read it first.

On Linux, starting from the XGBoost directory type:

cmake-Bbuild-S.-DUSE_CUDA=ON-DR_LIB=ONcmake--buildbuild--targetinstall-j$(nproc)

When default target is used, an R package shared library would be built in thebuild area.Theinstall target, in addition, assembles the package files with this shared library underbuild/R-package and runsRCMDINSTALL.

Building JVM Packages

Building XGBoost4J using Maven requires Maven 3 or newer, Java 7+ and CMake 3.18+ forcompiling Java code as well as the Java Native Interface (JNI) bindings. In addition, aPython script is used during configuration, make sure the commandpython is availableon your system path (some distros use the namepython3 instead ofpython).

Before you install XGBoost4J, you need to define environment variableJAVA_HOME as your JDK directory to ensure that your compiler can findjni.h correctly, since XGBoost4J relies on JNI to implement the interaction between the JVM and native libraries.

After yourJAVA_HOME is defined correctly, it is as simple as runmvnpackage under jvm-packages directory to install XGBoost4J. You can also skip the tests by runningmvn-DskipTests=truepackage, if you are sure about the correctness of your local setup.

To publish the artifacts to your local maven repository, run

mvninstall

Or, if you would like to skip tests, run

mvn-DskipTestsinstall

This command will publish the xgboost binaries, the compiled java classes as well as the java sources to your local repository. Then you can use XGBoost4J in your Java projects by including the following dependency inpom.xml:

<dependency><groupId>ml.dmlc</groupId><artifactId>xgboost4j</artifactId><version>latest_source_version_num</version></dependency>

For sbt, please add the repository and dependency in build.sbt as following:

resolvers+="Local Maven Repository"at"file://"+Path.userHome.absolutePath+"/.m2/repository""ml.dmlc"%"xgboost4j"%"latest_source_version_num"

If you want to use XGBoost4J-Spark, replacexgboost4j withxgboost4j-spark.

Note

XGBoost4J-Spark requires Apache Spark 2.3+

XGBoost4J-Spark now requiresApache Spark 3.4+. Latest versions of XGBoost4J-Spark uses facilities oforg.apache.spark.ml.param.shared extensively to provide for a tight integration with Spark MLLIB framework, and these facilities are not fully available on earlier versions of Spark.

Also, make sure to install Spark directly fromApache website.Upstream XGBoost is not guaranteed to work with third-party distributions of Spark, such as Cloudera Spark. Consult appropriate third parties to obtain their distribution of XGBoost.

Additional System-dependent Features

  • OpenMP on MacOS: SeeRunning CMake and build for installingopenmp. The flag-mvn-Duse.openmp=OFF can be used to disable OpenMP support.

  • GPU support can be enabled by passing an additional flag to mavenmvn-Duse.cuda=ONinstall. SeeBuilding with GPU support for more info. In addition,-Dplugin.rmm=ONcan enable the optional RMM support.

Building the Documentation

XGBoost usesSphinx for documentation. Tobuild it locally, you need a installed XGBoost with all its dependencies along with:

  • System dependencies

    • git

    • graphviz

  • Python dependencies

    Checkout therequirements.txt file underdoc/

Underxgboost/doc directory, runmake<format> with<format> replaced by theformat you want. For a list of supported formats, runmakehelp under the samedirectory. This builds a partial document for Python but not other language bindings. Tobuild the full document, seeDocumentation and Examples.