Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Install Android And Telerik NativeScript In Ubuntu Linux

TwitterFacebookRedditLinkedInHacker News

Ubuntu Linux is one heck of an operating system. It is fast, visually appealing, and it is great for development. Previously I wrote about how to install Ionic Framework and all the required dependencies for development in Ubuntu, but this time we’re going to look at how to do the same for Telerik NativeScript.

We’re going to review a series of shell commands that will get the Java Development Kit (JDK), the Node Package Manager (NPM), the Android SDK, and Telerik NativeScript up and running in no time.

Before going any further I’d like to point out that I’ve wrapped everything seen in this article into a nice script found on GitHub. If you’re not interested in the how or the why behind the script and only want it working, go ahead and visit GitHub.

You can run the script found on GitHub by executing the following from a Terminal:

chmod +x ubuntu_nativescript_installer.sh
sudo ./ubuntu_nativescript_installer.sh

If you’re interested in seeing what the script does and why it does what it does, keep reading.

The first thing you can see in the script is the following:

INSTALL_PATH=/opt
ANDROID_SDK_PATH=/opt/android-sdk
NODE_PATH=/opt/node

We’re defining where our files will be installed to. On Ubuntu, a great place to install things so they can be accessed by all users on the operating system is the /opt directory.

Because we’re running the script with sudo we need to keep track of who the parent user is. By parent user I mean the user who initiated the sudo command. When using sudo, many of our directories will hold root ownership. This includes directories in our users home directory. We need to make sure during cleanup, all files and directories in the home directory are defaulted back to the parent user. To get that parent user we would run the following:

PARENT_USER=$(who am i | awk '{print $1}')

There isn’t a good way to get the latest Android SDK and Node.js downloads so we have to specify what version we should download. For example, the following says we want revision 24.4.1 of the Android SDK and 4.2.6 of Node.js:

ANDROID_SDK_X64="http://dl.google.com/android/android-sdk_r24.4.1-linux.tgz"
NODE_X64="https://nodejs.org/download/release/v4.2.6/node-v4.2.6-linux-x64.tar.gz"

Even though 64-bit operating systems have been around forever, the Android SDK still has some 32-bit dependencies. We need to tell Ubuntu that we wish to be able to install these dependencies. This can be done by running:

dpkg --add-architecture i386

To download the required dependencies, we run the following:

apt-get update
apt-get install -qq -y libc6:i386 libgcc1:i386 libstdc++6:i386 libz1:i386

This will first update our software repository catalog and then download what we need. They are all build dependencies such as C and C++ compilers.

With the build dependencies out of the way we can install the latest JDK and g++ which is used for building NativeScript in a future step. We don’t want to be interrupted to install these things so we can run them in silent mode with the following:

apt-get -qq -y install default-jdk g++

With the JDK installed we want to first check to see if $JAVA_HOME was already set. If it was not already set, find the JDK that was just installed and add it to our user profile. All of this can be done like so:

if [ -z "$JAVA_HOME" ]; then
    export JAVA_HOME="$(find /usr -type l -name 'default-java')"
    if [ "$JAVA_HOME" != "" ]; then
        echo "export JAVA_HOME=$JAVA_HOME" >> ".profile"
    fi
else
    echo "The Java Development Kit was already installed"
fi

Android sometimes relies on $JAVA_HOME being set which is why we take the time to set it.

Now we can worry about downloading and installing the Node.js and Android SDK files that we defined, but before we do that we want everything to be downloaded to Ubuntu’s temporary directory:

cd /tmp

By having everything download to the temporary directory, it eliminates us from having to do manual cleanup. The operating system will clean it up on its own time.

Starting with the downloading and installation of Node.js, we see this:

if [ -z "$NODE_HOME" ]; then
    wget -c "$NODE_X64" -O "nodejs.tgz" --no-check-certificate
    tar zxf "nodejs.tgz" -C "$INSTALL_PATH"
    cd "$INSTALL_PATH" && mv "node-v4.2.6-linux-x64" "node"
    cd ~/ && echo "export NODE_HOME=$NODE_PATH" >> ".profile"
    cd ~/ && echo "export PATH=\$PATH:$NODE_PATH/bin" >> ".profile"
    export PATH=$PATH:$NODE_PATH/bin
