Setup Ruby on Rails on Windows 10 Ubuntu Linux Subsystem with postgresql

Install Linux subsystem on Windows:

We’re going to use Bash on Ubuntu on Windows as it allows you to run Linux on your Windows machine. This only works on 64-bit installations of Windows. Reboot your machine once the installation completes.

Search for Bash in your start menu and follow the installation instructions to get the Ubuntu subsystem installed on Windows 10 which will be downloaded from the Windows Store. Or you can download and install from Windows Store here for Ubuntu 16.04 LTS version. Launch the downloaded version from Windows Store and install it.

You can also use the command as shown below to download and install from Microsoft Store:

Setup the username and password for the Linux subsystem. The user must be a sudoer to use system commands at the root level. Once the installation is completed you’ll be able to search for Bash on Ubuntu on Windows in your start menu.

Note:

If you face any issues and need to uninstall again, open command prompt (not bash):

lxrun /uninstall /full

Then Turn of the Windows feature for Linux subsystem and follow the installation steps again.

Install Ruby:

First, we need to install some dependencies for Ruby environment. Install the latest recommended version. We’re currently working with Ruby 2.5.3 but this may change in future. Run the following commands in Bash (Remove $ sign as this is part of the prompt at the terminal):

$ sudo apt-get update
$ sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev postgresql-client-common postgresql-client libpq-dev

If the terminal prompts for confirmation, just press ‘y’. The pre-reqs are required to get Ruby and PostgreSQL installed.

Note: Some of dependencies for postgresql are not mentioned on the gorails blog as it is using mysql.

Install Ruby using ‘rubyenv’ with the following commands:

$ cd
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ exec $SHELL
$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
$ echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
$ exec $SHELL
$ rbenv install 2.5.3
$ rbenv global 2.5.3
Finally, check installed version with:
$ ruby -v

Install Bundler:

$ gem install bundler

After bundler install, run the below command:

$ rbenv rehash

Install Rails:

We’ll install the current latest version of Rails 5.2.1.

First, let’s install NodeJS runtime from the official repository that allows us to use Coffeescript and the Asset pipeline in Rails which combines and minifies your javascript to provide a faster production environment.

$ curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
$ sudo apt-get install -y nodejs

Next, install rails:

$ gem install rails -v 5.2.1

Run this to make the Rails executable available:

$ rbenv rehash

You can check the installed rails version as below:

$ rails -v

Setup PostgreSQL:

Download the latest version of PostgreSQL from the as recommended in Official PostgreSQL site. For, Windows 10 64-bit version, use the highlighted version. Download the installer certified by EnterpriseDB for all supported PostgreSQL versions.

This is the full installer that comes with PGAdmin4. Follow the installer steps and note down the username and password used while configuring the installation. The database Server runs using the default port 5432. After the installation completes, run the PostgreSQL Service.

Setup Demo Rails Project:

Let’s create a folder under the C: on Windows which is accessible in Linux under /mnt/c.

$ cd /mnt/c
$ mkdir –p code
$ rails new demoapp –d postgresql
$ cd demoapp

When you change the directory to the demoapp, you’ll find a lot of files created automatically by Rails framework structured using the MVC pattern. This is equivalent to C:\code\demoapp on Windows. In order to further build the app and automatically create the database in PostgreSQL, we need to first modify the config/database.yml file with the correct username and password which was used while installing the PostgreSQL software.

To do this, we can use popular code editor tools like Visual Studio code, Sublime text, Atom etc. We’re going to use Visual Studio Code. You can download and install it here. Open the DemoApp folder in Visual Studio code and find the database.yml under the config folder. Modify the file as shown below. Provide the required password used during installation. This file has the configuration for creating the database under different environments like dev, test and production. Default database.yml format.

Create the database:

The below commands should be run while you’re inside the demoapp folder in Ubuntu Bash.

We can use the rake utility to create and manage the database using the command line tool. Run the below command to create the database:

$ rake db:create

Once the database is up and running, run the rails Server to access the application.

$ rails s

You can visit the website at http://localhost:3000 which is the default port for the app.

You should see a screen like this:

Configure Git (optional):

You can use the Git configuration steps if you’re using version control for your code using GitHub or any other Git Service provider. The general process is to use SSH protocol and creating the public-private key pair. The public key requires to be added in the SSH keys section of the provider with a title while the private key should not be shared. This helps in creating a SSH tunnel from your machine to the provider like GitHub.

$ ssh-keygen -t rsa -b 4096 -C "YOUR@EMAIL.com"

Navigate the .ssh folder outside the demoapp folder:

$ cd .ssh

And copy the rsa public key generated above using text editors like vim or nano.

$ vim id_rsa.pub

You can check if the connection successfully established with your provider account and you should see a success message e.g.

$ ssh –T git@github.com

Accessing the Ubuntu vm created on Azure via vnc server on Mac

Currently I’ve setup the Ubuntu Server 18.04 LTS from the Azure marketplace and I’m trying to access it via VNC Server setup on the Linux machine. Also, you’ll need a vnc client like RealVNC or you can also use the screen-sharing client available on your Mac.

Login via SSH:

First you need to login to your Linux VM as a non-root user which you’ve created while setting up the VM. To spin up a new Linux VM, you can check out this post. You can use the Cloud shell to connect to your VM using the non-root username and password to your machine via SSH. Use the Connect menu of your VM and copy the SSH command to run in the Cloud shell.

