UbuntuUbuntu Based

How To Install Apache Cordova on Ubuntu 24.04 LTS

Install Apache Cordova on Ubuntu 24.04

Apache Cordova is a powerful open-source mobile application development framework that allows developers to create cross-platform mobile apps using standard web technologies such as HTML, CSS, and JavaScript. By leveraging Cordova, developers can build applications that run on multiple mobile platforms while maintaining a single codebase, significantly reducing development time and costs.

Ubuntu 24.04 LTS, the latest long-term support release of the popular Linux distribution, provides a robust and stable environment for developing Cordova applications. Its extensive software repositories, regular updates, and strong community support make it an excellent choice for mobile app development.

In this comprehensive guide, we’ll walk you through the process of installing Apache Cordova on Ubuntu 24.04 LTS, setting up the necessary development environment, and getting started with your first Cordova project. Whether you’re a seasoned developer or just starting with mobile app development, this tutorial will provide you with the knowledge and tools to begin your Cordova journey on Ubuntu.

Prerequisites

Before we dive into the installation process, let’s ensure you have everything you need to successfully set up Apache Cordova on your Ubuntu 24.04 LTS system:

System Requirements

  • A computer running Ubuntu 24.04 LTS (Desktop or Server edition)
  • At least 4GB of RAM (8GB or more recommended for smoother performance)
  • Sufficient free disk space (at least 10GB for development tools and Android SDK)
  • An active internet connection for downloading necessary packages and tools

Required Software and Tools

To install and use Apache Cordova effectively, you’ll need the following software components:

  • Node.js and npm (Node Package Manager)
  • Java Development Kit (JDK)
  • Android Studio and Android SDK
  • Git (version control system)

Preparing Your Ubuntu 24.04 LTS Environment

Before proceeding with the installation, it’s a good practice to update your system. Open a terminal and run the following commands:

sudo apt update
sudo apt upgrade

This will ensure that your system is up-to-date with the latest packages and security updates.

Installing Node.js and npm

Node.js is a crucial component for running Apache Cordova. We’ll install it along with npm, which is used to manage Node.js packages.

Using the Official Node.js Repository

To ensure you have the latest version of Node.js, we’ll use the official Node.js repository. Follow these steps:

  1. Install the required packages to add a new repository:
    sudo apt install curl software-properties-common
  2. Add the NodeSource repository for Node.js 18.x (or the latest LTS version):
    curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

Installing Node.js and npm via Command Line

With the repository added, you can now install Node.js and npm:

sudo apt install nodejs

This command will install both Node.js and npm.

Verifying the Installation

To confirm that Node.js and npm have been installed correctly, check their versions:

node --version
npm --version

You should see the version numbers displayed for both Node.js and npm.

Installing Apache Cordova

Now that we have Node.js and npm set up, we can proceed with installing Apache Cordova.

Using npm to Install Cordova Globally

To install Cordova globally on your system, use the following npm command:

sudo npm install -g cordova

The -g flag ensures that Cordova is installed globally, making it accessible from any directory in your system.

Verifying Cordova Installation

After the installation is complete, verify that Cordova has been installed correctly by checking its version:

cordova --version

This command should display the version number of the installed Cordova.

Understanding Cordova Version Information

The version information not only confirms a successful installation but also helps you keep track of the Cordova version you’re using. This is important for compatibility with plugins and when following documentation or tutorials.

Setting Up Android Development Environment

To develop Android applications with Cordova, you need to set up the Android development environment on your Ubuntu system.

Installing Java Development Kit (JDK)

Android development requires the Java Development Kit. Install OpenJDK using the following command:

sudo apt install default-jdk

Verify the installation by checking the Java version:

java --version

Installing Android Studio and Android SDK

Android Studio provides the most up-to-date Android SDK and tools. Follow these steps to install it:

  1. Download Android Studio from the official website.
  2. Extract the downloaded archive to a suitable location, e.g., /opt:
    sudo tar -xvzf android-studio-*.tar.gz -C /opt
  3. Create a desktop entry for easy access:
    sudo nano /usr/share/applications/android-studio.desktop

    Add the following content:

    [Desktop Entry]
    Version=1.0
    Type=Application
    Name=Android Studio
    Exec=/opt/android-studio/bin/studio.sh
    Icon=/opt/android-studio/bin/studio.png
    Categories=Development;IDE;
    Terminal=false

Launch Android Studio and follow the setup wizard to install the Android SDK and necessary tools.

Configuring Environment Variables

To ensure Cordova can find the Android SDK, you need to set up environment variables. Add the following lines to your ~/.bashrc file:

export ANDROID_SDK_ROOT=$HOME/Android/Sdk
export PATH=$PATH:$ANDROID_SDK_ROOT/tools/bin
export PATH=$PATH:$ANDROID_SDK_ROOT/platform-tools

After saving the file, reload it with:

source ~/.bashrc

Creating Your First Cordova Project

With the environment set up, let’s create your first Cordova project.

Using Cordova CLI to Create a New Project

To create a new Cordova project, use the following command:

cordova create MyFirstApp com.example.myfirstapp MyFirstApp

This command creates a new directory named “MyFirstApp” with the basic structure of a Cordova project.

Understanding the Project Structure

Navigate to your project directory and examine its structure:

cd MyFirstApp
ls -l

You’ll see directories like www (where your web app resides), platforms (for platform-specific code), and plugins (for Cordova plugins).

Adding Platforms to Your Project

Add the Android platform to your project:

cordova platform add android

This command prepares your project for Android development.

Building and Running Your Cordova App

Now that we have a project set up, let’s build and run it.

Building the App for Android

To build your app for Android, run:

cordova build android

This command compiles your web app and creates an Android APK file.

Running the App on an Emulator

To run your app on an Android emulator:

  1. Create an Android Virtual Device (AVD) using Android Studio’s AVD Manager.
  2. Run the following command:
    cordova emulate android

Deploying to a Physical Device

To run your app on a physical Android device:

  1. Enable USB debugging on your Android device.
  2. Connect your device to your computer via USB.
  3. Run:
    cordova run android

Troubleshooting Common Issues

During the installation and development process, you might encounter some issues. Here are solutions to common problems:

Path and Environment Variable Problems

If you encounter “command not found” errors, ensure that your PATH is correctly set in ~/.bashrc. Double-check the paths for Android SDK and platform tools.

Dependency Conflicts

If you face dependency conflicts, try clearing npm cache and reinstalling Cordova:

npm cache clean --force
sudo npm install -g cordova

Build Errors and Solutions

For build errors, check the following:

  • Ensure all required Android SDK components are installed.
  • Verify that your project’s config.xml file is correctly configured.
  • Run cordova requirements to check if all requirements are met for your target platforms.

Best Practices and Tips

To ensure a smooth development experience with Cordova on Ubuntu 24.04 LTS, consider these best practices:

Keeping Cordova and Dependencies Up to Date

Regularly update Cordova and its dependencies:

sudo npm update -g cordova
cordova platform update android

Optimizing Performance on Ubuntu 24.04 LTS

  • Use an SSD for faster build times and emulator performance.
  • Allocate sufficient RAM to Android Studio and emulators.
  • Consider using hardware acceleration for Android emulators.

Security Considerations

  • Keep your Ubuntu system and development tools updated.
  • Use HTTPS for any external resources in your Cordova apps.
  • Regularly audit and update plugins used in your projects.

Congratulations! You have successfully installed Apache Cordova. Thanks for using this tutorial for installing the Apache Cordova on Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the official Apache website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button