コンテンツにスキップ

Ubuntu 22.04 へ複数ユーザでの共有を前提に asdf をインストールする

以前に Ubuntu 24.04 へ asdf をインストールして言語 / ツールのバージョンを管理する というメモを書きました。 この過去メモでは相互に環境を汚染しないように「asdf をユーザ毎に分離して利用する」前提にしていました。 その為、以下の状態になります。

  • ユーザ A がインストールしたプラグインは、ユーザ A が利用出来る
  • ユーザ B がインストールしたプラグインは、ユーザ B が利用出来る
  • ユーザ A がインストールしたプラグインを、ユーザ B が利用することは出来ない

今回は複数ユーザでの共有することを前提に asdf をインストールする手順をメモしておきます。

検証環境

検証環境の Ubuntu では bash を利用している前提としています。 他のシェルを利用している場合は、各々の環境に併せて環境変数の定義方法を調整します。

対象 バージョン
Ubuntu 24.04.4LTS
asdf v0.14.0-ccdd47d

実行するコマンド

このメモで実行しているコマンドは以下です。

apt -y install curl git
git clone https://github.com/asdf-vm/asdf.git /opt/asdf --branch v0.14.0
cat << 'EOF' >> ~/.bashrc

# asdf settings.
export ASDF_DIR="/opt/asdf"
export ASDF_DATA_DIR="/opt/asdf"
. "/opt/asdf/asdf.sh"
. "/opt/asdf/completions/asdf.bash"
EOF
cat << 'EOF' >> /etc/skel/.bashrc

# asdf settings.
export ASDF_DIR="/opt/asdf"
export ASDF_DATA_DIR="/opt/asdf"
. "/opt/asdf/asdf.sh"
. "/opt/asdf/completions/asdf.bash"
EOF
source ~/.bashrc
apt -y install \
  build-essential \
  curl \
  libbz2-dev \
  libffi-dev \
  liblzma-dev \
  libncursesw5-dev \
  libreadline-dev \
  libsqlite3-dev \
  libssl-dev \
  libxml2-dev \
  libxmlsec1-dev \
  tk-dev \
  xz-utils \
  zlib1g-dev
asdf plugin add python
asdf install python 3.12.2
asdf global python 3.12.2

インストールする

asdf をインストールするには事前に curl と git をインストールしておく必要があります。 apt でインストールしておきます。

apt -y install curl git

~/.asdf へ git clone します。

git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.14.0

これでインストール完了です。

環境変数の定義

必要な環境変数を ~/.bashrc へ追記します。 ASDF_DIR は asdf の本体であるコアスクリプトを保存するディレクトリを、ASDF_DATA_DIR はインストールしたプラグインを保存するディレクトリを、各々指定します。 これらの定義が省略された場合、いずれもデフォルトでは ~/.asdf ディレクトリが利用されます。 デフォルトの ~/.asdf では「別ユーザがアクセス出来ない」為、今回は全てのユーザがアクセス出来る /opt/asdf を指定しています。

cat << 'EOF' >> ~/.bashrc

# asdf settings.
export ASDF_DIR="/opt/asdf"
export ASDF_DATA_DIR="/opt/asdf"
. "/opt/asdf/asdf.sh"
. "/opt/asdf/completions/asdf.bash"
EOF

~/.bashrc へ追記した内容を反映します。

source ~/.bashrc

ただ、これだけでは新規に追加したユーザは asdf を利用出来ません。 新規作成したユーザ用の .bashrc は /etc/skel/.bashrc をコピーして作成されます。 その為、このファイルにも同様に asdf 関連の設定を追加しておきます。

cat << 'EOF' >> /etc/skel/.bashrc

# asdf settings.
export ASDF_DIR="/opt/asdf"
export ASDF_DATA_DIR="/opt/asdf"
. "/opt/asdf/asdf.sh"
. "/opt/asdf/completions/asdf.bash"
EOF

基本的な使い方

