Getting Ionic framework up and running on Ubuntu 14.04

Ionic is a framework for hybrid or progressive mobile app development. Throw in electron and you can get a desktop app too!

Note that this will only deal with android app development, there are likely many more steps involved with iOS.

We need the following software setup –

  • NodeJS – ionic is build using node.
  • Android SDK – to build the android application.
  • Cordova – provides access to phone APIs regardless of platform.
  • Ionic – the framework
  • Bower – manage javascript libraries within the Ionic application
  • Gulp – build scripts for your angular app.

Installing NodeJS

There are multiple ways to install node, we are going to use nvm as it is the most powerful and flexible. It takes a little longer to set up though. Follow the previous link for the long version, here’s the short of it:

sudo apt-get update
sudo apt-get install build-essential libssl-dev
curl https://raw.githubusercontent.com/creationix/nvm/v0.16.1/install.sh | sh
mkdir ~/.nvm/versions
source ~/.profile
nvm ls-remote
nvm install 0.11.13
nvm use 0.11.13
node -v

Each version of node you install has its own libraries. Some extra nvm commands:

nvm ls
nvm alias default 0.11.13
nvm use default
0    0  * * Sun root     certbot -q renew

Installing the Android SDK

You can install Android studio, but here we will just install the SDK. Firstly get the SDK downloading by going to https://developer.android.com/sdk/index.html#Other and clicking the Linux link – something like android-sdk_r24.3.3-linux.tgz. It’s a pretty big download.

While that’s happening let’s get the latest version of Java provided by Oracle.

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-cache search oracle-java
sudo apt-get install oracle-java7-installer

Now back to the Android SDK – I like to install it to /opt so:

sudo tar zxfv android-sdk_r24.3.3-linux.tgz -C /opt
sudo chown sam:sam /opt/android-sdk-linux -R
/opt/android-sdk-linux/tools/android

The Android SDK Manager will open, and a bunch of libs will be selected but not installed. Install them.

Once that is done you will want to create an Android Virtual Device (AVD) which is used as an emulator. In the SDK manager go Tools -> Manage AVDS , then Create. I created a Nexus 5 named nexus-5 using Google APIs – API Level 22 as the target, Google APIs ARM as a CPU/ABI and HVGA as a skin.

Now export the tool paths in .bashrc:

echo "export PATH=\${PATH}:/opt/android-sdk-linux/platform-tools:/opt/android-sdk-linux/tools" | tee -a ~/.bashrc
source ~/.bashrc

You should be able to run adb from anywhere now.

Installing Ionic, Bower, Gulp & Cordova

To install the following we use npm, the NodeJS package manager.

Bower helps organise javascript libraries used by our application and Gulp helps build the application.

npm install -g ionic bower gulp gulp-util cordova

Getting started with Ionic

Head over to http://ionicframework.com/getting-started and follow along:

ionic start myApp tabs 
cd myApp
npm install
bower install
ionic platform add android 
ionic build android 
ionic emulate android 
ionic serve 
ionic run android

Setting up a self signed SSL certificate with Apache

I’m assuming you have Apache installed.

Enable the SSL mod for apache:

sudo a2enmod ssl

Create a directory for the certificates:

sudo mkdir /etc/apache2/ssl

Generate the certificate:

sudo openssl req -x509 -nodes -days 365000 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt

Openssl will ask for some information, make sure you set the FQDN properly, the rest don’t matter.

Now edit your virtualhost to point to port 443:

<VirtualHost *:443>

Add the port to the server name directive:

ServerName example.com:443

Then enable SSL and point the virtualhost at the certificates:

SSLEngine on
SSLCertificateFile /etc/apache2/ssl/apache.crt
SSLCertificateKeyFile /etc/apache2/ssl/apache.key

Save the file and restart Apache

sudo service apache2 restart

Now if you want to redirect non-SSL requests to the SSL port add another virtualhost that looks like the following:

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName example.com
   Redirect permanent / https://example.com/
</VirtualHost>

Then a2ensite the new virtualhost and restart Apache once more.

Apt can’t see sources? Try changing the User Agent

If you are trying to use apt behind a corporate firewall, try changing the user agent apt is using with wget to load the package lists. By default it uses:

User-Agent: Debian APT-HTTP/1.3

which isn’t recognised by the filter.

1. Create and edit /etc/apt/apt.conf

sudo vi /etc/apt/apt.conf

2. Paste the following into the file:

Acquire
{
  http::User-Agent "Mozilla/5.0 (Windows NT 5.1; rv:25.0) Gecko/20100101 Firefox/25.0";
};

3. Update apt

sudo apt-get update

Tada.