Monday, January 23, 2017

Ruby on Rails on Ubuntu Server, and Virtual Machine

GOAL: To have a Ruby on Rails development box that's easy to use in any OS, back-up, and restore. 

One of the essential parts of starting to develop an application is having a good development box set-up. My dilemma was I have a Xubuntu and Windows 7 installed in my laptop. I want to be able to work in both systems without the problem of setting up two working environments on both OS. Thus, this post is about having one development box for the two OS.

WHAT YOU NEED:

  1. Virtual Machine Software - use Virtualbox, VMWare or any other VM solution you prefer.
  2. Internet Connection - essential in downloading the tools we'll need.

VM Running on Ubuntu Server 16.04.1 LTS
The following bullets show the VM specifications, and configurations:
  • VM has 1GB RAM, and 2 vCPU
  • SSH and Samba services were chosen during installation of Ubuntu Server
  • After OS installation, upgrade the server:
$ sudo apt-get update; sudo apt-get upgrade
  • Networking Device is NAT networking
Samba Share Configuration
The Samba services is mainly for accessing the RoR files when running Win7. If Xubuntu is running, just use SSH to transfer/receive files. In order to share a folder, the /etc/samba/smb.conf file must be edited. Add the following lines at the end of the file to share a folder called ruby:
                   [ruby]
                       comment = Ruby on Rails Folder
                       path = /home/argel/ruby
                       available = yes
                       valid users = argel
                       read only = no
                       browsable = yes

Note: Samba user password must be set for user. The following command may be used:
$sudo smbpasswd -a argel
Ruby on Rails via Ruby Version Manager (RVM)
To borrow the words from their home page: "RVM is a command-line tool which allows you to easily install, manage, and work with multiple ruby environments from interpreters to sets of gems."

If you want to explore the homepage and follow the installation instructions there, feel free to do so. What I'll be writing next is based from my experience with the virtual machine set-up.

RVM Installation

