Cannot import xgboost in Jupyter notebook

Cannot Import XGBoost in Jupyter Notebook

Published: October 16, 2024  |  Categories: Python, Jupyter, Machine Learning

XGBoost is one of the most widely used machine learning libraries — it's behind many winning Kaggle solutions and is a go-to for structured data problems. But it has a frustrating quirk: on some systems, especially macOS, it fails to import with a cryptic error about a missing runtime library. This post explains why it happens and how to fix it on every platform.


The Error

You'll see something like this when running import xgboost:

XGBoostError: XGBoost Library (libxgboost.dylib) could not be loaded.
Likely causes:
  * OpenMP runtime is not installed
    - vcomp140.dll or libgomp-1.dll for Windows
    - libomp.dylib for Mac OSX
    - libgomp.so for Linux and other UNIX-like OSes
  * You are running 32-bit Python on a 64-bit OS

Why This Happens

XGBoost uses OpenMP (Open Multi-Processing) to run computations in parallel across CPU cores. OpenMP is not part of Python or XGBoost itself — it's a separate shared library that XGBoost expects to find on the system at runtime.

The problem is that OpenMP isn't installed by default on:

  • Fresh macOS installs (Apple's Clang compiler doesn't bundle it)
  • New virtual environments (Python envs don't carry system libraries)
  • Minimal Linux containers (stripped-down Docker images often omit it)

When XGBoost starts and tries to load the OpenMP shared library, it can't find it — and fails with the error above.


Fix on macOS

Install the missing OpenMP runtime via Homebrew:

brew install libomp

If Homebrew isn't installed yet, run this first:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

After installing libomp, restart your Jupyter kernel — not just the cell, the whole kernel. Go to Kernel > Restart in the menu.

Why kernel restart matters: Python caches import failures in the current session. Even after installing the missing library, the import will keep failing until you start a fresh Python process. Rerunning the cell isn't enough — you must restart the kernel.

Then try importing again:

import xgboost as xgb
print(xgb.__version__)

Fix on Windows

On Windows the import error is caused by a missing Visual C++ runtime:

  1. Go to the Microsoft Visual C++ Redistributable page
  2. Download the x64 version (vc_redist.x64.exe)
  3. Install it and restart your machine
  4. Try importing XGBoost again

If that doesn't work, try reinstalling via pip:

pip uninstall xgboost
pip install xgboost

Fix on Linux

Install libgomp via your package manager:

# Ubuntu / Debian
sudo apt-get update && sudo apt-get install libgomp1

# CentOS / RHEL
sudo yum install libgomp

# Fedora
sudo dnf install libgomp

# Alpine (e.g. in Docker)
apk add libgomp

Check Which Python Jupyter Is Using

The most common hidden cause of this error is an environment mismatch: XGBoost is installed in one Python environment, but Jupyter is running from a different one.

Check which Python your Jupyter kernel is using:

import sys
print(sys.executable)

Then check if XGBoost is installed in that environment:

/path/to/your/python -c "import xgboost; print(xgboost.__version__)"

If XGBoost is installed but not in the path Jupyter is using, reinstall it in the correct environment:

/path/to/your/python -m pip install xgboost

Using Conda?

If you're using a conda environment, install XGBoost through conda rather than pip — conda resolves the native library dependencies automatically:

conda install -c conda-forge xgboost

Verifying the Fix

Once XGBoost imports successfully, run a quick sanity check:

import xgboost as xgb
import numpy as np

X = np.random.rand(100, 5)
y = np.random.randint(0, 2, 100)

model = xgb.XGBClassifier(n_estimators=10, eval_metric='logloss')
model.fit(X, y)
print("XGBoost is working correctly. Version:", xgb.__version__)

If this runs without errors, you're good to go.


Summary

PlatformRoot CauseFix
macOSMissing libomp.dylibbrew install libomp
WindowsMissing Visual C++ runtimeInstall VC++ Redistributable
LinuxMissing libgomp.sosudo apt-get install libgomp1
AnyEnvironment mismatchCheck sys.executable, reinstall in correct env
AnyStale import cacheRestart Jupyter kernel after installing

The fix is almost always a one-liner. The tricky part is knowing that you need to restart the kernel — not just re-run the cell — after making any changes.

No comments :

Post a Comment

Please leave your message queries or suggetions.

Note: Only a member of this blog may post a comment.