如何在Ubuntu和CentOS上安装Chromium?

在Ubuntu和CentOS上安装无头Chromium的逐步指南。

什么是无头Chrome?

无头Chrome就像在没有UI(浏览器)的情况下运行Chrome浏览器,也就是在无头环境下运行,这对于running automated tests非常有用。

那么,无头Chrome给我们带来了什么?

最令人兴奋的事情之一就是能够测试最新和最好的Web平台功能,如ES6模块、服务工作者和流。使用无头Chrome,您可以编写应用程序并使用最新的渲染测试这些应用程序。它还可以解锁其他一些很棒的功能,如网络限速、设备仿真和代码覆盖。

有了无头Chrome,您甚至可以在不编写一行代码的情况下执行测试,这不是很酷吗!

您只需通过命令行执行它们。以下是一些常用的标志。

--screenshot #截图
--print-to-pdf #将webpage to PDF转换为PDF
--dump-dom #转储DOM(文档对象模型)数据

注意 – 这与Chromium browser不同。

在Ubuntu上安装Chromium

步骤1:更新Ubuntu

首先,更新Ubuntu中的软件包。

yaoweibin@yaoweibin:~$ sudo apt-get update
Hit:1 https://download.docker.com/linux/ubuntu bionic InRelease
Hit:2 http://security.ubuntu.com/ubuntu cosmic-security InRelease
Hit:3 http://ppa.launchpad.net/ansible/ansible/ubuntu cosmic InRelease
Hit:5 http://apt.puppetlabs.com bionic InRelease
Ign:6 http://pkg.jenkins.io/debian-stable binary/ InRelease
Hit:7 http://us.archive.ubuntu.com/ubuntu cosmic InRelease
Hit:8 http://ppa.launchpad.net/canonical-chromium-builds/stage/ubuntu cosmic InRelease
Hit:9 http://pkg.jenkins.io/debian-stable binary/ Release
Hit:10 http://us.archive.ubuntu.com/ubuntu cosmic-updates InRelease
Hit:11 http://us.archive.ubuntu.com/ubuntu cosmic-backports InRelease
Hit:4 https://packages.cloud.google.com/apt kubernetes-xenial InRelease
Reading package lists... Done

步骤2:安装依赖项

安装这些安装所需的依赖项。

