Apache Cordova, formerly known as PhoneGap, is a robust, open-source mobile development framework enabling developers to create applications for multiple mobile platforms, such as iOS, Android, and Windows, using web technologies like CSS3, HTML5, and JavaScript. By leveraging Cordova’s capabilities, developers can expedite the creation of hybrid mobile applications, significantly reducing development time compared to native apps.
This guide will take you through the process of installing Cordova on Ubuntu 18.04 LTS (Bionic Beaver).
Requirements
- An Ubuntu 18.04 server or system.
- A non-root user with sudo privileges.
Install Node.js
Before proceeding with Cordova’s installation, Node.js needs to be installed. The default Ubuntu 18.04 repository does not provide the latest Node.js version, so you’ll need to add an appropriate PPA to your system.
Execute the following commands to achieve this:
sudo apt-get install python-software-properties -y curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
Then, install Node.js with:
sudo apt-get install nodejs -y
Install Cordova
Next, Cordova can be installed globally through npm using the command below:
sudo npm install -g cordova
Upon completion, the output should look similar to this:
/usr/bin/cordova -> /usr/lib/node_modules/cordova/bin/cordova + cordova@8.0.0 updated 1 package in 15.829s
Verify the installation and version of Cordova with:
cordova --version
Sample output:
? May Cordova anonymously report usage statistics to improve the tool over time? Yes Thanks for opting into telemetry to help us improve cordova. 8.0.0
Create Your First App
With Cordova installed, you can now create your first app, like “TestApp”. Use this command:
cordova create TestApp
This will produce the following output:
Creating a new cordova project.
Then, navigate to the app directory and add a mobile platform for development:
cd TestApp cordova platform add android
Expected output might be:
Using cordova-fetch for cordova-android@~7.0.0 Adding android project... Creating Cordova project for the Android platform: Path: platforms/android Package: io.cordova.hellocordova Name: HelloCordova Activity: MainActivity Android target: android-26 Subproject Path: CordovaLib Subproject Path: app Android project created with cordova-android@7.0.0 Android Studio project detected Discovered plugin "cordova-plugin-whitelist" in config.xml. Adding it to the project Installing "cordova-plugin-whitelist" for android This plugin is only applicable for versions of cordova-android greater than 4.0. If you have a previous platform version, you do *not* need this plugin since the whitelist will be built in. Adding cordova-plugin-whitelist to package.json Saved plugin info for "cordova-plugin-whitelist" to config.xml --save flag or autosave detected Saving android@~7.0.0 into config.xml file ...
To see installed and available platforms for your Cordova application, use the following command:
cordova platform -ls
Typical output:
Installed platforms: android 7.0.0 Available platforms: browser ~5.0.1 ios ~4.5.4 osx ~4.0.1 windows ~5.0.0 www ^3.12.0
If a platform needs to be removed, execute:
cordova platform remove android
Before building your application, ensure all system requirements are met with:
cordova requirements
With dependencies installed, build the application using:
cordova build android
Links
FAQ
What version of Node.js is supported?
The instructions use Node.js version 8.x, but it’s always best to use the latest stable release of Node.js for security and features.
How do I add more platforms to my Cordova application?
You can add more platforms by using the `cordova platform add ` command, where can be ios, windows, etc.
Can I build applications for iOS on Ubuntu?
While you can develop the code for iOS on Ubuntu, building an iOS application requires macOS due to Appleās licensing terms and tools.
Is there a GUI for Cordova?
Cordova primarily functions through command-line interfaces, but third-party tools such as Visual Studio can provide a GUI for development.
What should I do if I encounter build issues?
Check the official documentation and community forums for troubleshooting steps, as build issues can be due to a variety of reasons like dependency conflicts or missing installations.