YAMAGUCHI::weblog

海水パンツとゴーグルで、巨万の富を築きました。カリブの怪物、フリーアルバイター瞳です。

OPAMを使い始めた

はじめに

こんにちは、Python界の情弱です。OCamlをしばらく書いていなかったのですが、久々に書こうと思い、環境を再セットアップしました。 もうOCaml 4.00.1が出ていたり、そもそもセットアップはOPAMでするのが楽ちんだったりするらしいので、試してみました。

OPAMのインストール

基本的には本家のチュートリアルにある通りにやればいいだけなんで、簡単。

% wget http://www.ocamlpro.com/pub/opam_installer.sh
% sh ./opam_installer.sh /usr/local/bin

ずーっと最新版のOCamlのビルド&インストールを /home/$USER/.opam に対して行ってるログが流れてきますが、最後に設定ファイルへの書き込みを聞いてきます。

=-=-=-= Configuring OPAM =-=-=-=
Do you want to update your configuration to use OPAM ? [Y/n] Y
[1/4] Do you want to update your shell configuration file ? [default: ~/.zshrc]
[2/4] Do you want to update your ~/.ocamlinit ? [Y/n] Y
[3/4] Do you want to install the auto-complete scripts ? [Y/n] Y
[4/4] Do you want to install the `opam-switch-eval` script ? [Y/n] Y
User configuration:
  Generating ~/.ocamlinit.
  Updating ~/.zshrc.
Global configuration:
  Updating <root>/opam-init/init.sh
    auto-completion : [true]
    opam-switch-eval: [true]
  Updating <root>/opam-init/init.zsh
    auto-completion : [true]
    opam-switch-eval: [true]
  Updating <root>/opam-init/init.csh
    auto-completion : [true]
    opam-switch-eval: [true]

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

To complete the configuration of OPAM, you need to run:

    . /home/ymotongpoo/.opam/opam-init/init.zsh > /dev/null 2> /dev/null || true

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

To use OCaml installed by OPAM, use
eval `opam config env`

追加された諸々のファイルを見て、各種設定を書き換えました。(.zshrcから各種OS用設定ファイルに書き換えたり、ユーザ名が直に入っているものやホームディレクトリのものを$USERや$HOMEに書き換えたり、など)

基本操作

OMakeをインストールしようと思ったらビルドがこけて困っていたところ、「OPAMなら入るよ」という情報を聞いたのが、そもそものきっかけだったのでOMakeのインストールを例にしてやってみます。

% opam search omake
Available packages for 4.00.1:
omake        --  Build system designed for scalability and portability
omake-mode   --  Omake Emacs integration
spotinstall  --  A tool to facilitate the installation of OCaml annotation files (.cmt, .cmti, .spot, .spit).

OPAMによりインストールしたOCamlが4.00.1(執筆時最新)なので、それに対応したパッケージが表示されています。すばらしい。

% opam install omake
The following actions will be performed:
 - install ocamlfind.1.3.3 [required by omake]
 - install omake.0.9.8.6-0.rc1
2 to install | 0 to reinstall | 0 to upgrade | 0 to downgrade | 0 to remove
Do you want to continue ? [Y/n]
...

依存パッケージを見て、全部自動でビルド&インストールしてくれている。すばらしい!無事OMakeが入りました。 そういえばProject EulerCSVファイルを扱うことがちょいちょいあるので、関連パッケージが無いか見てみます。

% opam search csv
Available packages for 4.00.1:
csv  --  Library to read and write CSV files

% opam info csv
             package: csv
             version: 1.2.2
             depends: ocamlfind & oasis = 0.3.0
   available-version: 1.2.2
         description: Library to read and write CSV files

This library can read and write CSV files, including all extensions
used by Excel - eg. quotes, newlines, 8 bit characters in fields, "0
etc. The library comes with a handy command line tool called csvtool
for handling CSV files from shell scripts.

パッケージの内容も確認できてすばらしい!

OCaml自体のインストールは?

異なるバージョンを入れる

そういえばOPAMでOCamlの最新版は入れたけど、古いOCamlや今後バージョンが上がったらどうするの?ということで3.12.1も入れてみたり、バージョンを切り替えたりしてみる。

