WSL-Ubuntu で bitsandbytes のインストールに失敗するとき

WSL-Ubuntu で bitsandbytes のインストールに失敗するとき
Photo by Gabriel Heinzer / Unsplash

bitsandbytes を pip install しようとしたときに、以下のようなエラーがでたときの対処方法です

Could not load bitsandbytes native library: libcusparse.so.11: cannot open shared object file: No such file or directory
Traceback (most recent call last):
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/bitsandbytes/cextension.py", line 109, in <module>
    lib = get_native_library()
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/bitsandbytes/cextension.py", line 96, in get_native_library
    dll = ct.cdll.LoadLibrary(str(binary_path))
  File "/usr/lib/python3.10/ctypes/__init__.py", line 452, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib/python3.10/ctypes/__init__.py", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: libcusparse.so.11: cannot open shared object file: No such file or directory

CUDA Setup failed despite CUDA being available. Please run the following command to get more information:

python -m bitsandbytes

Inspect the output of the command and see if you can locate CUDA libraries. You might need to add them
to your LD_LIBRARY_PATH. If you suspect a bug, please take the information from python -m bitsandbytes
and open an issue at: https://github.com/TimDettmers/bitsandbytes/issues

デバッグ情報の表示と読み解き

Pythonのモジュール実行機能を使って、bitsandbytesをモジュール実行してデバッグ情報を表示してみます。

python -m bitsandbytes

Could not load bitsandbytes native library: libcusparse.so.11: cannot open shared object file: No such file or directory
Traceback (most recent call last):
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/bitsandbytes/cextension.py", line 109, in <module>
    lib = get_native_library()
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/bitsandbytes/cextension.py", line 96, in get_native_library
    dll = ct.cdll.LoadLibrary(str(binary_path))
  File "/usr/lib/python3.10/ctypes/__init__.py", line 452, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib/python3.10/ctypes/__init__.py", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: libcusparse.so.11: cannot open shared object file: No such file or directory

CUDA Setup failed despite CUDA being available. Please run the following command to get more information:

python -m bitsandbytes

Inspect the output of the command and see if you can locate CUDA libraries. You might need to add them
to your LD_LIBRARY_PATH. If you suspect a bug, please take the information from python -m bitsandbytes
and open an issue at: https://github.com/TimDettmers/bitsandbytes/issues
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++ BUG REPORT INFORMATION ++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++ OTHER +++++++++++++++++++++++++++
CUDA specs: CUDASpecs(highest_compute_capability=(8, 6), cuda_version_string='117', cuda_version_tuple=(11, 7))
PyTorch settings found: CUDA_VERSION=117, Highest Compute Capability: (8, 6).
To manually override the PyTorch CUDA version please see: https://github.com/TimDettmers/bitsandbytes/blob/main/docs/source/nonpytorchcuda.mdx
CUDA SETUP: WARNING! CUDA runtime files not found in any environmental path.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++ DEBUG INFO END ++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Checking that the library is importable and CUDA is callable...
Couldn't load the bitsandbytes library, likely due to missing binaries.
Please ensure bitsandbytes is properly installed.

For source installations, compile the binaries with `cmake -DCOMPUTE_BACKEND=cuda -S .`.
See the documentation for more details if needed.