yaoweibin@yaoweibin:〜$ sudo apt-get install -y libappindicator1 fonts-liberation
读取软件包列表… 完成
正在构建依赖关系树
正在读取状态信息… 完成
fonts-liberation 已经是最新版本(1:1.07.4-7)。
fonts-liberation 设置为手动安装。
以下软件包是自动安装的,现在不再需要:
chromium-codecs-ffmpeg-extra
使用 ‘sudo apt autoremove' 来解除安装它。
将会同时安装下列软件:
libdbusmenu-gtk4 libindicator7
建议安装的软件包:
indicator-application
下列新软件包将被安装:
libappindicator1 libdbusmenu-gtk4 libindicator7
升级了 0 个软件包,新安装了 3 个软件包,要卸载 0 个软件包,有 238 个软件包未被升级。
需要下载 68.2 kB 的归档。
归档大小为 280 kB 将用于安装。
获取:1 http://us.archive.ubuntu.com/ubuntu cosmic/universe amd64 libdbusmenu-gtk4 amd64 16.04.1+18.10.20180917-0ubuntu1 [27.1 kB] 获取:2 http://us.archive.ubuntu.com/ubuntu cosmic/universe amd64 libindicator7 amd64 16.10.0+18.04.20180321.1-0ubuntu1 [21.7 kB] 获取:3 http://us.archive.ubuntu.com/ubuntu cosmic/universe amd64 libappindicator1 amd64 12.10.1+18.04.20180322.1-0ubuntu1 [19.4 kB] 已下载 68.2 kB,耗时 1s (61.1 kB/s)
正在选择未选择的软件包 libdbusmenu-gtk4:amd64。
(正在读取数据库 … 系统当前已安装 318270 个文件和目录。)
正在准备解包 …/libdbusmenu-gtk4_16.04.1+18.10.20180917-0ubuntu1_amd64.deb …
正在解包 libdbusmenu-gtk4:amd64 (16.04.1+18.10.20180917-0ubuntu1) …
正在选择未选择的软件包 libindicator7。
正在准备解包 …/libindicator7_16.10.0+18.04.20180321.1-0ubuntu1_amd64.deb …
正在解包 libindicator7 (16.10.0+18.04.20180321.1-0ubuntu1) …
正在选择未选择的软件包 libappindicator1。
正在准备解包 …/libappindicator1_12.10.1+18.04.20180322.1-0ubuntu1_amd64.deb …
正在解包 libappindicator1 (12.10.1+18.04.20180322.1-0ubuntu1) …
正在设置 libindicator7 (16.10.0+18.04.20180321.1-0ubuntu1) …
正在设置 libdbusmenu-gtk4:amd64 (16.04.1+18.10.20180917-0ubuntu1) …
正在处理用于 libc-bin (2.28-0ubuntu1) 的触发器 …
正在设置 libappindicator1 (12.10.1+18.04.20180322.1-0ubuntu1) …
正在处理用于 libc-bin (2.28-0ubuntu1) 的触发器 …
libappindicator1:此库用于将应用程序菜单导出到面板中
fonts-liberation:此库具有与 Arial、Times 和 Courier 类似的字体度量标准
yaoweibin@yaoweibin:〜$ sudo apt-get install -f
以下将安装额外的软件包:
  at-spi2-core cpp cpp-7 dconf-gsettings-backend dconf-service gcc-7-base glib-networking glib-networking-common
  glib-networking-services gsettings-desktop-schemas libappindicator3-1 libasound2 libasound2-data libatk-bridge2.0-0
  libatspi2.0-0 libauthen-sasl-perl libcairo-gobject2 libcolord2 libdata-dump-perl libdbusmenu-gtk3-4 libdconf1
  libdrm-amdgpu1 libdrm-intel1 libdrm-nouveau2 libdrm-radeon1 libencode-locale-perl libepoxy0 libfile-basedir-perl
  libfile-desktopentry-perl libfile-listing-perl libfile-mimeinfo-perl libfont-afm-perl libfontenc1 libgl1
  libgl1-mesa-dri libgl1-mesa-glx libglapi-mesa libglvnd0 libglx-mesa0 libglx0 libgtk-3-0 libgtk-3-bin libgtk-3-common
  libhtml-form-perl libhtml-format-perl libhtml-parser-perl libhtml-tagset-perl libhtml-tree-perl libhttp-cookies-perl
  libhttp-daemon-perl libhttp-date-perl libhttp-message-perl libhttp-negotiate-perl libice6 libindicator3-7
  libio-html-perl libio-socket-ssl-perl libipc-system-simple-perl libisl19 libjson-glib-1.0-0 libjson-glib-1.0-common
  liblcms2-2 libllvm9 liblwp-mediatypes-perl liblwp-protocol-https-perl libmailtools-perl libmpc3 libnet-dbus-perl
  libnet-http-perl libnet-smtp-ssl-perl libnet-ssleay-perl libnspr4 libnss3 libpciaccess0 libproxy1v5 librest-0.7-0
  libsensors4 libsm6 libsoup-gnome2.4-1 libsoup2.4-1 libtie-ixhash-perl libtimedate-perl libtry-tiny-perl liburi-perl
  libwayland-client0 libwayland-cursor0 libwayland-egl1 libwww-perl libwww-robotrules-perl libx11-protocol-perl
  libx11-xcb1 libxaw7 libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0 libxcb-present0 libxcb-shape0 libxcb-sync1 libxft2
  libxkbcommon0 libxml-parser-perl libxml-twig-perl libxml-xpathengine-perl libxmu6 libxpm4 libxshmfence1 libxss1 libxt6
  libxtst6 libxv1 libxxf86dga1 libxxf86vm1 perl-openssl-defaults x11-common x11-utils x11-xserver-utils xdg-utils
建议安装以下软件包:
  cpp-doc gcc-7-locales indicator-application libasound2-plugins alsa-utils libdigest-hmac-perl libgssapi-perl colord
  gvfs liblcms2-utils libcrypt-ssleay-perl lm-sensors libauthen-ntlm-perl libunicode-map8-perl libunicode-string-perl
  xml-twig-tools mesa-utils nickle cairo-5c xorg-docs-core