else
    echo "Node $(node -v) was already installed"
fi

We’re saying we want to first make sure the $NODE_HOME path was not set. If it was set we can assume it was already installed. We don’t want to step on a previous installation or in case the user (you) tries to run the script twice. If it is not set we want to download Node.js by using the wget command. We’ll extract the archive and move it to the appropriate installation path that we defined at the beginning of the script. Finally we’ll add the path to our profile so we can access it via a command line as necessary.

This leads us to the Android portion:

if [ -z "$ANDROID_HOME" ]; then
    wget -c "$ANDROID_SDK_X64" -O "android-sdk.tgz" --no-check-certificate
    tar zxf "android-sdk.tgz" -C "$INSTALL_PATH"
    cd "$INSTALL_PATH" && mv "android-sdk-linux" "android-sdk"
    cd "$INSTALL_PATH" && chown $PARENT_USER:$PARENT_USER "android-sdk" -R
    cd "$INSTALL_PATH" && chmod 777 "android-sdk" -R
    cd ~/ && echo "export ANDROID_HOME=$ANDROID_SDK_PATH" >> ".profile"
    cd ~/ && echo "export PATH=\$PATH:$ANDROID_SDK_PATH/tools" >> ".profile"
    cd ~/ && echo "export PATH=\$PATH:$ANDROID_SDK_PATH/platform-tools" >> ".profile"
    export PATH=$PATH:$ANDROID_SDK_PATH/tools
    export PATH=$PATH:$ANDROID_SDK_PATH/platform-tools
    echo y | android update sdk --no-ui --filter tools,platform-tools,build-tools-23.0.2,android-23,addon-google_apis-google-23,extra-android-m2repository,extra-android-support
else
    echo "The Android SDK was already installed"
fi

Although not really any different from the Node.js portion, we have to look at the following line:

echo y | android update sdk --no-ui --filter tools,platform-tools,build-tools-23.0.2,android-23,addon-google_apis-google-23,extra-android-m2repository,extra-android-support

We’re saying that we want to install Android API 23 via the command line instead of having to run the Android SDK Manager after our script has finished running. Everything in this line is required to build NativeScript applications.

We’re almost done. Now we need to use the Node Package Manager (NPM) that was installed with Node.js to install the NativeScript CLI tool:

npm install -g nativescript --unsafe-perm

Notice the --unsafe-perm of this command. We include this because we’re currently using sudo to run as the root user. The official NativeScript documentation even says to include it.

The bulk of our script is now finished. It’s time to do some cleanup.

cd ~/ && chown $PARENT_USER:$PARENT_USER ".android" -R
cd ~/ && chown $PARENT_USER:$PARENT_USER ".node-gyp" -R
cd ~/ && chown $PARENT_USER:$PARENT_USER ".tnsrc" -R
cd ~/.local/share && chown $PARENT_USER:$PARENT_USER ".nativescript-cli" -R

Remember the parent user that we defined at the beginning of our script? This is where we make use of it. Our home directory is mangled with wrong ownership so we need to change it back otherwise things, only related to what we just did, won’t run properly. This script doesn’t change anything important so feel safe that we only did isolated things.

When you’re done either running the script or doing the manual process, restart your Ubuntu session and give it a whirl.

tns create MyProject
cd MyProject
tns platform add android

All should be good at this point!

Conclusion

It wasn’t particularly difficult to get Node.js, the Android SDK, and Telerik NativeScript up and running on our Ubuntu Linux computer. However if you’re new to Ubuntu you can easily be left scratching your head which is why I made the nifty script on GitHub. With it you can start developing Android applications.

A video version of this article can be seen below.

Nic Raboy

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in C#, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Unity. Nic writes about his development experiences related to making web and mobile development easier to understand.