This page gives instructions on how to build and install XGBoost from the source code on
various systems. If the instructions do not work for you, please feel free to ask
questions at GitHub.
Obtaining the Source Code
To obtain the development repository of XGBoost, one needs to use git. XGBoost uses
Git submodules to manage dependencies. So when you clone the repo, remember to specify
--recursive option:
This section describes the procedure to build the shared library and CLI interface
independently. For building language specific package, see corresponding sections in this
document.
On Linux and other UNIX-like systems, the target library is libxgboost.so
On MacOS, the target library is libxgboost.dylib
On Windows the target library is xgboost.dll
This shared library is used by different language bindings (with some additions depending
on the binding you choose). The minimal building requirement is
A recent C++ compiler supporting C++17. We use gcc, clang, and MSVC for daily
testing. 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 top
level of source tree. We use ninja for build in this document, specified via the CMake
flag -GNinja. If you prefer other build tools like make or VisualStudio172022, please change the corresponding CMake flags. Consult the CMake generator document when
needed.
Running CMake and build
After obtaining the source code, one builds XGBoost by running CMake:
The same command applies for both Unix-like systems and Windows. After running the
build, one should see a shared object under the xgboost/lib directory.
Building on MacOS
On MacOS, one needs to obtain libomp from Homebrew first:
brew install libomp
Building with GPU support
XGBoost can be built with GPU support for both Linux and Windows using CMake. See
Building R package with GPU support for special instructions for R.
An up-to-date version of the CUDA toolkit is required.
Some distros package a compatible gcc version with CUDA. If you run into compiler
errors with nvcc, try specifying the correct compiler with
-DCMAKE_CXX_COMPILER=/path/to/correct/g++-DCMAKE_C_COMPILER=/path/to/correct/gcc. On
Arch Linux, for example, both binaries can be found under /opt/cuda/bin/. In addition,
the parameter can be useful.
Federated Learning
The federated learning plugin requires grpc and protobuf. To install grpc, refer
to the installation guide from the gRPC website. Alternatively, one can use the
libgrpc and the protobuf package from conda forge if conda is available. After
obtaining the required dependencies, enable the flag: -DPLUGIN_FEDERATED=ON when
running CMake. Please note that only Linux is supported for the federated plugin.
There are several ways to build and install the package from source:
Build C++ core with CMake first
You can first build C++ library using CMake as described in Building the Shared Library.
After compilation, a shared library will appear in lib/ directory.
On Linux distributions, the shared library is lib/libxgboost.so.
The install script pipinstall. will reuse the shared library instead of compiling
it from scratch, making it quite fast to run.
$ cdpython-package/
$ pipinstall.# Will re-use lib/libxgboost.so
Install the Python package directly
If the shared object is not present, the Python project setup script will try to run the
CMake build command automatically. Navigate to the python-package/ directory and
install 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 enable
additional compilation options, pass corresponding --config-settings:
To further enable rapid development and iteration, we provide an editable
installation. In an editable installation, the installed package is simply a symbolic
link to your working copy of the XGBoost source code. So every changes you make to your
source directory will be immediately visible to the Python interpreter. To install
XGBoost as editable installation, first build the shared library as previously described
in Running CMake and build, then install the Python package with the -e flag:
This option is useful for package managers that wish to separately package
libxgboost.so and the XGBoost Python package. For example, Conda
publishes libxgboost (for the shared library) and py-xgboost
(for the Python package).
To use this option, first make sure that libxgboost.so exists in the system library path:
By default, the package installed by running install.packages is built from source
using the package from CRAN. Here we list some other
options 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 above
sections for requirements of building C++ core).
Due to the use of git-submodules, remotes::install_github() cannot be used to
install the latest version of R package. Thus, one has to run git to check out the code
first, see Obtaining the Source Code on how to initialize the git repository for XGBoost. The
simplest way to install the R package after obtaining the source code is:
cdR-package
RCMDINSTALL.
Use the environment variable MAKEFLAGS=-j$(nproc) if you want to speedup the build. As
an alternative, the package can also be loaded through devtools::load_all() from the
same subfolder R-package in the repository’s root, and by extension, can be installed
through RStudio’s build panel if one adds that folder R-package as an R package
project in the RStudio IDE.
Note in this case that cmake will not take configurations from your regular
Makevars file (if you have such a file under ~/.R/Makevars) - instead, custom
configurations such as compilers to use and flags need to be set through CMake variables
like -DCMAKE_CXX_COMPILER.
Building R package with GPU support
The procedure and requirements are similar as in Building with GPU support, so make sure to read it first.
On Linux, starting from the XGBoost directory type:
When default target is used, an R package shared library would be built in the build area.
The install target, in addition, assembles the package files with this shared library under build/R-package and runs .
Building JVM Packages
Building XGBoost4J using Maven requires Maven 3 or newer, Java 7+ and CMake 3.18+ for
compiling Java code as well as the Java Native Interface (JNI) bindings. In addition, a
Python script is used during configuration, make sure the command python is available
on your system path (some distros use the name python3 instead of python).
Before you install XGBoost4J, you need to define environment variable JAVA_HOME as your JDK directory to ensure that your compiler can find jni.h correctly, since XGBoost4J relies on JNI to implement the interaction between the JVM and native libraries.
After your JAVA_HOME is defined correctly, it is as simple as run mvnpackage under jvm-packages directory to install XGBoost4J. You can also skip the tests by running mvn-DskipTests=truepackage, if you are sure about the correctness of your local setup.
To publish the artifacts to your local maven repository, run
mvn install
Or, if you would like to skip tests, run
mvn -DskipTests install
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 in pom.xml:
If you want to use XGBoost4J-Spark, replace xgboost4j with xgboost4j-spark.
Additional System-dependent Features
OpenMP on MacOS: See Running CMake and build for installing openmp. The flag
-mvn-Duse.openmp=OFF can be used to disable OpenMP support.
GPU support can be enabled by passing an additional flag to maven mvn-Duse.cuda=ONinstall. See Building with GPU support for more info. In addition, -Dplugin.rmm=ON
can enable the optional RMM support.
Building the Documentation
XGBoost uses Sphinx for documentation. To
build it locally, you need a installed XGBoost with all its dependencies along with:
System dependencies
git
graphviz
Python dependencies
Checkout the requirements.txt file under doc/
Under xgboost/doc directory, run make<format> with <format> replaced by the
format you want. For a list of supported formats, run makehelp under the same
directory. This builds a partial document for Python but not other language bindings. To
build the full document, see Documentation and Examples.
Visual Studio
The latest Visual Studio has builtin support for CMake projects. If you prefer using an
IDE over the command line, you can use the openwithvisualstudio option in the
right-click menu under the xgboost source directory. Consult the VS document
for more info.
CMAKE_CUDA_HOST_COMPILER
From the command line on Linux starting from the XGBoost directory, add the USE_CUDA
flag:
To speed up compilation, the compute version specific to your GPU could be passed to cmake
as, e.g., -DCMAKE_CUDA_ARCHITECTURES=75. A quick explanation and numbers for some
architectures can be found in this page.
Faster distributed GPU training with NCCL
By default, distributed GPU training is enabled with the option
USE_NCCL=ON. Distributed GPU training depends on NCCL2, available at this link. Since NCCL2 is only available for Linux machines,
Distributed GPU training is available only for Linux.
Some additional flags are available for NCCL, BUILD_WITH_SHARED_NCCL enables
building XGBoost with NCCL as a shared library, while USE_DLOPEN_NCCL enables
XGBoost to load NCCL at runtime using dlopen.
use_nccl
=
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 for
detailed description.
(
"lib"
,
"libxgboost.so"
)
assert
libpath
.
exists
()
Then pass use_system_libxgboost=True option to pipinstall: