Downloading and installing
When working with older versions of PhoneGap, we have to make a lot of detailed settings of the environment in order to run the application. However, with the newer versions, starting from 5.0.0, this procedure becomes easier. Before installing PhoneGap, we need to install Node.js, because it is easier to install PhoneGap CLI as a ready-to-use NPM package without compiling it from source codes. And NPM is a utility of Node.js.
Tip
Node.js is a platform built on Chrome's JavaScript runtime. It was built as a tool for fast and scalable network applications. The main feature of the framework is an event-driven, non-blocking I/O model. For now, it is mainly used on server side in the same way as PHP, Ruby, or others. However, it is very popular and spreading fast nowadays.
There are several ways to install Node.js, but I will describe only two of them.
Installing Node.js on Mac
We will see how to install Node.js from the official website and with Homebrew.
Installing Node.js from the official website
To install Node.js, you can download a pre-compiled binary package, which makes for a nice and easy installation. Follow these steps:
- Head over to http://nodejs.org/ and click on the INSTALL button to download the latest package:
- Install the package by following along. You will then be directed to install Node.js and NPM (Node Package Manager), which facilitates installs of additional packages for Node.js:
- At the end of the install, you will be prompted to make sure that
/usr/local/bin
is in your path. Double-check that you have it by running in the terminal using this command:$ echo $PATH
If not, add it in either
.bash_profile
or.bashrc
in your home directory. - After the installation, check whether it is OK by entering the following command in the command line node, which will open a Node.js JavaScript session:
$ node > console.log('PhoneGap by Example'); PhoneGap by Example undefined
- To exit the Node.js session, just hit control + c twice. And we are done with the first method of Node.js installation.
Installing Node.js with Homebrew
Another good way to install Node.js is using Homebrew.
Homebrew (http://github.com/mxcl/homebrew) is the package manager that Apple forgot. Written in Ruby, it allows you to quickly and easily compile software on your Mac.
To install Homebrew (http://brew.sh/), follow these steps:
- Run the following command on the console:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Tip
The script explains what it will do and then pauses before it does it. It might require you to execute
sudo
and enter your root password. You should wait for a while to download and install all the components. - Once the installation is complete, you will receive a Successful Installation message:
==> Installation successful! ==> Next steps Run `brew doctor` before you install anything Run `brew help` to get started
- Once Homebrew is installed, you can go ahead and install Node.js:
brew install node
It might require root access from you as well. And that is it. Node.js is installed now. It is pretty easy, right?
Installing Node.js on Windows
- Download the installer from https://nodejs.org/download/.
- Run the installer.
- Follow the steps in the installer. One default option is to install NPM and another is to add Node.js to our path:
- Test Node.js. Just open the Windows command prompt and type
node –v
. This should print a version number. - Test NPM. Type
npm -v
in the terminal. This should print NPM's version number.
Installing Node.js on Linux
To install Node.js on Linux, we should be familiar with the terminal as well. First of all, we need to install dependencies. Follow these steps:
- Installing Ruby and GCC:
- For Ubuntu or Debian:
sudo apt-get install build-essential curl git m4 ruby texinfo libbz2-dev libcurl4-openssl-dev libexpat-dev libncurses-dev zlib1g-dev
- For Fedora:
sudo yum groupinstall 'Development Tools' && sudo yum install curl git m4 ruby texinfo bzip2-devel curl-devel expat-devel ncurses-devel zlib-devel
- For Ubuntu or Debian:
- Installing Homebrew:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/linuxbrew/go/install)"
After that, we need to add the following three lines to .bashrc
or .zshrc
:
export PATH="$HOME/.linuxbrew/bin:$PATH" export MANPATH="$HOME/.linuxbrew/share/man:$MANPATH" export INFOPATH="$HOME/.linuxbrew/share/info:$INFOPATH"
So, all the prerequisites are done, and we can install Node.js now. There are only two steps left to follow:
- Open the terminal and type
brew install node
. - Wait until Homebrew finishes installation.
Now, we can test Node.js and NPM by running node -v
and npm -v
in the terminal accordingly.
Tip
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.