asdf の基本的な利用の流れは以下です。

  1. 各言語/ツールのプラグインをインストールする
  2. 各言語/ツールの指定バージョンをインストールする
  3. インストールしたバージョンを利用出来るように設定する

asdf でよく利用するコマンドには以下などがあります。

コマンド 説明
asdf update asdf 自身をアップデートする
asdf plugin list インストール済みのプラグイン一覧を表示する
asdf plugin list all インストール可能なプラグイン一覧を表示する
asdf list インストール済みのプラグイン・バージョンを表示する
asdf list [PLUGIN] インストール済みの NAME プラグイン・バージョンを表示する
asdf list all [PLUGIN] インストール可能な NAME プラグインのバージョン一覧を表示する
asdf plugin add [PLUGIN] NAME プラグインをインストールする
asdf plugin remove [PLUGIN] NAME プラグインをアンインストールする
asdf plugin update [PLUGIN] NAME プラグインをアップデートする
asdf plugin update --all インストール済みのプラグインを全てアップデートする
asdf install [PACKAGE] PACKAGE をインストールする
asdf install [PACKAGE] [VERSION] 指定バージョンの PACKAGE をインストールする
asdf uninstall [PACKAGE] [VERSION] 指定バージョンの PACKAGE をアンインストールする
asdf global [PACKAGE] [VERSION] システム全体で利用する PACKAGE のバージョンを指定する
asdf local [PACKAGE] [VERSION] 特定のディレクトリ配下のみで利用する PACKAGE のバージョンを指定する

Python プラグインをインストールする

試しに Python プラグインをインストールしてみます。 Python プラグインを利用する為に必要なソフトウェアをインストールしておきます。

apt -y install \
  build-essential \
  curl \
  libbz2-dev \
  libffi-dev \
  liblzma-dev \
  libncursesw5-dev \
  libreadline-dev \
  libsqlite3-dev \
  libssl-dev \
  libxml2-dev \
  libxmlsec1-dev \
  tk-dev \
  xz-utils \
  zlib1g-dev

まず Python 用のプラグインをインストールします。 asdf plugin add python を実行します。

asdf plugin add python

インストール可能な Python のバージョンを確認します。 asdf list all python を実行します。

# asdf list all python
2.1.3
2.2.3
2.3.7
      

今回はバージョン 3.12.2 をインストールすることにします。 asdf install python 3.12.2 を実行します。

asdf install python 3.12.2

インストールした Python 3.12.2 がシステム全体で利用されるように設定します。

asdf global python 3.12.2

asdf list を実行すると 3.12.2 の横にアスタリスクが付いており、「システム全体で利用されている」ことが分かります。

# asdf list
python
 *3.12.2

実際に pythonpython3 のバージョンを確認すると 3.12.2 が利用されていることが分かります。

# python --version
Python 3.12.2
# python3 --version
Python 3.12.2

これは対象ユーザのホームディレクトリ配下に .tool-versions ファイルが無く、asdf で利用するプラグインバージョンの定義が存在しない為です。 asdf list を実行すると「3.12.2 がインストールされている」ことは分かるのですが、「*」マークが付いておらず、利用すべきバージョンの指定が無いことが分かります。

$ asdf list
python
  3.12.2

3.12.2 を利用するよう、指定します。

asdf global python 3.12.2

これで「*」マークが付与されました。

$ asdf list
python
 *3.12.2

~/.tool-versions ファイルも作成されていることが分かります。

$ cat ~/.tool-versions
python 3.12.2

新規作成したユーザでの利用

新規作成したユーザで python3 のパスを確認すると asdf の管理下である /opt/asdf のバイナリが認識されています。

$ which python3
/opt/asdf/shims/python3

但し、実際にこのバイナリを利用しようとすると以下のようなエラーになってしまいます。

$ python3 --version
No version is set for command python3
Consider adding one of the following versions in your config file at
python 3.12.2

Python プラグインは内部で pyenv を利用している