ssh your_user_name@IP_Address

You just need to replace the your_user_name and IP_Address parts in the above command. Enter the password you’re prompted for to complete the Login as SSH.

Install the required packages:

We now need to install the required packages like Xfce desktop environment and VNC Server which are not bundled in the Ubuntu OS by default. Xfce is a free and open-source desktop environment for Unix and Unix like Operating Systems.

Update list of packages:
$ sudo apt update
Install Xfce Desktop environment and wait for the installation to complete:
$ sudo apt install xfce4 xfce4-goodies
Install the VNC Server:

$ sudo apt install tightvncserver

Complete the initial configuration and provide the setup password:
$ vncserver

Providing a view-only password is optional. You’ll get the below Output as the initial configuration completes:

Creating default startup script /home/your_user_name/.vnc/xstartup Starting applications specified in /home/your_user_name/.vnc/xstartup Log file is /home/your_user_name/.vnc/your_hostname:1.log

Configure VNC Server:

The VNC Server is by default configured on the port 5901 and display port :1. VNC can launch multiple instances on other ports like :2, :3 and so on.

Let’s first kill the current instance for further configuration that we require:

$ vncserver -kill :1

Output:

Killing Xtightvnc process ID <ID>

Backup the xstartup file before modifying:

$ mv ~/.vnc/xstartup ~/.vnc/xstartup.bak

Create a new xstartup file and open in editor:

$ nano ~/.vnc/xstartup

Add the following lines to your file in the nano editor and save it:

~/.vnc/xstartup
#!/bin/bash
xrdb $HOME/.Xresources
startxfce4 &

This is making certain settings to the graphical desktop like colours, themes and fonts. The last line is starting the Xfce desktop. Now, let’s convert the file to an executable and restart:

$ sudo chmod +x ~/.vnc/xstartup
$ vncserver

Now, let’s connect to the VNC Server from your Mac by creating a SSH tunnel and use Screen-sharing client to connect.

Run this command on your Mac terminal:

$ ssh -L 5901:127.0.0.1:5901 -C -N -l your_user_name your_server_ip

Do replace the your_user_name with your sudo non-root username and your_server_ip with the IP Address of your Linux VM. Provide the password when prompted for your username.

Now, open your screen sharing App available in the Finder Go Menu on your Mac that says “Connect to Server…”.

Click on Connect and provide your password when prompted again and you’ll see the Xfce Desktop running via Screen-sharing.

Spin up Linux VM on Microsoft Azure

Microsoft Azure provides a multiple ways to create Virtual Machines for Windows or Linux along with their multiple market variations. They are:

  1. Azure portal UI at https://portal.azure.com
  2. Azure CLI
  3. Azure PowerShell commands

I’ll be using the Azure portal UI to create a marketplace image for Ubuntu Linux. Some of the UI features may change in future, but the crux will remain pretty much similar more or less.

Open Marketplace for VM Images on Azure portal

Marketplace image

Fill up the VM Image details

Create Image1

  1. Create or select Resource Group.
  2. Give a suitable name.
  3. Select region based on your geographic availability.
  4. For personal use redundancy is not required. You can change Availability options based on Availability Zone or Availability set.
  5. Select the Marketplace image for the available Ubuntu version.
  6. Select a machine size based on vcpus, memory and IOPS requirement. Of course, check the cost factor.


Setup Authentication using Password or SSH public key

You can simply use Username and Password for authentication otherwise use SSH public/private key pair.

For generating SSH public key, use Putty gen for Windows or ssh-keygen on Linux and OSX. You can download a suitable Putty client for windows here.

  1. Generate RSA 2048-bit key and follow the instructions by the tool.
  2. Save the Private key file as .ppk
  3. Save the Public key file as .pub
  4. Export the Private key file as .openssh format using the Conversions menu if this key will be used by an external SSH client such as on Linux.

PuttyKeygen

For the Admin account, put a suitable Username and SSH public key generated above starting with “ssh-rsa” as shown below. Make sure the key is copied as is without any modifications.

generateSshAdmin


Add Disks information

It’s better to use Premium SSD for optimal performance. If you have additional disks already created you can attach it with the VM at this step or you can also do this later.

Create Image2

Networking

This step creates a Virtual Network, a subnet and a Public IP for the VM. All these are added to the same Resource Group while creating these new resources. You can also select if you have these existing resources.

Create Image3


Allow Inbound Ports

You can select the required ports e.g. SSH for connecting using SSH public/private key-pair or RDP to connect using Username and Password.

inbound ports

Management

Keep these to default if you prefer. You can enable/disable any option based on requirement. I turned off Boot diagnostics as it required to create a Storage account so I switched it off, as I don’t require it for a test VM.

Create Image4

Guest Config

You can provide additional post-deployment configuration options using extensions like chef and puppet or Cloud init for Linux.

Create Image5

Tags

You can add various tags to categorize resources for consolidated billing and automation management.

Create Image6

Review your provided details in the next step and click on Create. Wait for the deployment to succeed.

Accessing the VM

For accessing the VM, check if you have inbound port rules set up to access using Public IP address with RDP or SSH. Use Putty configuration client to SSH into the VM using port 22 on Windows machine. From a Unix like system including MacOS, use the following command:

ssh <username>@<computer name or IP address>

For details on how to connect to your Ubuntu Linux VM from your Mac machine, check out this post.