0
1

WSLのUbuntuにプログラミング言語の管理ツールasdfを導入する

Last updated at Posted at 2024-02-24

はじめに

WSLのUbuntuにプログラミング言語をインストールする際、通常はaptなどの標準パッケージ管理ツールを利用すると思います。aptでインストールする場合、例えばプログラミング言語ごとにバージョンを切り替える手順が違ったりと面倒なのですが、asdfを使用すると、asdfが提供する統一されたコマンドインターフェースで言語のバージョンの切り替えが行えたりします。

本記事では、asdfのインストール手順、asdfによる言語のインストール手順、言語バージョンの切り替え方法をご紹介します。

asdfについては、以下のページをご参照ください。

本記事の前提環境としては、以下の記事をご参照ください。
「WSL+Ubuntu+Docker+VSCode+Githubの構築手順」
https://qiita.com/perilla/items/ef73cf57ade24bc94f19

asdfインストール

asdfの導入手順につきましては、以下のページを参照ください。

Githubリポジトリからasdfをクローン

# バージョンが頻繁に更新されるため、Getting StartedページのOfficial Download欄のコマンドをご確認ください
git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.13.1

PATHを通す

Ubuntuの~/.bashrcに以下の記述を追加してPATHを通してコマンドとして使用できるようにします。

[Ubuntu] ~/.bashrc
. "$HOME/.asdf/asdf.sh"
. "$HOME/.asdf/completions/asdf.bash"

source ~/.bashrcを実行するか、WindowsターミナルでUbuntuの新しいタブを開いて反映します。
which asdfを実行して、asdf実行ファイルのパスが表示されればOKです。

asdfが対応している言語一覧

Ubuntu
asdf plugin list all

言語の一覧とリポジトリが多数表示される。

phpとnodejsに絞って表示してみる。

Ubuntu
asdf plugin list all | grep -e php -e nodejs
# nodejs                       *https://github.com/asdf-vm/asdf-nodejs.git
# php                          *https://github.com/asdf-community/asdf-php.git

インストールしたい言語の右側に表示されるgitリポジトリのURLを開いて、READMEを参照して、言語をインストールしていきます。

言語のインストール

nodejsの言語ごとにGithubリポジトリがありますので、READMEを参照しながらインストールします。

node.jsインストール

リポジトリのREADMEを参照します。
https://github.com/asdf-vm/asdf-nodejs.git

言語をasdfに追加します

Ubuntu
asdf plugin add nodejs

利用可能なバージョン一覧

Ubuntu
asdf list all nodejs

最新バージョンのnodejsをインストール

Ubuntu
asdf install nodejs latest

グローバルのバージョンを指定します。
(~/.tool-versionにグローバルで選択されるバージョン番号が書き込まれます)

Ubuntu
asdf global nodejs latest

phpインストール

リポジトリのREADMEを参照します。
https://github.com/asdf-community/asdf-php.git

言語をasdfに追加します

Ubuntu
asdf plugin add php https://github.com/asdf-community/asdf-php.git

利用可能なバージョン一覧

Ubuntu
asdf list all php

READMEを参照すると、workflow.ymlに記述されているapt-getを参考に依存パッケージをインストールするよう記述がありますので、依存パッケージをインストールします。(※ runs-on: ubuntu-latestの箇所です)

Ubuntu
sudo apt install -y autoconf bison build-essential curl gettext git libgd-dev libcurl4-openssl-dev libedit-dev libicu-dev libjpeg-dev libmysqlclient-dev libonig-dev libpng-dev libpq-dev libreadline-dev libsqlite3-dev libssl-dev libxml2-dev libzip-dev openssl pkg-config re2c zlib1g-dev

最新バージョンのphpをインストール

Ubuntu
asdf install php latest

8.2.14バージョンのphpをインストール

Ubuntu
asdf install php 8.2.14

asdfにインストールされているphpバージョン一覧

Ubuntu
asdf list php

グローバルのバージョンを指定します。
(~/.tool-versionにグローバルで選択されるバージョン番号が書き込まれます)

Ubuntu
asdf global php latest

現在の言語バージョン一覧を表示

言語バージョンの変更方法は、以下のページを参照します。
https://asdf-vm.com/manage/versions.html

Ubuntu
asdf current
# nodejs          21.5.0          /home/[user]/.tool-versions
# php             8.3.1           /home/[user]/.tool-versions

phpでバージョン確認

Ubuntu
php -v
# PHP 8.3.1 (cli) (built: Jan 13 2024 21:54:08) (NTS)
# Copyright (c) The PHP Group
# Zend Engine v4.3.1, Copyright (c) Zend Technologies

phpのバージョンを8.2.14に変更

Ubuntu
asdf global php 8.2.14

asdf current php
# php             8.2.14          /home/[user]/.tool-versions

php -v
# PHP 8.2.14 (cli) (built: Jan 13 2024 21:59:21) (NTS)
# Copyright (c) The PHP Group
# Zend Engine v4.2.14, Copyright (c) Zend Technologies

さいごに

以上で、asdfのインストール方法から言語バージョンの変更方法までの手順をご紹介しました。

asdfでは多くの言語がサポートされている中で、今回の例ではnodejsとphpのインストールを例とさせて頂きましたが、要領は掴んでいただけたのではないでしょうか。

asdfに関する詳細は、冒頭でも掲載させて頂きました公式ページを参照して頂いて、ぜひ試して頂けたら幸いです。

0
1
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
1