StupidDog's blog

IT関連の手近な所で、疑問に思った事を調べた記録

「Ubuntu 12.10で未インストールのコマンドを入力すると、Sorry, command-not-found has crashed! ~」とメッセージが表示される件

はじめに

Ubuntuで未インストールのコマンドを入力すると、該当するパッケージを探してインストールの案内を表示してくれる。

$ pp
The program 'pp' is currently not installed. You can install it by typeing:
sudo apt-get install libpar-packer-perl

しかし、障害で強引なログインをしている際に、未インストールのコマンドで下記のようなエラーメッセージが表示された。
「command-not-found」がクラッシュしたからバグレポート送って欲しいって・・・

Distributor ID:	Ubuntu
Description:	Ubuntu 12.10
Release:	12.10
Codename:	quantal
Sorry, command-not-found has crashed! Please file a bug report at:
https://bugs.launchpad.net/command-not-found/+filebug
Please include the following information with the report:

command-not-found version: 0.3
Python version: 3.2.3 final 0
Exception information:

unsupported locale setting
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/CommandNotFound/util.py", line 24, in crash_guard
    callback()
  File "/usr/lib/command-not-found", line 69, in main
    enable_i18n()
  File "/usr/lib/command-not-found", line 40, in enable_i18n
    locale.setlocale(locale.LC_ALL, '')
  File "/usr/lib/python3.2/locale.py", line 541, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

「command-not-found」とは?

冒頭の「未インストールのコマンドを入力すると、該当するパッケージを探してインストールの案内を表示してくれる」を処理している、Pythonで書かれたスクリプトである。
手元の環境では下記の場所にあった。

/usr/lib/command-not-found

クラッシュの原因は?

調べた結果、言語設定*1が正しく設定されていない場合に「locale.setlocale()」が失敗するらしい。

対応

バグレポートを送る程ではなく言語設定を再設定すれば解決する。
※メッセージが「言語設定を確認して~」になれば良いとは思う。

下記のコマンドで設定可能な言語設定のリストを表示する。

$ locale -a
C
C.UTF-8
...
en_US.utf8
...

言語設定を仮で、"en_US"と設定していたが、そんな値はなく"en_US.utf8"しか"en_US~"が無かった。

$ LANG=en_US.utf8

と再設定して本件は解決した。