はじめに
Amazon Linux2 上で、Puppeteer から Chrome を起動しようとしたところ、起動しなかった。ググったところ、こうすれば動く!みたいなものはいくつか出てきたが、「なんで?」が不明なものが多かった。
先に結論として、Chrome を起動するために必要なライブラリが足らなかったのが原因で、それは、yum install では、すんなり入らない(ものがある)ので、調べて入れる必要がある。
実行環境
$ node -v
v14.9.0
$ npm -v
6.14.8
Chrome のインストール
puppeteerを使ってCentOSでheadless chromeを動かす
ここに書いてある通り。
以下を追加
/etc/yum.repos.d/google.chrome.repo
[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
$ sudo yum search google-chrome
...
google-chrome-beta.x86_64 : Google Chrome (beta)
google-chrome-stable.x86_64 : Google Chrome
google-chrome-unstable.x86_64 : Google Chrome (unstable)
$ sudo yum install google-chrome-stable -y
$ sudo yum install ipa-gothic-fonts -y
Puppeteer のサンプルプログラム
よくある記念スクショのヤツ。
test.js
constpuppeteer=require('puppeteer');(async()=>{constbrowser=awaitpuppeteer.launch({args:['--no-sandbox','--disable-setuid-sandbox']});constpage=awaitbrowser.newPage();awaitpage.goto('https://www.google.com');awaitpage.screenshot({path:'example.png'});browser.close();})();
puppeteer インストール
$ npm install puppeteer
実行、そしてエラー
$ node test.js
(node:7520) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
..../node_modules/puppeteer/.local-chromium/linux-782078/chrome-linux/chrome: error while loading shared libraries: libXss.so.1: cannot open shared object file: No such file or directory
...
libXss.so.1
がないと。
本当にないのは、それだけか調べる。
$ cd node_modules/puppeteer/.local-chromium/linux-782078
$ ldd chrome | grep not
libXss.so.1 => not found
確かにこれだけ。こいつをインストールすれば動くようになるはず。
解決方法
足らないものは、yum でそのままインストールできてしまうか、それを含む何らかのパッケージを探してインストールするか、の2通り。libXss.so.1
は後者なので、これを含むパッケージを調べてインストールする。これ以外のファイルが足らないと出た場合も手順は同様。
調べる。
$ yum whatprovides */libXss.so.1
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Repository google-chrome is listed more than once in the configuration
google-chrome 3/3
amzn2-core/2/x86_64/filelists_db | 34 MB 00:00:00
amzn2extra-docker/2/x86_64/filelists_db | 14 kB 00:00:00
google-chrome/filelists | 1.8 kB 00:00:00
nodesource/x86_64/filelists_db | 250 kB 00:00:00
yarn/filelists_db | 165 kB 00:00:00
libXScrnSaver-1.2.2-6.1.amzn2.x86_64 : X.Org X11 libXss runtime library
Repo : amzn2-core
Matched from:
Filename : /usr/lib64/libXss.so.1
libXScrnSaver-1.2.2-6.1.amzn2.0.2.i686 : X.Org X11 libXss runtime library
Repo : amzn2-core
Matched from:
Filename : /usr/lib/libXss.so.1
libXScrnSaver-1.2.2-6.1.amzn2.0.2.x86_64 : X.Org X11 libXss runtime library
Repo : amzn2-core
Matched from:
Filename : /usr/lib64/libXss.so.1
libXScrnSaver
こいつに含まれているようなので、インストール。
$ sudo yum install libXScrnSaver -y
確認
$ ldd chrome | grep not
何も出てこないので、OK
再度実行
$ node test.js
成功!おしまい。