Installing Phonegap on Ubuntu

  1. Bring up a terminal window (Ctrl+Alt+T), and type sudo -s to sign in to a root session.
  2. Do a apt-get update and apt-get upgrade, just because it’s good practice.
  3. Copy the latest link to the node.js Linux package from this link.
  4. Change directory to /tmp: cd /tmp
  5. Type wget <URL of node.js package from Step #3> to download the latest version.
  6. Unzip the tarball by entering tar -zxvf node-*.tar.gz.
  7. Remove the tarball by entering rm node-*.tar.gz.
  8. Move the resulting directory somewhere friendly.  This tutorial will use /usr/lib/nodejs as the destination.  (If all goes well, you should see “bin” and “node_module” directories [amongst others] in the /usr/lib/nodejs path.)  To do this, type mv node-* /usr/lib/nodejs.
  9. Change directory to /usr/bin: cd /usr/bin
  10. Create a symlink for the node binary: ln -s /usr/lib/nodejs/bin/node ./node
  11. Create a symlink for the npm binary: ln -s /usr/lib/nodejs/lib/node_modules/npm/cli.js ./npm
  12. Now, we need to download git and some libraries that phonegap is dependent upon.  If you are running the x86_64 version of 13.10, type apt-get install lib32z1 lib32ncurses5 lib32bz2-1.0 lib32stdc++6 git -y. If you are running the i386 version, type apt-get install libz1 libncurses5 libbz2-1.0 libstdc++6 git -y.
  13. With any luck, we’re ready to start the PhoneGap install.  Type npm install -g phonegap.
  14. Exit the root shell by typing exit.  This is quite important for various permission-based reasons.
  15. Go back to your home directory by typing: cd ~.
  16. Run phonegap.  You should now see the manual page.
  17. Create an empty project by typing phonegap create helloWorld
  18. Navigate into the project’s directory: cd ~/helloWor
  19. Build the program by entering phonegap build android -V

Well, kind of.  As you will see, once you run the build command in PhoneGap, it will ask you to login with an Adobe ID account (hit CTRL-C to break out of this, if you wish).  This is necessary if you want to use PhoneGap’s cloud compilation services – which is probably a good idea if you just want a one-stop solution for producing an app on multiple operating systems.  Accounts to their cloud compilation service range from free (1 private project with unlimited open source projects) to $10/month to expensive, but it is definitely the easiest way to date to mass-produce apps for multiple platforms.  The drawback to this is that all of your code is uploaded to Adobe’s cloud (and Adobe’s authentication servers just recently got hacked)… just food for thought.

If you want to test your PhoneGap in a more secure environment, we can do so by building the packages locally and using the freely available Android emulator for testing.  I recommend this method since it does not require you to register with Adobe in order to run PhoneGap API applications.

After completing steps #1 – 14 above, perform the following steps:

  1. Type sudo apt-get install openjdk-7-jdk ant -y to install OpenJDK and Ant, both required by Android’s SDK.
  2. Go back into /tmp to download the Android SDK: cd /tmp
  3. Type wget <URL of Android SDK found here> to download the Android SDK ZIP.  This is quite large (~450MB for the x86_64 version).
  4. Type unzip adt-bundle-linux-*.zip to unzip the file.
  5. Type rm adt-bundle-linux-*.zip to delete the ZIP.  It is no longer needed.
  6. Now, we need to move the extracted files to a more suitable location.  Type sudo mv adt-bundle-linux-* /usr/lib/android-sdk, and enter in the password for your account if prompted.
  7. We now need to update our PATH environment variable in the bash configuration file, so that PhoneGap can call the Android emulator.  Type nano ~/.bashrc.
  8. Navigate to the end of the file (press the “Page Down” key multiple times until the cursor is at the bottom).  Type export PATH=$PATH:/usr/lib/android-sdk/sdk/tools:/usr/lib/android-sdk/sdk/platform-tools on a new line at the very end of the file, and press CTRL+X.  A dialog at the bottom of the screen will ask you if you want to overwrite the existing .bashrc file.  Type “Y” and hit the ENTER key to confirm.
  9. Type source ~/.bashrc to refresh your bash configuration.
  10. You should now be able to type in android and see the “Android SDK Manager” window.  Make sure that the checkboxes next to the “Tools” folder (at the top of the list) and the “Android 4.2 (API 17)” are checked, then click on the “Install # packages…” button below the list (where # is a number).  We are skipping Android 4.3/API 18 group as PhoneGap does not support it yet (and in fact, breaks the PhoneGap build process).
  11. Read through the EULA for the Android tools, and accept them.  Click “Install”.  Wait for everything to download and install.  This will download about ~400 MB of packages.
  12. Once finished, remain in Android SDK Manager. Click on the “Tools” menu, and select the “Manage AVDs” option.
  13. Click on the “New” button on the right-hand side of the screen, and give the AVD a name.  Select “Nexus S” as the Device, “Android 4.2.2 (API 17)” for the Target (IMPORTANT), and “ARM” as the CPU.  If your system can handle it, I would give the AVD at least 512 MB of RAM under “Memory Options” to speed things along.  Similarly, give the AVD a SD card of at least 64 MB.  Click “OK”.
  14. Select the AVD in the list, and click the “Start…” button on the right-hand side of the screen.  Click “Launch” on the popup window.
  15. If you start to see the emulator pop up, and see an “Android” splash screen, you’re all set.  Close out of the emulator, the Android Virtual Device Manager, and the Android SDK Manager.
  16. Returning to the shell, type cd ~/helloWorld to navigate to your PhoneGap project.
  17. Finally, type phonegap run android -V.  If all went well, you will see the Android emulator pop up after a minute or so, and install your PhoneGap project after a few more minutes of waiting.  Just to note: you may have to unlock the screen and go through Android’s tutorial before you can get to your project the first time.  It is a good idea to keep the emulator running while building new revisions of a PhoneGap project, as it only takes a few seconds to push a new version of the resulting APK to it (as opposed to the minutes it takes to cold-boot).

And that’s all there is to it.

Happy coding!

Advertisements

2 Comments Add yours

  1. After installing, when I type “phonegap create hello” it shows error like:
    /usr/bin/env: node: No such file or directory

    1. Check the node is install or not .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s