简体   繁体   English

如何在Ubuntu上设置和运行PhantomJS?

[英]How can I setup & run PhantomJS on Ubuntu?

I set up PhantomJS and recorded it to video: https://www.dailymotion.com/video/xnizmh_1_webcam 我设置了PhantomJS并将其录制到视频: https//www.dailymotion.com/video/xnizmh_1_webcam

Build instructions: http://phantomjs.org/build.html 构建说明: http//phantomjs.org/build.html

Is there anything wrong in my setup? 我的设置有什么问题吗?

After I set it up I read the quick start tutorial and tried to write this code 在我设置之后,我阅读了快速入门教程并尝试编写此代码

phantomjs hello.js 

It gives me "command not found" error. 它给了我“命令未找到”错误。 How can I solve this problem? 我怎么解决这个问题?

Guidouil's answer put me on the right track. Guidouil的回答让我走上正轨。 I had to add one additional symlink to /usr/bin/, and I did direct symlinks for all 3 - see below. 我不得不在/ usr / bin /中添加一个额外的符号链接,我为所有3添加了直接符号链接 - 见下文。

I'm installing on Ubuntu server Natty Narwhal. 我正在Ubuntu服务器Natty Narwhal上安装。

This is exactly what I did. 这正是我所做的。

cd /usr/local/share
sudo wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-x86_64.tar.bz2
sudo tar xjf phantomjs-1.9.7-linux-x86_64.tar.bz2
sudo ln -s /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/local/share/phantomjs
sudo ln -s /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs
sudo ln -s /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/bin/phantomjs

And finally when I do 最后,当我这样做

phantomjs -v

I get 1.9.7 我得到1.9.7

If anyone sees any problems with what I've done, please let me know. 如果有人发现我所做的任何问题,请告诉我。

PhantomJS is on npm . PhantomJS在npm上 You can run this command to install it globally: 您可以运行此命令以全局安装它:

npm install -g phantomjs-prebuilt  

phantomjs -v should return 2.1.1 phantomjs -v应返回2.1.1

download from phantomjs website the prebuilt package : http://phantomjs.org/download.html then open a terminal and go to the Downloads folder 从phantomjs网站下载预构建的软件包: http ://phantomjs.org/download.html然后打开终端并转到Downloads文件夹

sudo mv phantomjs-1.8.1-linux-x86_64.tar.bz2 /usr/local/share/.
cd /usr/local/share/
sudo tar xjf phantomjs-1.8.1-linux-x86_64.tar.bz2
sudo ln -s /usr/local/share/phantomjs-1.8.1-linux-x86_64 /usr/local/share/phantomjs
sudo ln -s /usr/local/share/phantomjs/bin/phantomjs /usr/local/bin/phantomjs

then to check install phantomjs -v should return 1.8.1 然后检查安装phantomjs -v应该返回1.8.1

从包管理器安装:

sudo apt-get install phantomjs

Here are the build steps I used (note these instructions are for version 1.3. See comments to this answer for the installation instructions of the latest PhantomJS): 以下是我使用的构建步骤 (请注意这些说明适用于1.3版。有关最新PhantomJS的安装说明,请参阅本答案的注释):

sudo apt-get update
sudo apt-get install git-core
sudo apt-get install build-essential
sudo apt-get install libqt4-dev libqtwebkit-dev qt4-qmake
git clone git://github.com/ariya/phantomjs.git && cd phantomjs
git checkout 1.3
qmake-qt4 && make

Now install Xvfb 现在安装Xvfb

sudo apt-get install xvfb xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic

Launch Xvfb: 启动Xvfb:
Xvfb :23 -screen 0 1024x768x24 &

Now run phantom: 现在运行幻像:
DISPLAY=:23 ./phantomjs hello.js

For PhantomJS version above 1.5, consider this (verbatim copy of the build instructions on the phantom website): 对于1.5以上的PhantomJS版本,请考虑这一点(幻像网站上的构建说明的逐字副本):