将会安装以下新增软件包:
  at-spi2-core cpp cpp-7 dconf-gsettings-backend dconf-service gcc-7-base glib-networking glib-networking-common
  glib-networking-services gsettings-desktop-schemas libappindicator3-1 libasound2 libasound2-data libatk-bridge2.0-0
  libatspi2.0-0 libauthen-sasl-perl libcairo-gobject2 libcolord2 libdata-dump-perl libdbusmenu-gtk3-4 libdconf1
  libdrm-amdgpu1 libdrm-intel1 libdrm-nouveau2 libdrm-radeon1 libencode-locale-perl libepoxy0 libfile-basedir-perl
  libfile-desktopentry-perl libfile-listing-perl libfile-mimeinfo-perl libfont-afm-perl libfontenc1 libgl1
  libgl1-mesa-dri libgl1-mesa-glx libglapi-mesa libglvnd0 libglx-mesa0 libglx0 libgtk-3-0 libgtk-3-bin libgtk-3-common
  libhtml-form-perl libhtml-format-perl libhtml-parser-perl libhtml-tagset-perl libhtml-tree-perl libhttp-cookies-perl
  libhttp-daemon-perl libhttp-date-perl libhttp-message-perl libhttp-negotiate-perl libice6 libindicator3-7
  libio-html-perl libio-socket-ssl-perl libipc-system-simple-perl libisl19 libjson-glib-1.0-0 libjson-glib-1.0-common
  liblcms2-2 libllvm9 liblwp-mediatypes-perl liblwp-protocol-https-perl libmailtools-perl libmpc3 libnet-dbus-perl
  libnet-http-perl libnet-smtp-ssl-perl libnet-ssleay-perl libnspr4 libnss3 libpciaccess0 libproxy1v5 librest-0.7-0
  libsensors4 libsm6 libsoup-gnome2.4-1 libsoup2.4-1 libtie-ixhash-perl libtimedate-perl libtry-tiny-perl liburi-perl
  libwayland-client0 libwayland-cursor0 libwayland-egl1 libwww-perl libwww-robotrules-perl libx11-protocol-perl
  libx11-xcb1 libxaw7 libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0 libxcb-present0 libxcb-shape0 libxcb-sync1 libxft2
  libxkbcommon0 libxml-parser-perl libxml-twig-perl libxml-xpathengine-perl libxmu6 libxpm4 libxshmfence1 libxss1 libxt6
  libxtst6 libxv1 libxxf86dga1 libxxf86vm1 perl-openssl-defaults x11-common x11-utils x11-xserver-utils xdg-utils
升级了0个软件包,新安装了117个软件包,要升级0个软件包,要删除58个软件包。
有1个软件包未完全安装或删除。
需要下载40.7 MB的归档文件。
此操作后,将使用369 MB的额外磁盘空间。
是否要继续进行?[Y/n] Y
在4秒内获取了40.7 MB(9470 kB/s)
正在从软件包中提取模板:100%

步骤3:下载Chrome

下载用于Ubuntu系统的Google Chrome稳定版本软件包。

yaoweibin@yaoweibin:~$ wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
--2020-01-15 13:58:41-- https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
正在解析主机 dl.google.com (dl.google.com)... 172.217.163.78, 2404:6800:4007:80c::200e
正在连接 dl.google.com (dl.google.com)|172.217.163.78|:443... 已连接。
HTTP 请求已发送,正在等待回应... 200 OK
长度:62204098 (59M) [application/x-debian-package]
正在保存至: “google-chrome-stable_current_amd64.deb”

google-chrome-stable_current_amd64.deb 100%[=================================================================>] 59.32M 11.1MB/s 共 29s

2020-01-15 13:59:10 (2.04 MB/s) - 已保存 “google-chrome-stable_current_amd64.deb” [62204098/62204098]

步骤4:安装Chrome

安装稳定版本软件包。

yaoweibin@yaoweibin:~$ sudo dpkg -i google-chrome*.deb
正在选择未选择的包 google-chrome-stable (79.0.3945.117-1)。
正在读取软件包列表... Done
正在解压缩 google-chrome-stable (79.0.3945.117-1) ...
正在设置 google-chrome-stable (79.0.3945.117-1) ...
update-alternatives: 使用 /usr/bin/google-chrome-stable 为 /usr/bin/x-www-browser (x-www-browser) 提供 (/usr/bin/x-www-browser 链接是自动模式)
update-alternatives: 使用 /usr/bin/google-chrome-stable 为 /usr/bin/gnome-www-browser (gnome-www-browser) 提供 (/usr/bin/gnome-www-browser 链接是自动模式)
update-alternatives: 使用 /usr/bin/google-chrome-stable 为 /usr/bin/google-chrome (google-chrome) 提供 (/usr/bin/google-chrome 链接是自动模式)
正在处理用于 gnome-menus (3.13.3-11ubuntu2) 的触发器...
正在处理用于 desktop-file-utils (0.23-3ubuntu2) 的触发器...
正在处理用于 mime-support (3.60ubuntu1) 的触发器...
正在处理用于 man-db (2.8.4-2) 的触发器...

