{"id":566,"date":"2023-10-04T11:03:40","date_gmt":"2023-10-04T11:03:40","guid":{"rendered":"https:\/\/blog.vavensoft.com\/?p=566"},"modified":"2023-10-04T11:03:40","modified_gmt":"2023-10-04T11:03:40","slug":"how-to-install-puppeteer-on-ubuntu","status":"publish","type":"post","link":"https:\/\/blog.vavencloud.com\/?p=566","title":{"rendered":"How to integrate Puppeteer with Node on Ubuntu"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-text-align-left\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-black-color\">&#8220;Puppeteer&#8221; is a special tool made by the Chrome team that lets you use a web browser, like Chrome, without seeing it on your screen. You can tell it what to do using code. People use Puppeteer for things like copying information from websites, filling out online forms automatically, making PDF files, and lots of other tasks on the internet. It&#8217;s like having a robot that can browse the web for you.<\/mark><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Update your system<\/h2>\n\n\n\n<pre class=\"wp-block-code alignwide\"><code>sudo apt-get update -y<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Install dependencies<\/h2>\n\n\n\n<pre class=\"wp-block-code alignwide\"><code>sudo apt install -y gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget<\/code><\/pre>\n\n\n\n<p>Puppeteer, even though it doesn&#8217;t have a graphical interface, relies on some libraries to work with the X11 server. One of these libraries is libxcb, which needs a distributed library called libX11-xcb.so.1. To fix this, you can install the libx11-xcb1 package on Debian-based systems.<\/p>\n\n\n\n<p>But sometimes, when you install a missing library, you might run into another missing library, and this can keep happening. That&#8217;s why it&#8217;s important to install a list of the required libraries to ensure Puppeteer runs smoothly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Install NodeJS using NVM<\/h2>\n\n\n\n<pre class=\"wp-block-code alignwide\"><code>$ wget https:\/\/raw.githubusercontent.com\/nvm-sh\/nvm\/master\/install.sh\n$ bash install.sh\n$ source ~\/.bashrc\n$ nvm list-remote \n$ nvm install v18\n$ nvm install node\n$ nvm use 18\n$ node --version<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Install puppeteer<\/h2>\n\n\n\n<pre class=\"wp-block-code alignwide\"><code>$ mkdir your-project\n$ mkdir -p \/home\/ubuntu\/.cache\/puppeteer\n$ chmod -R 700 \/home\/ubuntu\/.cache\/puppeteer \n$ cd your-project \n$ npm i\n$ npm install puppeteer\n$ cd \/home\/ubuntu\/.cache\/puppeteer\n$ npm i\n$ npm install puppeteer<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Install Google Chrome<\/h2>\n\n\n\n<pre class=\"wp-block-code alignwide\"><code>$ wget https:\/\/dl.google.com\/linux\/direct\/google-chrome-stable_current_amd64.deb\n$ sudo apt install .\/google-chrome-stable_current_amd64.deb -y\n$ google-chrome --version<\/code><\/pre>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<h2 class=\"wp-block-heading\">To test if Puppeteer was installed successfully, you can create a JavaScript script named &#8220;test.js&#8221; and use the following code:<\/h2>\n\n\n\n<pre class=\"wp-block-code alignwide\"><code>const puppeteer = require('puppeteer');\n(async () =&gt; {\n  const browser = await puppeteer.launch({\n    args: &#91;\"--no-sandbox\", \"--disable-setuid-sandbox\"]\n  });\n  const page = await browser.newPage();\n  const url = 'https:\/\/google.com';\n  await page.goto(url);\n  await page.pdf({ path: 'page.pdf', format: 'A4' });\n  await browser.close();\n})();<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Test it:<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code alignwide\"><code>node test.js<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Troubleshooting : <\/h2>\n\n\n\n<p>Error 1 : Could not find Chrome (ver. xxx.xxx.xx).<\/p>\n\n\n\n<pre class=\"wp-block-code alignwide\"><code>1. you did not perform an installation before running the script (e.g. `npm install`) or\n2. your cache path is incorrectly configured (which is: \/home\/ubuntu\/.cache\/puppeteer), please check chrome directory exist or not .\n   $ ls \/home\/ubuntu\/.cache\/puppeteer\n3. Please verify whether Google Chrome is correctly installed \n   $ ls \/usr\/bin\/google-chrome\n    <\/code><\/pre>\n\n\n\n<p>Error 2: Failed to launch the browser process! [1003\/185100.101396:ERROR:zygote_host_impl_linux.cc(100)] Running as root without &#8211;no-sandbox is not supported.<\/p>\n\n\n\n<pre class=\"wp-block-code alignwide\"><code>To fix the above (Running as root without --no-sandbox is not supported) error, use following steps:\n\nEdit the below file\n\n$ vim \/usr\/bin\/google-chrome\n\ninstead of this line\n\nexec -a \"$0\" \"$HERE\/chrome\" \"$@\"\n\nuse line\n\nexec -a \"$0\" \"$HERE\/chrome\" \"$@\"  --no-sandbox<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Chrome Supported Version : <\/h2>\n\n\n\n<pre class=\"wp-block-code alignwide\"><code>Chrome  117.0.5938.149 - Puppeteer v21.3.7\nChrome  117.0.5938.92 - Puppeteer v21.3.2\nChrome  117.0.5938.62 - Puppeteer v21.3.0\nChrome  116.0.5845.96 - Puppeteer v21.1.0\nChrome  115.0.5790.170 - Puppeteer v21.0.2\nChrome  115.0.5790.102 - Puppeteer v21.0.0\nChrome  115.0.5790.98 - Puppeteer v20.9.0\nChrome  114.0.5735.133 - Puppeteer v20.7.2\nChrome  114.0.5735.90 - Puppeteer v20.6.0\nChrome  113.0.5672.63 - Puppeteer v20.1.0\nChrome  112.0.5615.121 - Puppeteer v20.0.0<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;Puppeteer&#8221; is a special tool made by the Chrome team that lets you use a web browser, like Chrome, without seeing it on your screen. You can tell it what to do using code. People use Puppeteer for things like copying information from websites, filling out online forms automatically, making PDF files, and lots of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":835,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-566","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-automation-tools"],"_links":{"self":[{"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=\/wp\/v2\/posts\/566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=566"}],"version-history":[{"count":0,"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=\/wp\/v2\/posts\/566\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=\/"}],"wp:attachment":[{"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.vavencloud.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}