OU blog

Personal Blogs

ExLibris

Installing apache cordova under Linux: Summary

Visible to anyone in the world
Edited by Martin Thomas Humby, Sunday, 22 Jul 2018, 16:01

Installing Cordova under Linux Ubuntu / Mint is simple but many guides found on the web and elsewhere are out of date and/or hard to follow. This post is a list of commands for those faced with an incomprehensible set of instructions and as an aid mémoire for me. Commands for installation were tested using a fresh copy of Linux Mint 18.2 (Ubuntu 16.04 Xenial Xerus). As recommended, Cordova is installed using the JavaScript package manager NPM.

Only minimal experience using the command line is assumed. Reminder: paste in a terminal window is Edit > Paste or Ctrl+Shift+V. The sudo password expires after 15 minutes (by default) so probably best to copy and paste the first sudo prefixed command separately for each section. With this proviso commands can be copied and pasted as a block (ensure the basic command prompt shows after or press return to get it). After sudo all characters in the user password you type are hidden – no dots or * displayed. Texts following # are my remarks and can be in included in copy or not as convenient.

Open a terminal window
Click on the desktop and press Ctrl+Alt+T. The command prompt shows:

martin@mint18 ~ $  # for example

The prompt is shown below where a particular current directory is assumed in commands – avoid including bits of it in copy smile.

Check Java is installed

java -version

If Oracle Java has been installed previously this should get something like

java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode

If it is not present enter the commands

sudo add-apt-repository ppa:webupd8team/java 
sudo apt-get update
sudo apt-get install oracle-java8-installer

A similar painless method can be used to install Java 10. If Open Java is present from java -version it is probably best to uninstall it.

Check Node.js installed

nodejs --version

If not found download an installer for Node.js LTS version:

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get update    # essential for a correct install

and do the install

sudo apt-get install -y nodejs

Node.js should include the JavaScript package manager NPM so check it worked by getting versions:

nodejs -v
sudo npm -v # sudo is optional here but will save a spurious update-check warning

Install the Cordova application

sudo npm install -g cordova
sudo chown -R $USER ~/.config/configstore # give the user (you) access after NPM install as root

cordova # should display a help screen

Set system variables

Cordova requires two new system variables JAVA_HOME and ANDROID_HOME and modification of the PATH variable.

JAVA_HOME

NB: I have shown the Java version as 10 in all occurrences below because this blog renders  8–o as an emoticon copying from the page as 'surprise'. Version 8 should be used until Android Studio / SDK catches up.
sudo find / -name java-10-oracle  # or java-8

This will return something like

[sudo] password for martin: 
find: ‘/run/user/1000/gvfs’: Permission denied
/etc/java-10-oracle
/usr/lib/jvm/java-10-oracle

The JVM location is the one we want.

Find the name of the text editor that came with your system (Help > About from its menu bar). Open a new terminal window and run the editor with root privileges

sudo editor-name    #  Do not close the terminal window or this may close the editor instance
# alternatively
sudo editor-name /etc/environment # should open the required file directly

Edit  /etc/environment adding a line at the bottom of the file

JAVA_HOME="/usr/lib/jvm/java-10-oracle"  # or substitute java-8
Save the modified file, close the editor and terminal window.

ANDROID_HOME and PATH

To generate an Android app Cordova requires the Android SDK and Gradle. Both should be installed under your home directory with their locations identified in these system variables. If Android Studio is installed at a later date, when you first create a project it will copy the SDK and more to a directory ~/Android/Sdk so might as well put it there. The version of Gradle included in PATH below is that used in TM352-17J: modify for a later version.

Open the text editor and do File > Open, Right Click and check Show Hidden Files. Open the file .bashrc and add the following lines at the bottom. Alternatively entering: editor-name ~/.bashrc in a terminal should open the file directly.

# added for Cordova install
export ANDROID_HOME=$HOME/Android/Sdk
export PATH=$JAVA_HOME/bin:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools:$HOME/Gradle/gradle-4.4.1/bin:$PATH

Save the file.

To register JAVA_HOME the system must be restarted but before doing so you may wish to save a copy of the installation so far from the terminal window to a file: Edit > Select All, Ctrl+Shift+C and paste into a text editor.

Install Android SDK

Open a terminal window and create the directory for the SDK

martin@mint18 ~$ mkdir Android/Sdk

Download the Linux sdk-tools .zip from https://developer.android.com/studio/#downloads (from the 'Command line tools only' section) to Android/Sdk. Extract the zip’s tools folder into this directory.

Tools for creating Android apps
It is essential that ANDROID_HOME is set correctly here or sdkmanager will put files in the wrong folder. To check it is correct
echo $ANDROID_HOME
Optionally, to avoid a warning enter
martin@mint18 ~$ mkdir .android && touch .android/repositories.cfg

Copy the following, then paste and enter at the command prompt

sdkmanager "platform-tools" "platforms;android-26" "build-tools;26.0.3" "system-images;android-26;google_apis;x86" "extras;android;m2repository" "extras;google;m2repository"

Accept licence agreements. Versions are those used in TM252-17J: check they meet your requirements.

There should now be a new folder ‘cache’ in /home/martin/.android/ containing many .xml scripts and several new folders in ANDROID_HOME including build-tools. The file repositories.cfg remains empty indicating this file and any warning generated if it does not exist are probably spurious.

Install Gradle
martin@mint18 ~$ mkdir Gradle

Download Gradle v4.4.1 – complete from https://gradle.org/releases/ into the new Gradle folder and extract into  ~/Gradle. If you download a later version do not forget that PATH must have been adjusted accordingly.

Check installation
To check that all this worked a Cordova project must be created
martin@mint18 ~$ cordova create Test_cordova
cd Test_cordova
cordova platforms add android
cordova requirements

The result should be

Android Studio project detected

Requirements check results for android:
Java JDK: installed 1.8.0
Android SDK: installed true
Android target: installed android-26
Gradle: installed /home/martin/Gradle/gradle-4.4.1/bin/gradle

If an error is seen check the relevant system variable is correct and the names of files match. Otherwise installation is complete.

JAVA_HOME could be defined in ~/.bashrc using export as for ANDROID_HOME and PATH saving the complication of text editor root privileges. Java is available to all applications so it is good practice to put the definition in /etc/environment. If additional software, Android Studio for example, is installed Cordova will continue to work even if JAVA_HOME is modified for some reason.

Permalink
Share post

This blog might contain posts that are only visible to logged-in users, or where only logged-in users can comment. If you have an account on the system, please log in for full access.

Total visits to this blog: 74787