Trying a simple check anyway, but this will likely fail...
Traceback (most recent call last):
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/bitsandbytes/diagnostics/main.py", line 66, in main
    sanity_check()
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/bitsandbytes/diagnostics/main.py", line 40, in sanity_check
    adam.step()
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/torch/optim/optimizer.py", line 280, in wrapper
    out = func(*args, **kwargs)
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/bitsandbytes/optim/optimizer.py", line 287, in step
    self.update_step(group, p, gindex, pindex)
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
    return func(*args, **kwargs)
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/bitsandbytes/optim/optimizer.py", line 496, in update_step
    F.optimizer_update_32bit(
  File "/home/mlu/.virtualenvs/ChatStream/lib/python3.10/site-packages/bitsandbytes/functional.py", line 1584, in optimizer_update_32bit
    optim_func = str2optimizer32bit[optimizer_name][0]
NameError: name 'str2optimizer32bit' is not defined
Above we output some debug information.
Please provide this info when creating an issue via https://github.com/TimDettmers/bitsandbytes/issues/new/choose
WARNING: Please be sure to sanitize sensitive info from the output before posting it.

実行時のエラー情報とともに、 BUG REPORT INFORMATION が表示されます。

ここには以下2つの重要な情報が含まれています

  • LD_LIBRARY_PATH に CUDA のパスを入れたほうがいいかも
  • 現在の venv にある PyTorch が使用している CUDA バージョンは PyTorch settings found: CUDA_VERSION=117 と表示されていることから 11.7

現在の WSL-Ubuntu にインストールされている CUDA バージョンを確認する

現在の WSL2 Ubuntu にインストールされている CUDA バージョンを確認します。

ls /usr/local/cuda* -d

でCUDAのインストールディレクトリ一覧をみてみると、

/usr/local/cuda /usr/local/cuda-12 /usr/local/cuda-12.4

とありましたので、 CUDA バージョン 12.4 がこのubuntuにインストールされていることがわかりました。

モジュール単位でより詳しいバージョン番号までみたいときは

cat /usr/local/cuda/version.json

で表示させることも可能です。

エラーメッセージの原因

このエラーメッセージの原因がみえてきました。

venv にインストールされている PyTorch は CUDA_VERSION=117 用のインストールであったにもかかわらず、実際に ubuntu にインストールされている CUDAバージョンが12.4 だったため、 11.7をさがしにいったbitsandbytes がCUDA ライブラリを見つけられなかった、 ことが原因でした。

対処法

対処法としては3つあります

  1. ubuntu の CUDA ライブラリを venv とおなじ CUDA 11.7 にする
  2. venv にあるライブラリを ubuntu とおなじ CUDA 12.4 にする
  3. bitsandbytes に CUDA 12.4 を使うように教え込む

ここでは、いちばん簡単にできる 3 でやってみます。

(これで、アプリケーションのテストが通れば問題ない。テストが通らなければ、1や2で対策することになるでしょう)

Bitsandbytes に CUDA バージョンを教える

bitsandbytes に CUDA 12.4 ライブラリのパスを指定するには、以下のようにします

export BNB_CUDA_VERSION=124

さて、これで、もういちどbitsadnbytesをモジュール実行してみます。

python -m bitsandbytes

WARNING: BNB_CUDA_VERSION=124 environment variable detected; loading libbitsandbytes_cuda124.so.
This can be used to load a bitsandbytes version that is different from the PyTorch CUDA version.
If this was unintended set the BNB_CUDA_VERSION variable to an empty string: export BNB_CUDA_VERSION=
If you use the manual override make sure the right libcudart.so is in your LD_LIBRARY_PATH
For example by adding the following to your .bashrc: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<path_to_cuda_dir/lib64

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++ BUG REPORT INFORMATION ++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++ OTHER +++++++++++++++++++++++++++
CUDA specs: CUDASpecs(highest_compute_capability=(8, 6), cuda_version_string='117', cuda_version_tuple=(11, 7))
PyTorch settings found: CUDA_VERSION=117, Highest Compute Capability: (8, 6).
WARNING: BNB_CUDA_VERSION=124 environment variable detected; loading libbitsandbytes_cuda124.so.
This can be used to load a bitsandbytes version that is different from the PyTorch CUDA version.
If this was unintended set the BNB_CUDA_VERSION variable to an empty string: export BNB_CUDA_VERSION=
If you use the manual override make sure the right libcudart.so is in your LD_LIBRARY_PATH
For example by adding the following to your .bashrc: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<path_to_cuda_dir/lib64

To manually override the PyTorch CUDA version please see: https://github.com/TimDettmers/bitsandbytes/blob/main/docs/source/nonpytorchcuda.mdx
CUDA SETUP: WARNING! CUDA runtime files not found in any environmental path.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++ DEBUG INFO END ++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Checking that the library is importable and CUDA is callable...
SUCCESS!
Installation was successful!

Installation was successful!

ということで、今度は、無事に bitsandbytes が CUDAライブラリをみつけることができうまく動いてくれそうです。

環境変数が有効になるように設定する

BNB_CUDA_VERSION が有効になるようにしましょう

.bashrc に入れる場合

ターミナルからPythonアプリを実行するときのために .bashrc に BNB_CUDA_VERSION 環境変数を追加しておきます

.bashrc を開いたら、最後の行に

export BNB_CUDA_VERSION=124

を記述しておきます。

コードで直接指定する場合

以下のようにすれば、pythonコードから直接指定することもできます

import os

# 環境変数 BNB_CUDA_VERSION を設定
os.environ['BNB_CUDA_VERSION'] = '124'

Read more

公開から3日で停止──Fable 5/Mythos 5をめぐる米政府指令が示した、AIの新しい可用性リスク

公開から3日で停止──Fable 5/Mythos 5をめぐる米政府指令が示した、AIの新しい可用性リスク

こんにちは! 前回の記事では、Anthropicが2026年6月9日に発表したClaude Fable 5とClaude Mythos 5について取り上げました。 Mythos級の強力な能力にセーフガードを加え、一般ユーザーにも提供できる形へと降ろしたFable 5。 私たちはそれを、「神話が寓話になって降りてきた」と表現しました。 しかし、その寓話は、わずか3日で公開の場から姿を消すことになります。 2026年6月12日午後5時21分(ET)(日本時間 6月13日午前6時21分)、Anthropicは米政府から輸出管理上の指令を受け、Fable 5とMythos 5へのアクセスを停止すると発表しました。 指令の対象とされたのは、米国外の利用者だけではありません。 Anthropicの説明によれば、米国内にいる外国籍者や、同社で働く外国籍の従業員も含まれます。 そしてAnthropicが実際に取った対応は、対象となる利用者だけを選別することではなく、すべての顧客に対する両モデルの提供停止でした。 今回の出来事は、Fable 5のセーフガードが十分だったのかという技術論

By Qualiteg コンサルティング, Qualiteg AIセキュリティチーム
ついに一般公開、Claude Mythos5(ミュトス)/  Fable 5(フェイブル) を実務視点で読み解く

ついに一般公開、Claude Mythos5(ミュトス)/ Fable 5(フェイブル) を実務視点で読み解く

こんにちは! Qualitegプロダクト開発部です。 2026年6月9日、Anthropicから Claude Fable 5(フェイブル5)と Claude Mythos 5(ミュトス5)が発表されました。 この記事では、 Fable 5 とは何か、Mythos 5 と何が違うのか、 Claude Code やAIエージェントを実務で使う立場から見て何が変わるのか を整理します。当社ブログを読んでくださっている方は、4月の「強すぎて出せないモデル "Mythos"」や「Mythosレベルのオープンモデルはいつ出るのか」でも触れた、あの Mythosクラスの一般公開版がついに来た、という話でもあります。 この記事でわかること * Fable 5 と Mythos 5 は「同じ基盤モデルだが、安全装置の有無が違う」こと * 高リスク領域では応答が Opus 4.

By Qualiteg コンサルティング, Qualiteg プロダクト開発部, Qualiteg 研究部
Claude Codeで正規の運用作業が「Usage Policy違反」になる理由 ── リアルタイム・サイバーセーフガードの誤検知と対処法

Claude Codeで正規の運用作業が「Usage Policy違反」になる理由 ── リアルタイム・サイバーセーフガードの誤検知と対処法

こんにちは! 今日は、Claude Code を使っていると突然出てくる「Usage Policy違反」エラー いわゆる リアルタイム・サイバーセーフガードの誤検知(false positive) について、その傾向と対処法を詳しく解説します! 自社サーバへのデプロイ作業中や、ごく普通のインフラ運用の最中に、こんなメッセージが出て手が止まった経験はありませんか? API Error: Claude Code is unable to respond to this request, which appears to violate our Usage Policy. This request triggered cyber-related safeguards. やっていたのは、自分のサーバー への SSH デプロイと、自社リポジトリへのコミット指示だけ。 攻撃的な操作は何ひとつ含まれていないはずなのに、ブロックされてしまう… そんな状況に心当たりのある方は、

By Qualiteg プロダクト開発部
個人情報検出の精度を、どう正しく語るか ― Recall、信頼区間、代表性から考える評価設計

個人情報検出の精度を、どう正しく語るか ― Recall、信頼区間、代表性から考える評価設計

こんにちは。Qualiteg研究部です。 私たちは、個人情報(PII)や機密情報、要配慮個人情報を含むセンシティブな情報を検出・マスキングする技術(https://pii-fi.com)の開発に取り組んでいます。 その中で日々向き合っているのが、 「精度の数字を、どうすれば正直に、正しく語れるのか」 という問題です。 たとえば、検出器の Recall(再現率)が 0.95 だったとします。 これは高い数字に見えます。しかし、その数字はどの種類の文書で測ったものなのか。正解データはどう作ったのか。サンプル数は十分なのか。別の業務文書にも同じ数字を当てはめてよいのか。 精度の数字は、単独ではほとんど意味を持ちません。 「何を、どの条件で、どう数えたか」とセットになって、はじめて実務で使える数字になります。 本記事では、私たちが PII 検出の精度評価に取り組む中で得た、精度を誠実に語るための考え方を紹介します。アルゴリズムの中身ではなく、評価のしかたに焦点を当てます。 1. はじめに:「Recall 0.95

By Qualiteg 研究部