はじめに
Crystal言語は素晴らしい言語ですが、Ubuntuユーザーはinterpreterを使いたい場合、Crystalをソースコードからコンパイルする必要があります。
方法
リポジトリの準備
GitHubでCrystalのリポジトリをフォークし、ローカルにクローンします。
git clone https://github.com/kojix2/crystal
次に、リモートリポジトリにCrystalの公式リポジトリを追加します。
git remote add upstream https://github.com/crystal-lang/crystal
以下のコマンドで更新しますが、タグも更新する必要があるため、--tags
オプションも使用します。
git fetch upstream --tags
git merge upstream/master
Snapを使ったCrystalのインストール
Crystalはセルフホスティングされているため、初回インストールにはCrystalが必要です。Snapを使用すると簡単ですが、インタープリタ機能は利用できません。
sudo snap install crystal --classic
後述するように自分でビルドしたCrystalが準備できたら、パッケージマネージャーのCrystalは削除します。
sudo snap remove crystal
必要なパッケージのインストール
GitHubのWikiにあるAll required librariesを参考に、必要なライブラリをインストールします。
sudo apt-get install -y \
automake \
build-essential \
git \
libbsd-dev \
libedit-dev \
libevent-dev \
libgmp-dev \
libgmpxx4ldbl \
libpcre3-dev \
libssl-dev \
libtool \
libxml2-dev \
libyaml-dev \
lld \
llvm \
llvm-dev\
libz-dev
(上記は2024-01-19時点でのものです。最新の情報をご確認ください)
Crystalのコンパイル
リリースされたバージョンでコンパイルします。
git clean -f # 以前に生成されたファイルの削除
git checkout -b v1.11.2 refs/tag/1.11.2
cd crystal
コンパイルのためのオプションが表示されます。
make help
CRYSTAL which previous crystal compiler use
LLVM_CONFIG llvm-config command path to use
check Enable only check when running format
debug Add symbolic debug info
interpreter Enable interpreter feature
junit_output Path to output junit results
order Enable order for spec execution (values: "default" | "random" | seed number)
progress Enable progress output
release Compile in release mode
static Enable static linking
stats Enable statistics output
target Cross-compilation target
threads Maximum number of threads to use
verbose Run specs in verbose mode
recipes:
Build the compiler
$ make
Build the compiler with progress output
$ make progress=1
Clean up built files then build the compiler
$ make clean crystal
Build the compiler in release mode
$ make crystal release=1 interpreter=1
Run tests
$ make test
Run stdlib tests
$ make std_spec
Run compiler tests
$ make compiler_spec
Run generators (Unicode, SSL config, ...)
$ make -B generate_data
私は通常、以下のコマンドを使用します。
make release=1 interpreter=1 debug=1
Crystalはインクリメンタルコンパイルに対応していないので、make -j16
などとしても効果はありません。
既存のCrystalをアンインストールする
SnapでインストールしたCrystalは、ビルドが完了したら削除します。
sudo snap remove crystal
前回debパッケージを作ってインストールした場合も同様です。
sudo dpkg -r crystal
Crystalのインストール
checkinstall
を使用してdebパッケージを作成し、インストールします。
sudo checkinstall
おわりに
この方法が最適とは限りませんが、インタープリタがほしい場合は有効な方法の一つだと思います。
macOSではHomebrewを使用する方が簡単です。この記事が何かの参考になれば幸いです。皆さんはどのようにCrystalをインストールしていますか?
この記事は以上です。