Vagrant + Ubuntu 18.04 + anyenv + Renv + グラフをSVG出力

ホストは Ubuntu 16.04(まだ 18.04 に移行してない……)。 VirtualBox のインストールについては割愛。

$ apt list --installed | grep -i virtualbox

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

unity-scope-virtualbox/xenial,xenial,now 0.1+13.10.20130723-0ubuntu1 all [インストール済み、自動]
virtualbox/xenial-updates,now 5.1.38-dfsg-0ubuntu1.16.04.1 amd64 [インストール済み]
virtualbox-dkms/xenial-updates,xenial-updates,now 5.1.38-dfsg-0ubuntu1.16.04.1 all [インストール済み]
virtualbox-qt/xenial-updates,now 5.1.38-dfsg-0ubuntu1.16.04.1 amd64 [インストール済み、自動]
virtualbox-source/xenial-updates,xenial-updates,now 5.1.38-dfsg-0ubuntu1.16.04.1 all [インストール済み、自動]

Vagrant は昔 apt でインストールしたものだが、 古かったので公式から取ってきてインストールし直した。

$ vagrant -v
Vagrant 1.8.1

$ sudo apt-get remove vagrant

$ sudo dpkg -i vagrant_2.1.5_x86_64.deb

$ vagrant -v
Vagrant 2.1.5

Vagrant 用に用意した適当なディレクトリに移動して

$ vagrant init bento/ubuntu-18.04

Vagrantfile を下記のように修正。 メモリはデフォルトの 1024 だと R のビルド時にスワップが発生したので 2048 にした。

  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    vb.gui = true

    # Customize the amount of memory on the VM:
    vb.memory = "2048"

    vb.customize [
      "modifyvm", :id,
      "--vram", "256",
      "--clipboard", "bidirectional"
    ]
  end

起動させる。

vagrant up

vagrant login: のプロンプトが出たら ユーザ vagrant, パスワード vagrant でログイン。

……でもいいが、キーボードが英語配列になるので vagrant ssh して作業した方が楽だったかも。


まず apt-get update して、次いでデスクトップ環境をインストールする。

sudo apt-get update
sudo apt-get install ubuntu-desktop

いっぱいインストールされるのでしばらく待つ(ubuntu-desktop のインストールが十数分)。

とりあえずベタに ubuntu-desktop をインストールしたが、 (軽量な方がいいとかの理由で)他のが良ければ適宜 xubuntu-desktop とか lubuntu-desktop とかにすると良さそう。

終わったらホスト側で

vagrant reload

以後はグラフィカルログインでログインする。 ユーザ、パスワードはさっきと同様に vagrant, vagrant

最初に出るセットアップ:

  • Livepatch … いったん無視する。右上の [Next] で次へ。
  • Help improve Ubuntu … お好みで。 [Next]
  • [Done]

Software Updater

[Install now] - キーボードレイアウトの設定: Japanese を選択したかったが、 いったん戻ってやり直そうとしたらなぜか選択できなくなって、 English (US) で進んでしまった。後で設定し直す。

Continue without installing GRUB? … インストールしなくても問題なかった

キーボードの設定

  • 左下の「Show Applications」から「Settings」を開く。
  • Region & Language
    • Manage Installed Languages
      • [Install]
      • [Install / Remove Languages...]
        • Japanese を選択して [Apply]
        • [Close]
    • Input Sources の [+]
      • Japanese を選択して [Add]
    • Japanese を一番上に移動させる

設定反映のため再起動。

# ホスト側で
vagrant reload

その他の設定

画面の自動ロックを無効化

Settings>Privacy>Screen Lock

R のインストール

apt でインストールされるものはバージョンが古いらしいが、 後で Renv でインストールする際の依存パッケージがだいたい入るので、 とりあえずインストールしておく。

r-baser-base-core どっちをインストールするのが適切なのか分からなかったが、 上記の記事を見ると r-base-core をインストールしているようなので、その通りにしてみる。

ターミナルは画面左上の「Activities」または左下の「Show Applications」で "terminal" と入力して出てきたものを使う。 または Alt+F2 から gnome-terminal で起動する。

sudo apt-get install r-base-core

これらがインストールされる:

