XGBoost - Installation



XGBoost is an improved distributed gradient boosting library that is fast, versatile, and portable. XGBoost can be installed in a variety of ways, depending on the operating system and development environment. The following are the different methods for installing XGBoost.

Using pip (for Python)

The simplest and most common approach to install XGBoost is via pip. So all you have to do is type the following command into your terminal to download and install the library and its dependencies.

pip install xgboost

Output

Here is the process after running the above command on terminal or command prompt −

Collecting xgboost
  Downloading xgboost-1.7.3-py3-none-manylinux2014_x86_64.whl (199.9 MB)
     199.9/199.9 MB 8.4 MB/s eta 0:00:00
Requirement already satisfied: numpy in /usr/local/lib/python3.8/site-packages (from xgboost) (1.21.2)
Installing collected packages: xgboost
Successfully installed xgboost-1.7.3

Using Conda (for Anaconda users)

If you are working with Anaconda or Miniconda then you can install XGBoost with the help of Conda. When you run the below command it will download the required package and dependencies, then install it in your system.

conda install -c conda-forge xgboost

Outut

The terminal output would typically look like this −

(base) $ conda install -c conda-forge xgboost
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /path/to/conda/environment

  added / updated specs:
    - xgboost

The following packages will be downloaded:

    package                       |            build
    ------------------------------|-----------------
    xgboost-1.7.3                 |   py38h9b699db_0   70.0 MB
    ------------------------------------------------------------
                                           Total:   70.0 MB

Proceed ([y]/n)? y

Downloading and Extracting Packages
xgboost-1.7.3        |  70.0 MB | ########## | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

Building from Source

If you want the most latest version of XGBoost or want to make changes to your build, you can do this via source. You will have to install git, cmake, and other build tools on your computer.

  • First you have to clone the XGBoost repository −

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

    Below is the terminal output −

    $ git clone --recursive https://github.com/dmlc/xgboost
    Cloning into 'xgboost'...
    remote: Enumerating objects: 17723, done.
    remote: Counting objects: 100% (17723/17723), done.
    remote: Compressing objects: 100% (4181/4181), done.
    remote: Total 17723 (delta 12943), reused 16361 (delta 11547), pack-reused 0
    Receiving objects: 100% (17723/17723), 28.36 MiB | 4.61 MiB/s, done.
    Resolving deltas: 100% (12943/12943), done.
    
  • After running the above command you have to navigate to the cloned directory −

    cd xgboost
    
  • Now build the project like below −

    mkdir build
    cd build
    cmake ..
    make -j4
    

    The terminal output is as follows −

    -- The CXX compiler identification is GNU 9.4.0
    -- The C compiler identification is GNU 9.4.0
    ...
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /path/to/xgboost/build
    [  1%] Building CXX object CMakeFiles/xgboost.dir/src/learner.cc.o
    [  2%] Building CXX object CMakeFiles/xgboost.dir/src/common/host_device_vector.cc.o
    ...
    [100%] Linking CXX executable xgboost
    [100%] Built target xgboost
    
  • And lastly Install the python package −

    cd ../python-package
    python setup.py install
    

    Here is the output for the above command after installing the package −

    running install
    running bdist_egg
    running egg_info
    creating xgboost.egg-info
    writing xgboost.egg-info/PKG-INFO
    writing dependency_links to xgboost.egg-info/dependency_links.txt
    writing requirements to xgboost.egg-info/requires.txt
    writing top-level names to xgboost.egg-info/top_level.txt
    writing manifest file 'xgboost.egg-info/SOURCES.txt'
    reading manifest file 'xgboost.egg-info/SOURCES.txt'
    writing manifest file 'xgboost.egg-info/SOURCES.txt'
    installing library code to build/bdist.linux-x86_64/egg
    running install_lib
    creating /usr/local/lib/python3.8/dist-packages/xgboost-1.7.5-py3.8-linux-x86_64.egg
    ...
    byte-compiling /usr/local/lib/python3.8/dist-packages/xgboost/__init__.py to __init__.cpython-38.pyc
    running install_scripts
    creating /usr/local/bin
    ...
    running install_egg_info
    Writing /usr/local/lib/python3.8/dist-packages/xgboost-1.7.5-py3.8.egg-info
    

Installing for R

If you want to use the R programming language then XGBoost can be installed from the CRAN repository. Use the below command to install it for R programming −

install.packages('catboost', repos = 'https://cloud.r-project.org/', dependencies=TRUE)

So the command will install CatBoost for R programming. Please refer the below output for installation process on terminal −

Installing package into '/home/user/R/x86_64-pc-linux-gnu-library/4.1'
(as 'lib' is unspecified)
trying URL 'https://cloud.r-project.org/src/contrib/xgboost_1.7.5.tar.gz'
Content type 'application/x-gzip' length 2277833 bytes (2.2 MB)
==================================================
downloaded 2.2 MB

* installing *source* package 'xgboost' ...
** package 'xgboost' successfully unpacked and MD5 sums checked
** using staged installation
** libs

*** Installing xgboost
g++ -std=gnu++11 -I"/usr/share/R/include" -DNDEBUG -I../include -I../third_party/dmlc-core/include -I../third_party/rabit/include -I../third_party/xgboost/src -I../third_party/xgboost/src/../include -fpic -O2 -fPIC -c xgboost_R.cc -o xgboost_R.o
...
...
** R
** demo
** inst
** tests
** preparing package for lazy loading
** help
*** installing help indices
*** copying figures
*** copying HTML documentation
** building package indices
** testing if installed package can be loaded
* DONE (xgboost)
Advertisements