Python プラグインは下記の GitHub リポジトリで管理されているようです。

# asdf plugin list all | grep python
python                       *https://github.com/danhper/asdf-python.git

このリポジトリの内容を見ていくと、例えば asdf-python/bin/help.links などに「内部的には pyenv を利用している」旨の記載が見受けられます。

# Output should be <title>: <link>

echo 'home-page: https://github.com/danhper/asdf-python
dependencies: https://github.com/pyenv/pyenv/wiki#suggested-build-environment
troubleshooting: https://github.com/pyenv/pyenv/wiki/Common-build-problems
'

尚、Python プラグイン (≒ pyenv) 以外にも poetryrye も利用可能なようです。

# asdf plugin list all | grep -e asdf-poetry -e asdf-rye
poetry                        https://github.com/asdf-community/asdf-poetry.git
rye                           https://github.com/Azuki-bar/asdf-rye

pip で追加したコマンドが使えない場合の対処

asdf 環境の Python に pip で追加したコマンドが使えない場合の対処 に記載していますが、pip で追加したコマンドが利用出来ない場合は以下を実行します。

asdf reshim python

参考

# asdf --help
version: v0.14.0-ccdd47d

MANAGE PLUGINS
asdf plugin add <name> [<git-url>]      Add a plugin from the plugin repo OR,
                                        add a Git repo as a plugin by
                                        specifying the name and repo url
asdf plugin list [--urls] [--refs]      List installed plugins. Optionally show
                                        git urls and git-ref
asdf plugin list all                    List plugins registered on asdf-plugins
                                        repository with URLs
asdf plugin remove <name>               Remove plugin and package versions
asdf plugin update <name> [<git-ref>]   Update a plugin to latest commit on
                                        default branch or a particular git-ref
asdf plugin update --all                Update all plugins to latest commit on
                                        default branch


MANAGE PACKAGES
asdf current                            Display current version set or being
                                        used for all packages
asdf current <name>                     Display current version set or being
                                        used for package
asdf global <name> <version>            Set the package global version
asdf global <name> latest[:<version>]   Set the package global version to the
                                        latest provided version
asdf help <name> [<version>]            Output documentation for plugin and tool
asdf install                            Install all the package versions listed
                                        in the .tool-versions file
asdf install <name>                     Install one tool at the version
                                        specified in the .tool-versions file
asdf install <name> <version>           Install a specific version of a package
asdf install <name> latest[:<version>]  Install the latest stable version of a
                                        package, or with optional version,
                                        install the latest stable version that
                                        begins with the given string
asdf latest <name> [<version>]          Show latest stable version of a package
asdf latest --all                       Show latest stable version of all the
                                        packages and if they are installed
asdf list <name> [version]              List installed versions of a package and
                                        optionally filter the versions
asdf list all <name> [<version>]        List all versions of a package and
                                        optionally filter the returned versions
asdf local <name> <version>             Set the package local version
asdf local <name> latest[:<version>]    Set the package local version to the
                                        latest provided version
asdf shell <name> <version>             Set the package version to
                                        `ASDF_${LANG}_VERSION` in the current shell
asdf uninstall <name> <version>         Remove a specific version of a package
asdf where <name> [<version>]           Display install path for an installed
                                        or current version
asdf which <command>                    Display the path to an executable


UTILS
asdf exec <command> [args...]           Executes the command shim for current version
asdf env <command> [util]               Runs util (default: `env`) inside the
                                        environment used for command shim execution.
asdf info                               Print OS, Shell and ASDF debug information.
asdf version                            Print the currently installed version of ASDF
asdf reshim <name> <version>            Recreate shims for version of a package
asdf shim-versions <command>            List the plugins and versions that
                                        provide a command
asdf update                             Update asdf to the latest stable release
asdf update --head                      Update asdf to the latest on the master branch

RESOURCES
GitHub: https://github.com/asdf-vm/asdf
Docs:   https://asdf-vm.com

"Late but latest"
-- Rajinikanth