The following NEW packages will be installed:
  autoconf automake autopoint autotools-dev build-essential bzip2-doc cdbs debhelper dh-autoreconf dh-strip-nondeterminism
  dh-translations dpkg-dev fakeroot g++ g++-7 gcc gcc-7 gfortran gfortran-7 gir1.2-harfbuzz-0.0 icu-devtools intltool jq
  libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libarchive-cpio-perl libasan4 libatomic1
  libblas-dev libblas3 libbz2-dev libcilkrts5 libfakeroot libfile-stripnondeterminism-perl libfile-which-perl libgcc-7-dev
  libgfortran-7-dev libgfortran4 libglib2.0-dev libglib2.0-dev-bin libgraphite2-dev libharfbuzz-dev libharfbuzz-gobject0
  libicu-dev libicu-le-hb-dev libicu-le-hb0 libiculx60 libitm1 libjpeg-dev libjpeg-turbo8-dev libjpeg8-dev libjq1
  liblapack-dev liblapack3 liblsan0 libltdl-dev liblzma-dev libmail-sendmail-perl libmpx2 libncurses5-dev libonig4
  libpcre16-3 libpcre3-dev libpcre32-3 libpcrecpp0v5 libpng-dev libpng-tools libquadmath0 libstdc++-7-dev
  libsys-hostname-long-perl libtcl8.6 libtk8.6 libtool libtsan0 libubsan0 m4 pkg-config po-debconf python3-distutils
  python3-lib2to3 python3-scour r-base-core r-base-dev r-cran-boot r-cran-class r-cran-cluster r-cran-codetools
  r-cran-foreign r-cran-kernsmooth r-cran-lattice r-cran-mass r-cran-matrix r-cran-mgcv r-cran-nlme r-cran-nnet
  r-cran-rpart r-cran-spatial r-cran-survival r-doc-html r-recommended scour

anyenv のインストール

どうせ Ruby とかも使うだろうからということで まず anyenv をインストールする。 手順はほぼ公式の README の通り。 (Ubuntu なので ~/.bashrc に追記する)

git clone https://github.com/riywo/anyenv ~/.anyenv

echo 'export PATH="$HOME/.anyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(anyenv init -)"' >> ~/.bashrc
exec $SHELL -l

Renv のインストール

$ anyenv install Renv

$ anyenv envs
Renv

$ exec $SHELL -l

R 3.5.1 のインストール

利用可能なバージョンを Renv install -l で確認して、 現時点で最新の 3.5.1 をインストールする。

$ Renv install 3.5.1
Downloading http://cran.r-project.org/src/base/R-3/R-3.5.1.tar.gz...
Installing R-3.5.1...

BUILD FAILED

Inspect or clean up the working tree at /tmp/R-build.20181016202219.8938
Results logged to /tmp/R-build.20181016202219.8938.log

Last 10 log lines:
checking whether wcstombs exists and is declared... yes
checking whether wctrans exists and is declared... yes
checking whether iswblank exists and is declared... yes
checking whether wctype exists and is declared... yes
checking whether iswctype exists and is declared... yes
checking for wctrans_t... yes
checking for mbstate_t... yes
checking for ICU... yes
checking for X... no
configure: error: --with-x=yes (default) and X11 headers/libs are not available

おそらく plot などでの描画で必要なので、 --with-x=yes のオプションはそのままにしておいて必要なパッケージの方をインストールする。

sudo apt-get install xorg-dev

これらがインストールされる:

The following NEW packages will be installed:
  libcapnp-0.6.1 libdmx-dev libdmx1 libdrm-dev libexpat1-dev libfontconfig1-dev libfontenc-dev libfreetype6-dev libfs-dev
  libfs6 libice-dev libmirclient-dev libmirclient9 libmircommon-dev libmircommon7 libmircookie-dev libmircookie2
  libmircore-dev libmircore1 libmirprotobuf3 libpciaccess-dev libpixman-1-dev libprotobuf-dev libprotobuf-lite10
  libpthread-stubs0-dev libsm-dev libx11-dev libx11-doc libxau-dev libxaw7-dev libxcb1-dev libxcomposite-dev libxcursor-dev
  libxdamage-dev libxdmcp-dev libxext-dev libxfixes-dev libxfont-dev libxft-dev libxi-dev libxinerama-dev libxkbcommon-dev
  libxkbfile-dev libxmu-dev libxmu-headers libxmuu-dev libxpm-dev libxrandr-dev libxrender-dev libxres-dev libxss-dev
  libxt-dev libxtst-dev libxv-dev libxvmc-dev libxxf86dga-dev libxxf86vm-dev mesa-common-dev mir-client-platform-mesa-dev
  x11proto-composite-dev x11proto-core-dev x11proto-damage-dev x11proto-dev x11proto-dri2-dev x11proto-fixes-dev
  x11proto-fonts-dev x11proto-gl-dev x11proto-input-dev x11proto-randr-dev x11proto-record-dev x11proto-render-dev
  x11proto-resource-dev x11proto-scrnsaver-dev x11proto-xext-dev x11proto-xf86bigfont-dev x11proto-xf86dga-dev
  x11proto-xf86vidmode-dev x11proto-xinerama-dev xorg-dev xorg-sgml-doctools xserver-xorg-dev xtrans-dev