% opam switch list
4.00.1  C 4.00.1                     Official 4.00.1 release
--     -- 3.11.2                     Official 3.11.2 release
--     -- 3.12.1                     Official 3.12.1 release
...
--     -- 4.01.0dev+short-paths      latest trunk snapshot with short type names
--     -- 4.01.0dev+trunk            latest trunk snapshot
--     -- system                     System compiler (4.00.1)

なんかいっぱい出てきた!3.12.1があるのでそれを使ってみよう。

% opam switch 3.12.1
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 3574k  100 3574k    0     0   295k      0  0:00:12  0:00:12 --:--:--  767k
200Configuring for a x86_64-unknown-linux-gnu ...
gcc found
The C compiler is ANSI-compliant.
Checking the sizes of integers and pointers...
Wow! A 64 bit architecture!
...
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

To complete the configuration of OPAM, you need to run:

    eval `opam config env`

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

まだインストールされてないバージョンを指定したら、勝手にインストールが始まりました!

% eval `opam config env`
% ocaml -version
The Objective Caml toplevel, version 3.12.1
% opam switch list
4.00.1  I 4.00.1                     Official 4.00.1 release
3.12.1  C 3.12.1                     Official 3.12.1 release
--     -- 3.11.2                     Official 3.11.2 release
--     -- 4.00.0                     Official 4.00.0 release
--     -- 3.12.1+mirage-unix-direct  Mirage compiler for unix
...
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

To complete the configuration of OPAM, you need to run:

    eval `opam config env`

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

インストールされたあとにちゃんと指定したバージョンに切り替わっています。4.00.1も手元にあることが見て分かります。

% opam search omake
Available packages for 3.12.1:
omake       --  Build system designed for scalability and portability
omake-mode  --  Omake Emacs integration

3.12.1でOMake関連パッケージを探すと4.00.1の時とは違う結果が返って来ますね。4.00.1に戻してみます。

% opam switch 4.00.1
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

To complete the configuration of OPAM, you need to run:

    eval `opam config env`

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
% eval `opam config env`
% ocaml -version
The OCaml toplevel, version 4.00.1

戻りました。しかし、毎度evalするのはめんどくさいですね...

同じバージョンで複数の環境を作りたい

Pythonであればvirtualenv、Goであればgoenvなど、開発環境ごとにモジュール/パッケージの依存を切り分けたい時は次のオプションを使えばいいようです!

opam switch install <alias> --alias-of <version>

たとえば、いま既に4.00.1を入れてますが、ここで開発用に別の4.00.1の環境を用意してみます。

% opam switch install dev4.00.1 --alias-of 4.00.1
...
% eval `opam config env`
% opam switch list
dev4.00.1  C 4.00.1                     Official 4.00.1 release
4.00.1     I 4.00.1                     Official 4.00.1 release
3.12.1     I 3.12.1                     Official 3.12.1 release
--        -- 3.11.2                     Official 3.11.2 release
--        -- 4.00.0                     Official 4.00.0 release
--        -- 3.12.1+mirage-unix-direct  Mirage compiler for unix

おおおお!dev4.00.1という名前で4.00.1が新たに追加されました!先ほど4.00.1ではomakeパッケージを入れましたが、新しい環境では入ってないことを確認します。

% opam list -i
Installed packages for dev4.00.1:
base-bigarray  base  Bigarray library distributed with the OCaml compiler
base-threads   base  Threads library distributed with the OCaml compiler
base-unix      base  Unix library distributed with the OCaml compiler

% opam switch 4.00.1
% eval `opam config env`
% opam list -i
Installed packages for 4.00.1:
base-bigarray           base  Bigarray library distributed with the OCaml compiler
base-threads            base  Threads library distributed with the OCaml compiler
base-unix               base  Unix library distributed with the OCaml compiler
ocamlfind              1.3.3  A library manager for OCaml
omake          0.9.8.6-0.rc1  Build system designed for scalability and portability

ちゃんと環境が切り分けられていますね!

OPAMすばらしい!

すばらしいので、OCaml使う時は今後はOPAMから入れることにします。