Installing M.E.A.N on Ubuntu

The MEAN stack is comprised of 4 key elements that work together to dish out some powerful Javascript-FU. It is fairly easy to install the stack, but you want to stay away from the sources included with most package managers as they tend to be a bit out of date. I have comprised a list of ways to install the MEAN stack with the newest versions below.

Nodejs

The most obvious option would be to head on over to Nodejs.org and pull down the pre-compiled version for your O.S. If that is not an option for you, you can also get the source code. There is a great document on the Github Wiki page showing how to install from source.

If you plan or have need to work with more than one version of Nodejs on a single machine you may want to install via NVM (Node Version Manager). This will allow you to install any number of versions and switch between them with east. You can find the Github project here NVM.

Express

Express is the framework for Nodejs that allows you to build out your applications back end. In order to use Express you will need to install it via NPM (Node Package Manager).

npm install -g express && nmp install -g express-generator

A brief note should be mentioned about the '-g' switch. Normally Node will install the package dependencies for a project in the projects directory. In the case of a library like Express, we want that functionality to be available to us anywhere. When you use '-g' you are telling NPM that you would like this package to be installed globally.

Angularjs

Angularjs is the wizard that supplies the front end magic to the MEAN stack. It is a Javascript library that handles all sorts of cool things like two-way-binding and HTML Directives. There is technically nothing to install for Angularjs, you merely need to include the Javascript files in your project and link them in your HTML.

MongoDB

When Nodejs, Express, and Angular are done dishing out some Javascript-FU they need a place to record the carnage, this is where MongoDB comes into the picture. MongoDB is a Open Source No-SQL document store. There is an excellet write up on how to install MongoDB on Ubuntu here.

Resources

nodejs
Angularjs
Installing Node and NPM
MongoDB

Bootstrap CSS Framework and Rails

Adding Bootstrap to Rails is pretty easy with the Asset Pipeline. Granted it would be easier without the Asset Pipeline, but having it has more benefits than the extra overhead for configuration.

You will first want to visit the getbootstrap.com and download the compiled and minified version. Once you extract the files you will have something like this:

    /bootstrap--dist
        /css
            /bootstrap.min.css
            /bootstrap-theme.min.css
        /fonts
        /js
            /bootstrap.min.js

You will want to extract your bootstrap.min.css and the bootstrap-theme.min.css to the Rails /vendor/assets/stylesheets directory. Now alter the /app/assets/stylesheets/application.css so that it looks like the following:

    *= require_self
    += require bootstrap.min
    *= require_tree .
    */

    @font-face {
        font-family: 'Glyphicons Halflings';
        src: url('../assets/glyphicons-halflings-regular.eot');
        src: url('../assets/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'),
        url('../assets/glyphicons-halflings-regular.woff') format('woff'),
        url('../assets/glyphicons-halflings-regular.ttf') format('truetype'),
        url('../assets/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg');
    }

After that you need to copy the entire fonts directory into the /app/assets directory. To get the javascript working You will need to copy the bootstrap.min.js into the /vendor/assets/javascripts directory then alter /app/assets/javascripts/application.js to look like the following:

    //= require jquery
    //= require jquery_ujs
    //= require turbolinks
    //= require bootstrap.min
    //= require_tree .

Make sure you reboot your instance and your good to go.

Asset Pipeline Precompile Fail

I had a bit of an adventure tracking down an issue after moving one of my sites to another server. This particular application uses Rails 4 and the twitter-bootstrap-rails gem (2.2.7).

I did the typical rake assets:precompile, like you do, and went about my day. I later realized that the icons for Font Awesome were not showing up. Being the late night worker that I typically am I simply thought I may have missed a step. So I diligently clobbered the assets and precompiled… again.

This did not work. Checked the logs and found there to be not a single error specify asset paths not found. Interesting. I started looking at some of the gems in both environments, making sure they were the same version numbers of which they were exact. This is an important step for me because I tend to forget to specify the version number, which leaves me open to getting the newest version (and unexpected behavior) on the next install.

A couple of Googles later and I still had nothing, everything suggested what I had already tried. I finally found a link to an issue on Github. Which makes me think I should pay more attention and search for issues already submitted, they may just help me out again.

The issue pointed me to try

bundle exec rake assets:precompile RAILS_ENV=production RAILS_GROUPS=assets

I thought this to be odd, I had just installed it on the other server less than a month ago. I also used the old Rails 3 method

rake assets:precompile

instead of the new Rails 4 method

RAILS_ENV=production bundle exec rake assets:precompile

I am actually even more curious now as to why they want you to specify the environment. It is my understanding that you want to precompile for production as it shortens the response time. Seems out of place to tell it to do something for production that is intended for production.

Well this little icon adventure had a happy ending, and now the site looks pretty again. Hope my mistakes help you to not make the same.

Active Record Jailbreak

There are plenty of good ORM (Object Relational Mapping) gems that you can use in your projects, but only one is attached to Rails. Being associated with Rails means that it has seen and will continue to see pretty heavy development. This makes it the candidate for use when ever you need to interact with a database in Ruby. Turns out it is pretty easy to do.

So, what you want to start of with is adding it to your Gemfile. What? You don’t use Bundler for your projects? It is perfect for any project that you will be deploying to any other machine than the one that it was written on. That being said you should jump on the wagon with RVM (Ruby Version Manager) as well.

If you are not using a Gemfile for this project just make sure you install your gem before you try to use it. This might be a good time to point out that if you are pulling the latest version of ActiveRecord (as you are with the above code) you will need to be using Ruby 2.0. If you are using Ruby 1.9.3 you will want to use version 3.2.14.

  gem 'activerecord', '3.2.14'

Now its time to get to work.

1
2
3
4
5
6
7
8
9
require 'active_record'

ActiveRecord::Base.establish_connection(
  adapter: mysql
  database: dbName
  username: user
  password: password
  host: localhost
)

What the above line of code does is require active_record and sets the params for the connection to the database. There are several other options you can consider when establishing your connection but these will get you started. Now active record knows how to communicate with your database and you can finally start to make your models.

1
2
class Posts < ActiveRecord::Base
end

I am going to guess that you are using this in a project that you want to keep clean. A better option would be to use YAML and pull that information in before you use it.

1
2
3
4
require 'yaml'

databaseConfig = YAML::load(File.open('path/to/config'))
ActiveRecord::Base.establish_connection(databaseConfig)

Now you have everything you need to work with your persistence layer.

Installing Oracle Java on Ubuntu

I use a lot of Jetbrains products and they always complain about OpenJDK. I have nothing against the OpenJDK but I really don’t like seeing those complaints every time I launch one of the applications from the command line. So I decided to look for a way to install Oracle Java.

There seems to be as many ways to install, update, and upgrade Java as there are flavors of Linux. Some of them are overly complex while others are very very simple. I am not a comfortable with Linux as I should be so I chose the simple way, install from a third party repository. Please note that one of the issues with using a third party repo is that you cannot always guarantee that it will be there and that it will be up to date.

So the PPA (Personal Package Archive) that I have found to work fine all the (8+) times I have used it on different machines is ppa:webupd8team/java. In order to use this repository you must add it with:

sudo add-apt-repository ppa:webupd8team/java

Once you have added the repository you can then install the desired package from it just like any other package.

sudo apt-get update
sudo apt-get install oracle-java7-installer

It will ask you if you agree to the terms of service as described by Oracle, then the best part; it does everything else for you. Your done.