再度ビルド。

$ Renv install 3.5.1
Downloading http://cran.r-project.org/src/base/R-3/R-3.5.1.tar.gz...
Installing R-3.5.1...

BUILD FAILED

Inspect or clean up the working tree at /tmp/R-build.20181016204050.19429
Results logged to /tmp/R-build.20181016204050.19429.log

Last 10 log lines:
checking for pcre/pcre.h... no
checking if PCRE version >= 8.20, < 10.0 and has UTF-8 support... yes
checking if PCRE version >= 8.32... yes
checking whether PCRE support suffices... yes
checking for pcre2-config... no
checking for curl-config... no
checking curl/curl.h usability... no
checking curl/curl.h presence... no
checking for curl/curl.h... no
configure: error: libcurl >= 7.22.0 library and headers are required with support for https
$ apt-cache search '^libcurl4.+-dev'
libcurl4-gnutls-dev - development files and documentation for libcurl (GnuTLS flavour)
libcurl4-nss-dev - development files and documentation for libcurl (NSS flavour)
libcurl4-openssl-dev - development files and documentation for libcurl (OpenSSL flavour)

この3つのうちどれが適切なのか分からなかったが、 上から順番に試そうということで libcurl4-gnutls-dev をインストールしたところ 先に進めたので、ひとまずこのまま進める。

(調べてないので分かりませんが、 内部で使っているライブラリが違うだけで libcurl の動作としてはどれでも同じという状況ではないかと想像)

$ sudo apt-get install libcurl4-gnutls-dev

$ Renv install 3.5.1
Downloading http://cran.r-project.org/src/base/R-3/R-3.5.1.tar.gz...
Installing R-3.5.1...
Installed R-3.5.1 to /home/vagrant/.anyenv/envs/Renv/versions/3.5.1

インストールできた。

グローバルに 3.5.1 を使うように設定。

$ Renv global 3.5.1

$ which R
/home/vagrant/.anyenv/envs/Renv/shims/R

$ R --version
R version 3.5.1 (2018-07-02) -- "Feather Spray"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
(略)

試しに使ってみる。

$ R -q
> d = data.frame(
+   x = c(1, 2, 3)
+  ,y = c(12, 13, 14)
+ )
> plot(d$x, d$y)
> 

別ウィンドウでグラフが表示された。問題なさそう。

グラフをSVGで出力する

d = data.frame(
  x = c(1, 2, 3)
 ,y = c(12, 13, 14)
)

svg(filename="sample.svg")
plot(d$x, d$y)
dev.off()

のようにして SVG に出力したいが、今の状態で実行するとこうなる:

> svg(filename="sample.svg")
Warning messages:
1: In svg(filename = "sample.svg") :
  unable to load shared object '/home/vagrant/.anyenv/envs/Renv/versions/3.5.1/lib/R/library/grDevices/libs//cairo.so':
  /home/vagrant/.anyenv/envs/Renv/versions/3.5.1/lib/R/library/grDevices/libs//cairo.so: cannot open shared object file: No such file or directory
2: In svg(filename = "sample.svg") : failed to load cairo DLL
> 

これを解消するには configure のオプションに --with-cairo=yes を付けて ビルドし直さないといけないらしい。

まず libcairo2-dev だけインストールしてみる:

$ sudo apt-get install libcairo2-dev
(略)
The following NEW packages will be installed:
  libcairo-script-interpreter2 libcairo2-dev libxcb-render0-dev libxcb-shm0-dev
(略)

configure のオプションはどこで指定すればいいか。 Renv install の時にビルド(configure 含む)しているようなので、適当に grep してみると:

$ pwd
/home/vagrant/.anyenv/envs/Renv

$ grep -r ./configure .
(略)
./plugins/R-build/README.md:* `CONFIGURE_OPTS` lets you pass additional options to `./configure`.
./plugins/R-build/bin/R-build:  { ./configure --prefix="$PREFIX_PATH" $CONFIGURE_OPTS
(略)

CONFIGURE_OPTS を使えば良いらしいと分かる。 R-build の readme もさらっと眺めて、 一度アンインストールして

Renv uninstall 3.5.1

再度 Renv install

CONFIGURE_OPTS='--with-cairo=yes' \
  Renv install 3.5.1

これで SVG 出力できるようになった。

$ R -q
> d = data.frame(
+   x = c(1, 2, 3)
+  ,y = c(12, 13, 14)
+ )
> 
> svg(filename="sample.svg")
> plot(d$x, d$y)
> dev.off()
null device 
          1 
> q()
Save workspace image? [y/n/c]: n

$ firefox sample.svg &
[1] 29954