For Ubuntu Linux (tested on a barebone install of Ubuntu 10.04 Lucid Lynx and Ubuntu 11.04 Natty Narwhal): 对于Ubuntu Linux(在Ubuntu 10.04 Lucid Lynx和Ubuntu 11.04 Natty Narwhal的裸机安装上测试):

 sudo apt-get install build-essential chrpath git-core libssl-dev libfontconfig1-dev git clone git://github.com/ariya/phantomjs.git cd phantomjs git checkout 1.7 ./build.sh 

For Ubuntu you can use the prebuilt versions downloadable from the PhantomJS site . 对于Ubuntu,您可以使用可从PhantomJS站点下载的预构建版本。

If you have some serious time on your hands you can also build it yourself . 如果你手上有一些重要的时间,你也可以自己构建它 (This is exactly the procedure from Nikhil's answer). (这正是Nikhil回答的程序)。

The guys over at PhantomJS recommend using the binaries to save time: PhantomJS上的人建议使用二进制文件来节省时间:

Warning: Compiling PhantomJS from source takes a long time, mainly due to thousands of files in the WebKit module. 警告:从源代码编译PhantomJS需要很长时间,主要是因为WebKit模块中有数千个文件。 With 4 parallel compile jobs on a modern machine, the entire process takes roughly 30 minutes. 在现代机器上进行4个并行编译作业,整个过程大约需要30分钟。 It is highly recommended to download and install the ready-made binary package if it is available. 强烈建议下载并安装现成的二进制包(如果可用)。

With a modern machine they mean > 4 cores, > 8gb mem I think. 使用现代机器,他们意味着> 4核,> 8gb mem我想。 I tried it on a micro AWS instance and gave up after 2 hours. 我在一个微型AWS实例上尝试过,并在2小时后放弃了。

In short: install the prebuilt packages from the PhantomJS site per their instructions. 简而言之:按照他们的说明从PhantomJS站点安装预构建的软件包。

in my vagrant bootstrap: 在我的流浪汉引导:

apt-get install -y build-essential chrpath git-core libssl-dev libfontconfig1-dev
git clone git://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 1.9
echo y | ./build.sh
ln -s /home/vagrant/phantomjs/bin/phantomjs /usr/local/bin/phantomjs
cd ..

Personaly I prefer using npm (see Arnel Bucio answer ) Personaly我更喜欢使用npm (参见Arnel Bucio的回答

sudo npm install -g phantomjs

but! 但! I noticed that some of npm module still can't see it as global executable. 我注意到一些npm模块仍然无法将其视为全局可执行文件。

在此输入图像描述

so! 所以!

  • Create new /usr/share/phantomjs/ directory link 创建新的/usr/share/phantomjs/目录链接

     cd /usr/share sudo ln -s ../lib/node_modules/phantomjs/lib/phantom phantomjs 
  • Remove old /usr/bin/phantomjs executable link and create the new one 删除旧的/usr/bin/phantomjs可执行链接并创建新链接

     cd /usr/bin sudo mv phantomjs phantomjs.old sudo ln -s ../share/phantomjs . 

I have done with this. 我已经完成了这件事。

sudo apt-get update
sudo apt-get install build-essential chrpath git-core libssl-dev libfontconfig1-dev
git clone git://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 1.9
./build.sh

See link Installation guide is in ... 请参阅链接安装指南在......

https://gist.github.com/julionc/7476620 https://gist.github.com/julionc/7476620

And run in terminal with this command 并使用此命令在终端中运行

phantomjs --webdriver=4444 phantomjs --webdriver = 4444

From the official site: phantomjs site 来自官方网站: phantomjs网站

sudo apt-get install build-essential chrpath git-core libssl-dev libfontconfig1-dev
git clone git://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 1.8
./build.sh

For Ubuntu, download the suitable file from http://phantomjs.org/download.html . 对于Ubuntu,请从http://phantomjs.org/download.html下载合适的文件。 CD to the downloaded folder. CD到下载的文件夹。 Then: 然后:

sudo tar xvf phantomjs-1.9.0-linux-x86_64.tar.bz2
sudo mv phantomjs-1.9.0-linux-x86_64 /usr/local/share/phantomjs
sudo ln -s /usr/local/share/phantomjs/bin/phantomjs /usr/bin/phantomjs

Make sure to replace the file name in these commands with the file you have downloaded. 确保使用您下载的文件替换这些命令中的文件名。

Be aware this is definitely one way to do it: 请注意,这绝对是一种方法:

$ sudo apt-get install phantomjs
$ phantomjs -v
1.6.0

Sadly, it installs 1.6 and not the latest one, but this works for my purposes. 可悲的是,它安装了1.6而不是最新版本,但这适用于我的目的。

我知道这个太旧了,但是,就我个人现在从谷歌那里得到这个问题,你可以通过输入apt-get install phantomjs安装它

Or the latest - 32bit version Linux 或者最新的 - 32位版Linux

sudo wget http://phantomjs.googlecode.com/files/phantomjs-1.9.2-linux-i686.tar.bz2

sudo ln -s /usr/local/share/phantomjs-1.9.2-linux-i686/bin/phantomjs /usr/local/share/phantomjs

sudo ln -s /usr/local/share/phantomjs-1.9.2-linux-i686/bin/phantomjs /usr/local/bin/phantomjs

sudo ln -s /usr/local/share/phantomjs-1.9.2-linux-i686/bin/phantomjs /usr/bin/phantomjs

Installation and Calling Phantomjs 安装和调用Phantomjs

Follow the steps doesn't work, but cloned from others built. 按照步骤不起作用,但克隆其他人建立。 (ver2.0) (ver2.0)

Bellow the installation procedure by Julio Napurí https://gist.github.com/julionc 由胡里奥·纳普里(JulioNapurí)编写安装程序, https: //gist.github.com/julionc

Version: 1.9.8 版本:1.9.8

Platform: x86_64 平台:x86_64

First, install or update to the latest system software. 首先,安装或更新最新的系统软件。

sudo apt-get update
sudo apt-get install build-essential chrpath libssl-dev libxft-dev

Install these packages needed by PhantomJS to work correctly. 安装PhantomJS所需的这些软件包才能正常工作。

sudo apt-get install libfreetype6 libfreetype6-dev
sudo apt-get install libfontconfig1 libfontconfig1-dev

Get it from the PhantomJS website. 从PhantomJS网站获取。

cd ~
export PHANTOM_JS="phantomjs-1.9.8-linux-x86_64"
wget https://bitbucket.org/ariya/phantomjs/downloads/$PHANTOM_JS.tar.bz2
sudo tar xvjf $PHANTOM_JS.tar.bz2

Once downloaded, move Phantomjs folder to /usr/local/share/ and create a symlink: 下载后,将Phantomjs文件夹移动到/ usr / local / share /并创建符号链接:

sudo mv $PHANTOM_JS /usr/local/share
sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin

Now, It should have PhantomJS properly on your system. 现在,它应该在您的系统上正确使用PhantomJS。

phantomjs --version

I have found this simpler way - Phantom dependencies + Npm 我发现这种更简单的方式 - 幻影依赖+ Npm

sudo apt-get update
sudo apt-get install build-essential chrpath libssl-dev libxft-dev
sudo apt-get install libfreetype6 libfreetype6-dev
sudo apt-get install libfontconfig1 libfontconfig1-dev

and npm 和npm

[sudo] npm install -g phantomjs

Done. 完成。

On linux to run hello.js don't forget to add the path of hello.js: 在linux上运行hello.js时不要忘记添加hello.js的路径:

phantomjs YourPathToPhantomjsFolder/examples/hello.js phantomjs YourPathToPhantomjsFolder / examples / hello.js

如果你想轻松使用phantomjs,可以在phantomjscloud.com上使用它。你可以通过http请求获得结果。

On Ubuntu for Windows, I found neither apt-get nor npm versions worked for me. 在Ubuntu for Windows上,我发现apt-getnpm版本apt-get没有为我工作。 What worked was the script from this comment . 这个评论的脚本是有用的

For ease of use, I pasted the whole thing into a script file called install_phantomjs.sh , made it executable ( chmod u+x install_phantomjs.sh ), and then ran it ( ./install_phantomjs.sh ) 为了便于使用,我将整个内容粘贴到名为install_phantomjs.sh的脚本文件中,使其成为可执行文件( chmod u+x install_phantomjs.sh ),然后运行它( ./install_phantomjs.sh

This is how I place a specific version of phantomjs in /usr/local/bin on my docker containers. 这就是我在我的docker容器上将特定版本的phantomjs放在/ usr / local / bin中的方法。

curl -Ls https://github.com/Medium/phantomjs/releases/download/v1.9.19/phantomjs-1.9.8-linux-x86_64.tar.bz2 \
    | tar jxvf - --strip-components=2 -C /usr/local/bin/ ./phantomjs-1.9.8-linux-x86_64/bin/phantomjs

or with out ./ depending on OS. 或者用./取决于操作系统。

curl -Ls https://github.com/Medium/phantomjs/releases/download/v1.9.19/phantomjs-1.9.8-linux-x86_64.tar.bz2 \
    | tar jxvf - --strip-components=2 -C /usr/local/bin/ phantomjs-1.9.8-linux-x86_64/bin/phantomjs

You can get up and running without sudo or npm . 你可以在没有sudonpm情况下启动和运行。 Simply download , extract , and add to path . 只需下载 ,解压缩添加到路径即可

This has the added advantage of easy backup if you are in the habit of backing up your entire home folder which I highly recommend. 如果您习惯备份我强烈建议的整个主文件夹,这还具有易于备份的额外优势。 This also works with any version of Linux. 这也适用于任何版本的Linux。

➤  cd ~
➤  wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
➤  mkdir phantomjs
➤  tar xjf phantomjs-2.1.1-linux-x86_64.tar.bz2 -C phantomjs
➤  echo 'export PATH="$PATH:$HOME/phantomjs/bin"' >> .profile
➤  source .profile
➤  phantomjs -v
2.1.1

The disadvantages are: 缺点是:

  1. You will need to manually upgrade 您需要手动升级
  2. Other users will not have access to this. 其他用户无权访问此内容。

You could use a very simple shell script for installing/upgrading 您可以使用非常简单的shell脚本进行安装/升级

#!/bin/sh
# install_phantomjs.sh $VERSION

$VERSION = $1
printf "Downloading PhantomJS $VERSION...\n"
wget "https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-$VERSION-linux-x86_64.tar.bz2"

printf "Extracting PhantomJS $VERSION to ~/phantomjs...\n"
mkdir ~/phantomjs
tar xjf phantomjs-$VERSION-linux-x86_64.tar.bz2 -C ~/phantomjs

printf "Done! Make sure $HOME/phantomjs/bin is in your path.\n"

Or in a Dockerfile 或者在Dockerfile中

# Download and setup PhantomJS
ENV PHANTOMJS_VERSION 2.1.1
RUN curl -fSL "https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2" -o /tmp/phantomjs.tar.bz2 && \
  mkdir ~/phantomjs && \
  tar xjf /tmp/phantomjs.tar.bz2 -C ~/phantomjs && \
  rm /tmp/phantomjs.tar.bz2
ENV PATH /home/$USERNAME/phantomjs/bin:$PATH

Here is what I did on my ubuntu 16.04 machine 这是我在ubuntu 16.04机器上所做的

sudo apt-get update
sudo wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
sudo mv path/where/you/downloaded/phantomjs  /usr/bin

and finally when I do 最后当我这样做

phantomjs -v

I get 2.1.1 我得到2.1.1

After going through every answer of this thread. 经过这个帖子的每个答案。 I think this is the best solution for installing and running phantomjs in ubuntu. 我认为这是在ubuntu中安装和运行phantomjs的最佳解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM