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

(株)Qualiteg、Startup JAPAN EXPO 2026 出展レポート

(株)Qualiteg、Startup JAPAN EXPO 2026 出展レポート

こんにちは! Qualitegビジ開マーケティング部です! 2026年4月15日(水)から16日(木)までの2日間、幕張メッセで開催された「Startup JAPAN EXPO 2026」(主催:Eight / Sansan株式会社)に、 株式会社Qualitegとして出展してまいりました! Startup JAPAN EXPO 2026 出展概要 項目内容会期2026年4月15日(水)〜16日(木) 各日10:00〜17:00会場幕張メッセ 展示ホール7・8ブース16-16主催Eight(Sansan株式会社) 今回の展示テーマは—— 「依頼は並列に、思考は止めず。」 主力プロダクト「Bestllam®」に新搭載されたAIエージェント機能と、AIセキュリティソリューション「LLM-Audit™」の実演デモを、2日間にわたってお届けしました。 ブース番号は 16-16。展示ホール7・8の一角に、今回も気合いを入れて陣を構えました💪 プレスリリース 株式会社Qualiteg、

By Qualiteg ビジネス開発本部 | マーケティング部
【プレスリリース】株式会社Qualiteg、「Startup JAPAN EXPO 2026」に出展-「Bestllam®」に、AIエージェント機能を搭載-

【プレスリリース】株式会社Qualiteg、「Startup JAPAN EXPO 2026」に出展-「Bestllam®」に、AIエージェント機能を搭載-

2026年4月13日 プレスリリース 株式会社Qualiteg、「Startup JAPAN EXPO 2026」に出展株式会社Qualitegのプレスリリース(2026年4月13日 10時00分)株式会社Qualiteg、「Startup JAPAN EXPO 2026」に出展PR TIMES株式会社Qualiteg 「Bestllam®」に、AIエージェント機能を搭載 ― 依頼は並列に、思考は止めず。日本企業の業務システムに溶け込む"働くAI"へ ― 生成AI導入・AIエージェント・業務自動化・コンサルティング 株式会社Qualiteg(本社:東京都千代田区、代表取締役:三澤智則)は、2026年4月15日(水)から16日(木)まで幕張メッセで開催される「Startup JAPAN EXPO 2026」(ブース番号:16-16)に出展いたします。 この度、

By Qualiteg ニュース
Anthropicが「強すぎて出せないモデル "Mythos"」を出した

Anthropicが「強すぎて出せないモデル "Mythos"」を出した

Project Glasswingが映し出す、防御側のパラダイム転換 すごいモデルが出た、らしい 2026年4月7日、AnthropicがClaude Mythos Previewという新しいAIモデルを発表しました。(Anthropic公式発表 / Anthropic技術解説) Anthropicは、ChatGPTで知られるOpenAIと並ぶ米国の大手AI企業のひとつで、Claudeシリーズと呼ばれる生成AIモデルを開発しています。 普段なら、新モデル発表は「より速く、より賢くなりました」というアップデートの話で、誰でも触れるようになるのが通例です。 ところが今回はだいぶ様子が違いました。 一般公開はされません。 アクセスできるのは選ばれた一部のパートナーだけ。 同時に立ち上げられた業界横断プロジェクト「Project Glasswing」の枠組みの中で、防御目的に絞って提供される、という発表でした。 ただ、この話を「危険なAIが出た」の一言で受け止めると、もっと重要なところを取り逃してしまいます。 少し腰を据えて見ていきましょう! どのくらい「とんでも

By Qualiteg コンサルティング, Qualiteg AIセキュリティチーム
「AIを作る国」から「AIで勝つ国」へ ── 日本のAI投資戦略を再設計する【後編】

「AIを作る国」から「AIで勝つ国」へ ── 日本のAI投資戦略を再設計する【後編】

── SaaS再編の時代に、どこにポジションを取るか こんにちは! Qualitegコンサルティングです! ここ数年、「日本のAI戦略」というテーマでの相談やディスカッションが増えてきました。 生成AIの登場以降、経営層から現場のエンジニアまで、それぞれの立場で「自社はどこに張ればいいのか」「国としてはどう進むべきか」を模索している、というのが実感です。 本シリーズでは、その問いに対して少し腰を据えて向き合ってみたいと思い、前後編の構成で書いてみました。 前編では、国産LLM、データセンター投資、データ主権の3テーマを通じて、日本のAI投資が必ずしも「使われて勝つ構造」に向かっていない可能性を見てきました。投資の総額やプレイヤーの動きを並べてみると、号令の方向と実際の資金の流れにはちょっとしたズレがあるのではないか、という現在地が見えてきます。 後編では、その前提の上で視点をソフトウェア産業全体に広げます。もしAIによってアプリケーション層そのものの競争ルールが変わるなら、日本が張るべき場所もまた変わるはずです。海外で起きているSaaS産業の地殻変動を眺めたうえで、日本がど

By Qualiteg コンサルティング