Keywords: Ubuntu | PhantomJS | Setup
Abstract: This article provides a detailed step-by-step guide for installing and configuring PhantomJS on Ubuntu systems, focusing on the binary package download and symbolic link creation method, with comparisons to npm installation. It addresses common issues like 'command not found' errors, covering environment setup, path configuration, and version verification to help developers quickly establish a headless browser testing environment.
When setting up PhantomJS on Ubuntu, developers often encounter the 'command not found' error, typically due to incorrect system path configuration. Based on best practices, this article offers a complete solution to ensure smooth operation of PhantomJS.
Installing the PhantomJS Binary Package
First, download the pre-compiled binary package of PhantomJS from the official source. It is recommended to use the wget tool to fetch the latest stable version, such as phantomjs-1.9.7-linux-x86_64.tar.bz2. After downloading, extract it to a system directory like /usr/local/share/ using the tar command, which helps centralize third-party software management.
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.bz2Configuring System Paths
After extraction, the key step is to create symbolic links that point the PhantomJS executable to system paths. This includes linking to /usr/local/share/phantomjs for local reference, and to /usr/local/bin/ and /usr/bin/ for global access. Using the ln -s command avoids duplicating files and maintains consistency during updates.
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/phantomjsVerifying the Installation
Once installed, run the phantomjs -v command to check the version number and confirm successful setup. For example, an output of 1.9.7 indicates that PhantomJS is properly configured. This step is crucial as it ensures subsequent script executions won't fail due to environment issues.
phantomjs -vAlternative Installation Methods
In addition to binary package installation, PhantomJS can be installed via npm (Node.js package manager). Using the command npm install -g phantomjs-prebuilt installs the latest version globally, such as 2.1.1. This method is suitable for systems with Node.js already configured, but may be less stable than the binary package approach.
npm install -g phantomjs-prebuiltIn summary, by properly setting symbolic links and paths, developers can efficiently run PhantomJS on Ubuntu, supporting headless browser testing and automation tasks.