步骤5:检查Chrome版本

为了确认安装已成功完成,请检查Google Chrome版本。

yaoweibin@yaoweibin:~$ google-chrome-stable -version
Google Chrome 79.0.3945.117

可选:运行无界面Chrome

让我给你展示一个在无界面模式下运行Chrome的例子。

我将在无界面模式下运行Chrome,并将https://gf.dev/的所有内容保存为PDF文件。

yaoweibin@yaoweibin:~$ google-chrome-stable --headless --disable-gpu --print-to-pdf https://gf.dev/
[0115/140930.772108:INFO:headless_shell.cc(620)] Written to file output.pdf.

你可以看到一个名为output.pdf的文件

yaoweibin@yaoweibin:~$ ls -l out*
-rw------- 1 yaoweibin yaoweibin 80799 Jan 15 14:09 output.pdf
yaoweibin@yaoweibin:~$ 

接下来,我将向你展示如何在CentOS系统中进行相同的设置。

在CentOS上安装Chromium

步骤1:更新CentOS

使用以下命令更新CentOS系统。

[yaoweibin@yaoweibin ~]$ sudo yum update

步骤2:创建Chrome仓库

将Google Chrome仓库添加到系统中。在/etc/yum.repos.d/目录下创建一个名为google-chrome.repo的文件,并添加以下详细信息。

[yaoweibin@yaoweibin ~]$ sudo gedit /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

步骤3:安装Chrome

运行以下yum命令在CentOS系统上安装Google Chrome。

[yaoweibin@yaoweibin ~]$ sudo yum install google-chrome-stable
上次元数据到期检查:0:04:04前,于2020年1月22日星期三下午01:06:24 EST。
解析依赖关系。
================================================================================
包 架构 版本 存储库 大小
================================================================================
正在安装:
google-chrome-stable x86_64 79.0.3945.130-1 google-chrome 60 M
正在安装依赖项:
libXScrnSaver x86_64 1.2.3-1.el8 AppStream 31 k
libappindicator-gtk3 x86_64 12.10.0-19.el8 AppStream 43 k
libdbusmenu x86_64 16.04.0-12.el8 AppStream 140 k
libdbusmenu-gtk3 x86_64 16.04.0-12.el8 AppStream 41 k
libindicator-gtk3 x86_64 12.10.1-14.el8 AppStream 70 k
liberation-fonts noarch 1:2.00.3-4.el8 BaseOS 19 k
liberation-serif-fonts noarch 1:2.00.3-4.el8 BaseOS 607 k

事务总结
================================================================================
安装 8 个软件包

总下载大小:60 M
已安装大小:213 M
是否确定[y/N]:y
正在下载软件包:
(1/8): libXScrnSaver-1.2.3-1.el8.x86_64.rpm 146 kB/s | 31 kB 00:00
(2/8): libappindicator-gtk3-12.10.0-19.el8.x86_6 171 kB/s | 43 kB 00:00
(3/8): libdbusmenu-gtk3-16.04.0-12.el8.x86_64.rpm 691 kB/s | 41 kB 00:00
(4/8): libindicator-gtk3-12.10.1-14.el8.x86_64.rp 558 kB/s | 70 kB 00:00
(5/8): liberation-fonts-2.00.3-4.el8.noarch.rpm 74 kB/s | 19 kB 00:00
(6/8): liberation-serif-fonts-2.00.3-4.el8.noarch 1.3 MB/s | 607 kB 00:00
(7/8): libdbusmenu-16.04.0-12.el8.x86_64.rpm 121 kB/s | 140 kB 00:01
(8/8): google-chrome-stable-79.0.3945.130-1.x86_6 8.4 MB/s | 60 MB 00:07
——————————————————————————–
总计 6.5 MB/s | 60 MB 00:09
警告:/var/cache/dnf/google-chrome-eb0d6f10ccbdafba/packages/google-chrome-stable-79.0.3945.130-1.x86_64.rpm:头 V4 DSA/SHA1 签名,密钥 ID 7fac5991:NOKEY
google-chrome 49 kB/s | 10 kB 00:00
正在导入 GPG 密钥 0x7FAC5991:
用户标识 : “Google, Inc. Linux Package Signing Key ”
指纹 : 4CCA 1EAF 950C EE4A B839 76DC A040 830F 7FAC 5991
来源 : https://dl-ssl.google.com/linux/linux_signing_key.pub
是否确定[y/N]:y
密钥导入成功
正在导入 GPG 密钥 0xD38B4796:
用户标识 : “Google Inc. (Linux Packages Signing Authority) ”
指纹 : EB4C 1BFD 4F04 2F6D DDCC EC91 7721 F63B D38B 4796
来源 : https://dl-ssl.google.com/linux/linux_signing_key.pub
是否确定[y/N]:y
密钥导入成功
正在运行事务检查
事务检查成功。
正在运行事务测试
事务测试成功。
正在运行事务
准备中 : 1/1
正在安装 : libdbusmenu-16.04.0-12.el8.x86_64 1/8
正在安装 : libdbusmenu-gtk3-16.04.0-12.el8.x86_64 2/8
正在安装 : liberation-serif-fonts-1:2.00.3-4.el8.noar 3/8
正在安装 : liberation-fonts-1:2.00.3-4.el8.noarch 4/8
正在安装 : libindicator-gtk3-12.10.1-14.el8.x86_64 5/8
正在安装 : libappindicator-gtk3-12.10.0-19.el8.x86_64 6/8
正在安装 : libXScrnSaver-1.2.3-1.el8.x86_64 7/8
正在运行脚本let: google-chrome-stable-79.0.3945.130-1.x86_ 8/8
正在安装 : google-chrome-stable-79.0.3945.130-1.x86_ 8/8
正在运行脚本let: google-chrome-stable-79.0.3945.130-1.x86_ 8/8
重定向到 /bin/systemctl start atd.service

