After recognizing that I develop almost entirely on Ubuntu Linux, a subscriber of mine asked me how to properly set up an Ubuntu machine for Ionic Framework Android development. Now there are a ton of options to handle this task, but not many bare bones solutions. Most solutions on the internet explain how to use an IDE, or fail to elaborate a complete installation.
If you’re not interested in learning how to accomplish the task of installing NPM, Android, Apache Cordova, and Ionic Framework, you can just download a convenient shell script that I made. If you want to know how this shell script works and would like to get the most out of it, continue reading the article.
To install everything using the shell script I provided, download it and do the following:
1
2
|
chmod 775 ubuntu_ionic_installer.sh
sudo ./ubuntu_ionic_installer.sh
|
The above will make the script executable and then it will be installed using a privileged account. Failing to use sudo will probably give you strange results.
The installation script will download and configure the following:
- Java JDK
- Apache Ant
- Android SDK
- NodeJS / NPM
- Apache Cordova
- Ionic Framework
You can find most of these items installed to your operating system’s /opt directory. Once setup has completed, you will need to restart your Ubuntu session, by logging out or restarting your machine. When you sign back in you will need to download the various Android targets that could not be installed via a command line.
To download the necessary Android targets and platform tools you will need to enter android from a command prompt to launch the Android UI.
Start by installing whatever the SDK Manager recommends when launching it. You will need Android API 19 for Ionic Framework, so install that when you’re able to.
So what exactly are we doing in this wonderful script? Pretty much only very basic downloading and extracting in an automated fashion.
Let’s start by looking at the following line:
1
|
LINUX_ARCH=“$(lscpu | grep ‘Architecture’ | awk -F\: ‘{ print $2 }’ | tr -d ‘ ‘)”
|
The above line of code will parse the system’s CPU information looking for the architecture. Because there are no universal binaries for x86 and x64 architectures we need to figure out what we are installing. The above line will either return x86_64 for 64-bit architectures or i686 for 32-bit architectures.
Next in our script we want to update our Ubuntu software repository list. Because we will be installing Java and Ant from the software channel, we need it to be up to date. Updating the list can be done as follows:
1
|
apt–get update
|
Because the file names for each Ubuntu architecture are different, we need to create a conditional that first determines which binary set should be downloaded and installed. If you’re familiar with Linux, you’ll know that we can do this check like so:
1
2
3
4
5
|
if [ “$LINUX_ARCH” == “x86_64” ]; then
else
fi
|
For the x86 and x64 architectures, the process is pretty much the same. Only the file names are different. However, a critical difference in the x64 architecture is that we need to download a few x86 libraries. This can be done as follows:
1
|
apt–get install –qq –y libc6:i386 libgcc1:i386 libstdc++6:i386 libz1:i386
|
Once that’s done we start downloading the defined versions of NodeJS and the Android SDK by making use of the wget linux command. After downloading, we need to extract the tarballs and rename the directories for easier management. All this can be demonstrated below:
1
2
3
4
5
6
7
8
|
wget “$ANDROID_SDK_X64” –O “android-sdk.tgz”
tar zxf “nodejs.tgz” –C “$INSTALL_PATH”
tar zxf “android-sdk.tgz” –C “$INSTALL_PATH”
cd “$INSTALL_PATH” && mv “android-sdk-linux” “android-sdk”
cd “$INSTALL_PATH” && mv “node-v0.10.32-linux-x64” “node”
|
Since the Android SDK is being placed in a location that requires higher permissions, we need to change the permissions to allow for read and write. This is because the SDK doesn’t ship with everything out of the box. You’ll need install these items after running the install script.
When our NodeJS and Android SDK directories are placed in the correct location, we need to make sure that they get added to our user profile. Adding the paths to the user profile makes them usable every time you sign into your computer. Adding to the profile can be done like so:
1
2
3
|
echo “export PATH=\$PATH:$ANDROID_SDK_PATH/tools” >> “.profile”
echo “export PATH=\$PATH:$ANDROID_SDK_PATH/platform-tools” >> “.profile”
echo “export PATH=\$PATH:$NODE_PATH/bin” >> “.profile”
|
Adding to the profile isn’t good enough for our script because for changes to happen the Ubuntu session must be restarted. Because of this, we also need to temporarily alter our path by running the following:
1
2
3
|
export PATH=$PATH:$ANDROID_SDK_PATH/tools
export PATH=$PATH:$ANDROID_SDK_PATH/platform–tools
export PATH=$PATH:$NODE_PATH/bin
|
Just a few things left to do, then we are done. By default, Ubuntu doesn’t ship with the Java JDK or Apache Ant. These must be installed separately. Because I wanted to keep a silent installation, I added a few tags to a standard install:
1
|
apt–get –qq –y install default–jdk ant
|
The last thing we want to do before cleanup, is install Apache Cordova and Ionic Framework using NPM:
1
2
|
npm install –g cordova
npm install –g ionic
|
Just like that we are good to go. If everything went smooth you should be able to use Ionic Framework with Android in a few lines of code:
1
2
3
|
ionic start ExampleProject blank
cd ExampleProject
ionic platform add android
|
If you’re interested in seeing the full source code to this script you can either download and open it in a text editor, or see below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
#!/bin/bash
# Ubuntu Developer Script For Ionic Framework
# Created by Nic Raboy
# http://bit.ly/1qK4r3b
#
#
# Downloads and configures the following:
#
# Java JDK
# Apache Ant
# Android
# NPM
# Apache Cordova
# Ionic Framework
HOME_PATH=$(cd ~/ && pwd)
INSTALL_PATH=/opt
ANDROID_SDK_PATH=/opt/android–sdk
NODE_PATH=/opt/node
# x86_64 or i686
LINUX_ARCH=“$(lscpu | grep ‘Architecture’ | awk -F\: ‘{ print $2 }’ | tr -d ‘ ‘)”
# Latest Android Linux SDK for x64 and x86 as of 10-19-2014
ANDROID_SDK_X64=“http://bit.ly/1sQrsn1;
ANDROID_SDK_X86=“http://bit.ly/1sQrsn1;
# Latest NodeJS for x64 and x86 as of 10-19-2014
NODE_X64=“http://bit.ly/1qK4dcg;
NODE_X86=“http://bit.ly/1sQrsn0;
# Update all Ubuntu software repository lists
apt–get update
cd ~/Desktop
if [ “$LINUX_ARCH” == “x86_64” ]; then
wget “$NODE_X64” –O “nodejs.tgz”
wget “$ANDROID_SDK_X64” –O “android-sdk.tgz”
tar zxf “nodejs.tgz” –C “$INSTALL_PATH”
tar zxf “android-sdk.tgz” –C “$INSTALL_PATH”
cd “$INSTALL_PATH” && mv “android-sdk-linux” “android-sdk”
cd “$INSTALL_PATH” && mv “node-v0.10.32-linux-x64” “node”
# Android SDK requires some x86 architecture libraries even on x64 system
apt–get install –qq –y libc6:i386 libgcc1:i386 libstdc++6:i386 libz1:i386
else
wget “$NODE_X86” –O “nodejs.tgz”
wget “$ANDROID_SDK_X86” –O “android-sdk.tgz”
tar zxf “nodejs.tgz” –C “$INSTALL_PATH”
tar zxf “android-sdk.tgz” –C “$INSTALL_PATH”
cd “$INSTALL_PATH” && mv “android-sdk-linux” “android-sdk”
cd “$INSTALL_PATH” && mv “node-v0.10.32-linux-x86” “node”
fi
cd “$INSTALL_PATH” && chown root:root “android-sdk” –R
cd “$INSTALL_PATH” && chmod 777 “android-sdk” –R
cd ~/
# Add Android and NPM paths to the profile to preserve settings on boot
echo “export PATH=\$PATH:$ANDROID_SDK_PATH/tools” >> “.profile”
echo “export PATH=\$PATH:$ANDROID_SDK_PATH/platform-tools” >> “.profile”
echo “export PATH=\$PATH:$NODE_PATH/bin” >> “.profile”
# Add Android and NPM paths to the temporary user path to complete installation
export PATH=$PATH:$ANDROID_SDK_PATH/tools
export PATH=$PATH:$ANDROID_SDK_PATH/platform–tools
export PATH=$PATH:$NODE_PATH/bin
# Install JDK and Apache Ant
apt–get –qq –y install default–jdk ant
# Set JAVA_HOME based on the default OpenJDK installed
export JAVA_HOME=“$(find /usr -type l -name ‘default-java’)”
if [ “$JAVA_HOME” != “” ]; then
echo “export JAVA_HOME=$JAVA_HOME” >> “.profile”
fi
# Install Apache Cordova and Ionic Framework
npm install –g cordova
npm install –g ionic
cd “$INSTALL_PATH” && chmod 777 “node” –R
# Clean up any files that were downloaded from the internet
cd ~/Desktop && rm “android-sdk.tgz”
cd ~/Desktop && rm “nodejs.tgz”
echo “———————————-“
echo “Restart your Ubuntu session for installation to complete…”
|
A video version of this article can be seen below.
View more at: http://bit.ly/1XReQFr
Post a Comment