NOTE: Before anything else install mpapis public key. (Check from rvm if this hasn't changed)
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

The pre-requisite to install this is to have curl. Ubuntu Server 16.04.1 LTS comes with this package already installed. Run the following command to install RVM:
\curl -sSL https://get.rvm.io | bash -s stable
After the installation, the ~/.bash_profile needs to be reloaded by executing the following command:
source .bash_profile
or
. bash_profile
Ruby Requirements
Ruby needs additional software before it can be installed. Typing the following command installs1 the required packages:
rvm requirements
Ruby Installation
The command below installs the latest stable Ruby version via RVM. If you want to get a list of known versions you can install via rvm, run the command rvm list known. Otherwise, proceed as follows:
rvm install ruby

To check whether installation was successful:
ruby -v
Now, if you installed multiple versions of ruby, you can choose which version to use by using the --default option. Example:
rvm --default use 2.0.0
Rails Installation
Since ruby is now installed, it's time to complete the Ruby on Rails 3.0 development box by installing Rails. To do this, issue the following command:
gem install rails
This will install the latest version of rails available from RVM.

Install nodejs
This package is needed in order to develop web applications that need javascript gem. Most web applications have javascript so to be on the safe side, install this.
sudo apt-get install nodejs
That's it. The VM now has a development environment for Ruby on Rails! Other advanced set-ups will be posted later like installing productive database postgresql, or deploying in heroku.

Wednesday, February 05, 2014

Using NAT, and Host-Only Adapter for Virtualbox

A virtual machine using a bridged-adapter as its network adapter will have its own unique IP address. In small networks, as much as possible, IP assignment should be limited. This is where Network Address Translation (NAT) comes in. The VM client will still have connection to the Internet, or within the host's network while also not getting assigned its own IP. Any traffic by the VM client is seen as traffic from the VM Host.

In my old post, I used samba share if Windows is used to run virtualbox. And then, any editor can be used to edit the rails files. My dilemma was using samba share in my Xubuntu machine while maintaining the NAT network set on my machine. I tried port forwarding to address ssh, and accessing rails via the browser. But I couldn't seem to solve the samba port forwarding. Instead of figuring out the exact ports to forward for samba, I just used the Host-Only Adapter for virtualbox.

If you are using Windows machine, this is readily available. For Xubuntu, or any other linux-based OS, this should be created first in Virtualbox. To do this, open Virtualbox preferences or press CTRL+G. Then, select the Network. You should be seeing something similar to this:


As you can see, I already have my host-only network in the image above. If this is the first time you are doing this, yours should be empty. Next, you may add a network by clicking the add button. (The green one with the plus sign)

From here, a default set of IP address will be generated by Virtualbox. You may change this if you want to suit your needs. Afterwards, if your OS in your client is already installed, you may need to edit your settings so that your guest OS can use your new adapter. In my case, I already have Ubuntu Server 12.04.3 LTS installed already, thus, I added the following to the file interfaces located at /etc/network/interfaces

auto eth1
iface eth1 inet dhcp

I restarted the networking service after. If you want to set a static IP, you may do so.

Tuesday, March 05, 2013

How to Secure Shell (SSH) to a Server Without Using a Password

I really don't like typing passwords, as evidenced by my previous post about autologin. I have a local VM of Ubuntu Server and I hate it when I have to press the right+CTRL to be able to get out of the session and do my work on the other open applications. Thus, I use secure shell protocol a lot. Since I hate typing passwords every time I log-in, I used the ssh-keygen to provide a secure log-in to my VM without entering any password. In this post, Host Machine will connect to VM Server securely without the need of a password.

Step 1: Do you have ssh installed?
In order to connect via ssh securely, you need to have ssh. Check if you have one by running:
$ sudo apt-get install ssh
Step 2: Generate Authentication Key
To generate authentication keys, the ssh-keygen program is used. Run the following commands in Host Machine:
$ ssh-keygen -t rsa
A prompt that asks where to save is shown and one that asks, and verifies the passphrase. Since a password-free log-in is desired, press Enter three times.

Step 3: Copy Authentication Key to Remote Computer
Next, the generated key is copied to the remote computer, in this post it's VM Server. There are numerous ways to do the copy, the command below is one of them. Feel free to use other means of copying the keys but make sure that it is copied in folder .ssh under the home folder.
$ cat id_rsa.pub | ssh -p 2222 argel@localhost 'cat >> .ssh/authorized_keys'
Note that the command above assumes that the set-up is the same based on my previous posts about installing VM on Ubuntu1, and about using port-forwarding to ssh to the VM server2.

Step 3: Log-in with NO PASSWORD via SSH!
Now, try logging into the VM and verify that a password is not needed to have access to VM Server.



Step 4 (Optional): Adding Alias in .bashrc
Optionally, adding an alias in .bashrc file of Host Machine will further help in shortening the commands to type to ssh to the VM Server. In the example below, c3po is the alias used. The corresponding ssh command is executed if c3po is executed:
alias c3po='ssh -p 2222 localhost'
Author's Note: For VM Server to access Host Machine with a password, do the same process but this time use VM Server to generate the keys and transfer those keys to Host Machine. DO NOT DELETE the id_rsa file. The id_rsa.pub can be safely deleted after it is copied to the remote server.

Wednesday, February 06, 2013

How to Enable Extended Display in Xubuntu

I've been using XFCE-based Ubuntu since 2003. At that time, install Ubuntu first, then install the XFCE desktop environment. Nowadays, you can get a copy of XFCE-based Ubuntu, called Xubuntu, from http://www.xubuntu.org.

I like this flavor of Ubuntu simply because of its minimalist look and minimal usage of resources. But unlike the Gnome-based Ubuntu, Xubuntu's extended display does not work right after installation. I did not bother looking for a solution until my recent addiction to Ruby on Rails. I needed to separate my work stuff to Ruby on Rails stuff. I requested for a new monitor at the office so I can work on two displays.

I used the following script from this post in http://www.askubuntu.com forums:
#!/bin/bash
# Script to enable extended monitor
# This sets the VGA1 monitor resolution.
xrandr --output VGA1 --mode 1280x1024 --rate 60
# This sets the laptop monitor resolution.
xrandr --output LVDS1 --mode 1366x768 --rate 60
# MONITOR ORDER
# Put the Laptop right, VGA1 monitor left
# xrandr --output VGA1 --left-of LVDS1
# Put the Laptop left, VGA1 monitor right
xrandr --output LVDS1 --left-of VGA1
# PRIMARY MONITOR
# This sets your laptop monitor as your primary monitor.
xrandr --output LVDS1 --primary
# This sets your VGA monitor as your primary monitor.
# xrandr --output VGA1 --primary
After saving this script in a file, I just modified the permissions using chmod for it to be executable. Now, I have a working Xubuntu with an extended display monitor!

Author's Update: I sometimes use projectors aside from a monitor. Thus, there's a need to use a different resolution and orientation in my extended display. I modified the code above to give some options in the script.
#!/bin/bash
#Script to enable extended monitor
if [ $# -ne 2 ]
  then
    echo "Usage: ./dual_monitor.sh  , 1=1024x768, 2=1280x1024
          orientation is either left or right"
else
  #Variable Declaration
  screenSize=$1
  orientation=$2

  if [ $screenSize -eq 1 ]
    then
      xrandr --output VGA1 --mode 1024x768 --rate 60
  elif [ $screenSize -eq 2 ]
    then
      xrandr --output VGA1 --mode 1280x1024 --rate 60
  else
    xrandr --output VGA1 --mode 800x600 --rate 60
  fi

  if [ $orientation == "left" ]
    then
      xrandr --output LVDS1 --left-of VGA1
  elif [ $orientation == "right" ]
    then
      xrandr --output VGA1 --left-of LVDS1
  else
    xrandr --output LVDS1 --left-of VGA1
  fi
#Set laptop as primary monitor
xrandr --output LVDS1 --primary
fi

Monday, February 04, 2013

Improving VI Improved! (vim)

I haven't used vim for a very loooooong time. But since my friend Jim convinced me to try RoR, I've been back at it again. I never really appreciated it's full power until now. I will be updating this post every time I add a new plugin in my vim or whenever I change my .vimrc file.

  • .vimrc file - Get it here.
  • vim plugins
    1. Add matching brackets, parenthesis, etc. - credit goes to Karl Guertin - Get it here.