正在验证 : libXScrnSaver-1.2.3-1.el8.x86_64 1/8
正在验证 : libappindicator-gtk3-12.10.0-19.el8.x86_64 2/8
正在验证 : libdbusmenu-16.04.0-12.el8.x86_64 3/8
正在验证 : libdbusmenu-gtk3-16.04.0-12.el8.x86_64 4/8
正在验证 : libindicator-gtk3-12.10.1-14.el8.x86_64 5/8
正在验证 : liberation-fonts-1:2.00.3-4.el8.noarch 6/8
正在验证 : liberation-serif-fonts-1:2.00.3-4.el8.noarc 7/8
正在验证 : google-chrome-stable-79.0.3945.130-1.x86_ 8/8

已安装:
google-chrome-stable-79.0.3945.130-1.x86_64
libXScrnSaver-1.2.3-1.el8.x86_64
libappindicator-gtk3-12.10.0-19.el8.x86_64
libdbusmenu-16.04.0-12.el8.x86_64
libdbusmenu-gtk3-16.04.0-12.el8.x86_64
libindicator-gtk3-12.10.1-14.el8.x86_64
liberation-fonts-1:2.00.3-4.el8.noarch
liberation-serif-fonts-1:2.00.3-4.el8.noarch

完成!

第四步:检查Chrome版本

为了确认安装已经成功完成,请检查Google Chrome的版本。

[yaoweibin@yaoweibin ~]$ google-chrome -version
Google Chrome 79.0.3945.130

可选步骤:运行Chrome Headless

让我给你展示在CentOS上运行无头模式的Chrome的示例。

我将以截取屏幕截图的示例为例。下面的命令将以无头模式运行Chrome,并将https://www.chromestatus.com/的所有内容保存为pdf文件。

[yaoweibin@yaoweibin ~]$ google-chrome-stable --headless --disable-gpu --print-to-pdf https://www.chromestatus.com/
[0122/131602.193881:INFO:headless_shell.cc(620)] Written to file output.pdf.

生成了output.pdf文件,其中包含https://www.chromestatus.com/的所有内容。

[yaoweibin@yaoweibin ~]$ ls -l
total 61208
drwxr-xr-x. 2 yaoweibin yaoweibin 6 Sep 25 11:49 Desktop
drwxr-xr-x. 2 yaoweibin yaoweibin 6 Sep 25 11:49 Documents
drwxr-xr-x. 2 yaoweibin yaoweibin 6 Sep 25 11:49 Downloads
-rw-rw-r--. 1 yaoweibin yaoweibin 180 Jan 22 13:05 google-chrome.repo
-rw-------. 1 yaoweibin yaoweibin 146776 Jan 22 13:16 output.pdf

关于如何在Ubuntu和CentOS上安装Chrome并在无头模式下运行的内容就是这些。

类似文章