0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ubuntu20.04にGemini APIをインストールする

Posted at

はじめに

 Oracle VirtualBox上のUbuntu22.04では、公式のGemini API クイックスタートの手順通りにGemini APIを使用できたが、実機に入っているUbuntu20.04ではエラーが出て実行できなかった。この記事では、仮想環境を使用せず、Ubuntu20.04のデフォルトのPythonバージョンを変更する方法で説明する。

この方法を実行すると、異なるPythonバージョンが混在することになり、他の実行環境に影響を与える場合があるのでご注意ください。仮想環境の使用を推奨します。

実行環境

  • Ubuntu 20.04 LTS (インストール直後の状態)

エラーが発生した実行内容

以下の記事を参考にして、google-generativeaiをインストールし、API Keyをエクスポートした後に、test.pyを作成・実行した。

test.py
import google.generativeai as genai

genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))

model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("Explain how AI works")
print(response.text)

実行したコマンド

$ pip install google-generativeai
$ export GOOGLE_API_KEY="My_API_Key"
$ python3 test.py

エラーメッセージ

$ python3 test.py
~省略~
AttributeError: module 'google.generativeai' has no attribute 'GenerativeModel'

原因

 Pythonのバージョンとpipのバージョンが、google-generativeaiと互換性を持たないことが原因のようだ。google-generativeaiは推奨されるバージョンがPython3.9~3.11である。(一応Python3.8でも動くって書いてあるけど...)

しかし、Ubuntu20.04のPythonはデフォルトがPython3.8らしい。

この状態でgoogle-generativeaiをインストールしても、現時点で最新版の0.8.3ではなく初期版の0.1.0rc1がインストールされてしまう。

$ pip list | grep google-generativeai

google-generativeai        0.1.0rc1

初期版は'GenerativeModel'には対応していないようなので、Pythonとpipのバージョンを上げる必要がある。

解決方法

1. すでにインストールされているgoogle-generativeai関連パッケージとpipをアンインストールする。

$ python3 -m pip uninstall google-ai-generativelanguage google-generativeai protobuf grpcio-status
$ python3 -m pip uninstall pip

2. Python3.10をインストールし、デフォルトのPythonバージョンに変更する。

$ sudo apt update
$ sudo apt install software-properties-common

以下でRepositoryを登録する。追加で登録するか聞かれるが、Enterで続行してOK。

$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt install python3.10
$ reboot

ここで、インストールされているpythonを確認する。

$ ls /usr/bin/ | grep python
dh_python2
glade2script-python3
python2
python2.7
python3
python3-config
python3-futurize
python3-pasteurize
python3.10
python3.8
python3.8-config
x86_64-linux-gnu-python3-config
x86_64-linux-gnu-python3.8-config

2つのバージョンのpythonをupdate-alternativeの登録し、デフォルトをpython3.10に手動で設定する。

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1
update-alternatives: /usr/bin/python (python) を提供するために自動モードで /usr/bin/python3.10 を使います

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2
update-alternatives: /usr/bin/python (python) を提供するために自動モードで /usr/bin/python3.8 を使います

$ sudo update-alternatives --config python
alternative python (/usr/bin/python を提供) には 2 個の選択肢があります。

  選択肢    パス               優先度  状態
------------------------------------------------------------
* 0            /usr/bin/python3.8    2         自動モード
  1            /usr/bin/python3.10   1         手動モード
  2            /usr/bin/python3.8    2         手動モード

現在の選択 [*] を保持するには <Enter>、さもなければ選択肢の番号のキーを押してください: 1
update-alternatives: /usr/bin/python (python) を提供するためにマニュアルモードで /usr/bin/python3.10 を使います

これで、デフォルトがpython3.10に設定できた!

$ python --version
Python 3.10.16

3. Python3.10用のpipを再インストールする。

$ reboot
$ sudo apt update

#pipを入れ直す
$ sudo apt remove python3-pip
$ curl -sS https://bootstrap.pypa.io/get-pip.py | sudo python
Collecting pip
  Using cached pip-24.3.1-py3-none-any.whl.metadata (3.7 kB)
Using cached pip-24.3.1-py3-none-any.whl (1.8 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 24.3.1
    Uninstalling pip-24.3.1:
      Successfully uninstalled pip-24.3.1
Successfully installed pip-24.3.1
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, possibly rendering your system unusable.It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv. Use the --root-user-action option if you know what you are doing and want to suppress this warning.

$ pip3 -V
pip 24.3.1 from /usr/local/lib/python3.10/dist-packages/pip (python 3.10)

4. google-generativeaiをインストールする。

$ pip install google-generativeai

最後に、google-generativeaiのバージョンが最新であればOK

$ pip list | grep google-generativeai

google-generativeai        0.8.3

これでUbuntu上でGeminiが使えて幸せになれます!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?