HOWTO · Python

How to Install XGBoost in Python

Install XGBoost in the Python environment you use, choose pip or conda, and learn when a source build is appropriate.

On this page

XGBoost is an open-source library for gradient-boosted decision trees. To use it from Python, install it with pip or conda in the environment where your project runs. Building from source is an advanced option when you need to customize the build or use a setup that a packaged release does not support.

Choose an installation method

Use pip if your project already uses pip and a standard Python environment. Choose conda if you manage the project with conda and want its environment and package tools to manage dependencies together. Avoid mixing package managers in one environment unless you understand how that environment resolves packages.

Select or activate the environment first, then run its package manager. The package commands below have not been executed as part of this article draft. Check the official XGBoost installation guide and release documentation for current package instructions and supported Python and platform combinations before installing.

Install XGBoost with pip

In the terminal for the Python environment you intend to use, run python -m pip install xgboost. Using python -m pip associates pip with that interpreter, which helps avoid installing into a different Python installation. On systems where the command is named python3, use python3 -m pip install xgboost instead.

To update an existing pip installation, run python -m pip install --upgrade xgboost in the same environment. Read any dependency or compatibility messages from pip and confirm the installation with the verification step below.

Install XGBoost with conda

Activate the conda environment for your project, then follow the conda-forge command in the official XGBoost installation guide, currently conda install -c conda-forge py-xgboost. This selects the conda-forge channel and the Python package. Package availability can vary by platform and release, so check that guide before installing or updating; use the package name and channel it lists for your environment.

Build XGBoost from source

Building from source is for advanced users who need to customize XGBoost or whose platform and requirements are not met by an available package. Follow the official XGBoost source-build guide for the prerequisites and commands for your operating system and release. The guide covers the native build process and Python package installation; use its current steps rather than a generic sequence, because build requirements and commands can change. Install the resulting Python package into the same environment used by your project.

Verify the installation and troubleshoot common problems

Run this self-contained check with the Python interpreter selected for your project. It reports the installed XGBoost version, or prints a clear message when this interpreter cannot find the package.

import importlib.util

if importlib.util.find_spec("xgboost") is None:
    print("XGBoost is not installed in this Python environment.")
else:
    import xgboost

    print(f"XGBoost {xgboost.__version__}")
XGBoost is not installed in this Python environment.

If the check reports that XGBoost is not installed after you installed it, compare the interpreter used for installation with the one running your application. For pip, python -m pip show xgboost checks the package associated with that interpreter; for conda, conda list xgboost checks the active environment. Run installation and verification from the same activated environment. If the environment matches, use the full import error and consult the official installation guidance for your operating system, Python version, and XGBoost release before changing dependencies.

Frequently asked questions

Can I install XGBoost in a virtual environment?

Yes. Activate the venv or conda environment first, install XGBoost with that environment’s package manager, and run the verification check with its Python interpreter.

Which Python versions and platforms are supported?

Support depends on the XGBoost release and available package builds. Check the official release and installation documentation for the Python versions and platforms supported by the release you plan to install.

Can I use XGBoost with other machine-learning libraries?

XGBoost provides a Python package and can be used in Python projects alongside other libraries. For a specific integration, follow the relevant library’s current documentation and confirm that the installed versions are compatible.