Monday, November 30, 2015

CSE 321 Project - Part 2 - Install Ruby, Rails, PostgreSQL, MySQL and others

This is one post in a series of posts related to CSE 321 - Software Engineering project for ASU CSE 2017 students. The series starts from setting up the environment, to planning the work on the website project, to implementation

After Installing Lubuntu/Ubuntu in a VM in part 1, we want to prepare it for development.

Thinking about the required project, this is what I want to install.
  • Ruby
  • Rails
  • PostgresSQL (or MySQL)
  • Node.js
  • Git
  • Gitg (a graphical tool to use with Git)
  • Atom editor (or you can install Sublime if you like)
Plus enabling copy & paste between the VM and the computer, and enabling flexible display resolution to suit your taste. And I will start with this.

Install Guest Additions

Now we need to install some additional packages to add more power to our VM. This will make our job easier in scaling the VM window (and keeping the aspect ratio), and enables copy&paste between the VM and the host computer to easily follow the next instructions.

If you have a toolbar in the outer VM window, go to Devices -> Insert Guest Additions CD image.... If you cannot find the toolbar, press Right Ctrl + Home buttons and the menu should appear. (if you still fail, press Right Ctrl + C and search for the toolbar again)

Once you do this, a virtual CD will be mounted (inserted) into the machine to install some additional packages.

In case of Ubuntu, the process should start immediately once you click Run in the box that will appear.

In case of Lubuntu, you will have something like this:

Click OK. Then run in the window that will appear (this is the content of the virtual CD).

Then choose Execute.

Authorize the script to have additional privileges by entering your password and pressing OK.

The script will run for a few seconds, installing some packages. Once finished you will have to press Enter to exit.

Extra: In case of Lubuntu, you need to install another package. Open the terminal by pressing Ctrl + Alt + T or clicking the start menu -> System Tools -> XTerm. Then type (you cannot copy at this point):
sudo apt-get install --yes virtualbox-guest-dkms
Then enter your password when it asks for it (it will download about 60MB).

Now shutdown the machine, go to this VM Settings -> General -> Advanced tab, and set Shared Clipboard to Bidirectional. This way you can copy instructions from your host OS to the VM terminal, and copy any error message you face from the VM terminal to -for example- your browser in the host OS.

Now let's start the VM again.

This time, try to copy any text (for example, a url) from your host OS. Then search for Firefox browser in the VM and paste it. It works!

One more thing has happened. If you resize the VM window, it will scale and keeps the aspect ratio. If you find the screen is getting 'compressed' and losing the correct ratio, press Right Ctrl + C.

Now we have a clean and helpful environment to work with.
Let's start with the development installations. Open the terminal and get ready!

Install Ruby

(references used: DigitalOcean, GoRails)

First there are some packages to install before Ruby.

Update out package list
sudo apt-get update

Install git, curl, and some other packages for compilation (~90MB)
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

Now install rbenv (the tool for manging Ruby versions, better than installing Ruby directly)
git clone git:// .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
git clone git:// ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Next, install Ruby and set the installed version to be the default. (this will take a while to download and compile)
rbenv install -v 2.2.3
rbenv global 2.2.3

Now if you type
ruby -v
you will see the default ruby version.

Let's add one more step, disable documentation installation. We do not need them.
echo "gem: --no-document" > ~/.gemrc

Install Bundler Gem

This is the first gem to install. It is responsible for handling later gems in Rails projects.
gem install bundler

Install Rails
gem install rails -v 4.2.5

When it finishes, if you type
rails -v
you will see the default rails version.

Install Node.js

Node.js is a platform for development using Javascript. Rails uses Node.js in a certain step when joining multiple Javascript files together for better performance. You may not need this at first, but let's setup the environment the right way for future use and future reference.

curl -sL | sudo -E bash -
sudo apt-get install -y nodejs

Now type
node -v
to check the installed version.

Install PostgreSQL

PostgreSQL is the biggest competitor to MySQL database. It is getting more popular, so it is nice to stay up-to-date and give it a try. But don't be scared. at first, it will not matter if you use MySQL or PostgreSQL because most of your interactions will be through Rails. (You can skip it and install MySQL)
sudo sh -c "echo 'deb precise-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
wget --quiet -O - | sudo apt-key add -
sudo apt-get update
sudo apt-get install -y postgresql-common
sudo apt-get install -y postgresql-9.4 libpq-dev

Create PostgreSQL User

The last step is to create a new db user for later.
sudo -u postgres createuser cse -s
If you would like to set a password for the user, you can do the following
sudo -u postgres psql
postgres=# \password cse 
then enter the password you like.

Install MySQL

Use this command to install MySQL server and client.
sudo apt-get install mysql-server mysql-client libmysqlclient-dev
When prompted, you can choose to enter a password for the root user of MySQL or leave it blank. Whatever you choose, then use the down arrow to go to OK line and hit enter.

The root user has access to all databases. It is not safe to use it in production environment. It is better to create a new user for each project and give access to project's database only. But since this is a development environment, we can use root user.

Install Atom Editor

Now we need a nice editor to help us with developmen. Recently, I switched from Sublime Text to Atom Editor. So I will continue with using Atom. Feel free to use whatever you want.
You can install Atom by downloading the installer (*.deb) from their home page, or install it from the terminal. To avoid explaining how to share folders between VM and host, I will install it from the terminal. You can follow my steps or download the installer from the VM browser.
sudo add-apt-repository ppa:webupd8team/atom
sudo apt-get update
sudo apt-get install atom

Install Gitg

The last optional step is to install Gitg. It is a graphical interface to use Git. It make things a bit easier when it comes to committing changes, reviewing history, and other operations you may need.

sudo apt-get install gitg

That's all folks! Your machine is ready. You can compress the machine and share it with others or keep it as a backup as a ready-to-use machine.

CSE 321 Project - Part 1 - Install Lubuntu on VirtualBox

This is one post in a series of posts related to CSE 321 - Software Engineering project for ASU CSE 2017 students. The series starts from setting up the environment, to planning the work on the website project, to implementation

In this tutorial, we will start a virtual machine and install Lubuntu or Ubuntu.

Install VirtualBox

The first thing is downloading and installing VirtualBox:

And assuming that you are working on Linux, then you'll go for the first link. The download link is for both 32-bit and 64-bit computers.
(note: x86 means 32-bit, amd64 means 64-bit)

Create a New Lubuntu Machine

Let's create a new machine by clicking the New icon.

Enter a suitable name for your machine. Any name to remind you what this machine does.

Select the amount of memory this machine is allowed to use. If you have plenty of RAM, give it 2GB. No worries, this value can be changed later.

Leave the default option to create a new virtual hard disk.

Leave the VDI option as it is.

Leave the dynamic size as it is. This option allows the virtual hard disk (the file created) to start small then expand later to a max limit. The other option will create the full sized file from the beginning.

Choose a different name for the virtual hard disk or leave it with the same name as the virtual machine name. And you can leave the size at it is for now. Actually 8GB is more than enough.

Now the VM is ready.

Install Lubuntu

Before starting, let's mount the Lubuntu iso image as a CD to use it for installation.

Select the machine and click Settings.

Go to Storage, and select the empty CD.

Click on the small CD icon on the far right. And choose Choose Virtual Optical Disk file.
Then browse your folders and select the iso file you downloaded (Ubuntu iso , Lubuntu iso) then click Open.

Click OK. When done.

Now we are ready to start the machine. Select the machine and click Start or double-click it.

Choose English.

Choose Install Lubuntu.

Wait for a few seconds until it boots the installer.

Choose English.

Click Continue.

Now for the critical part of formatting if it was a real installation. Leave the first option as it is. After all, this is a virtual disk created for Lubuntu. Not your actual disk. Click Install Now. Then click Continue.

Choose the city of Cairo or any other city you prefer, and click Continue.

Leave the keyboard layout as it is and press Continue.

Enter your name and choose the username and password. Click Continue.

Now leave it for a few minutes to install and pull only some basic data from the Internet.

When finished, press Restart Now.

The machine will eject the virtual CD it used for installation and asks you to press Enter. Do it.

The fresh Lubuntu OS will boot.

The login dialog will appear. Enter your password.

Welcome to your new machine. Now you can shut it down and start it again whenever you want.

That's all folk! :)

In the next tutorial, we will work on installing:
  • Ruby
  • Rails
  • Node.js
  • PostgresSQL
  • Atom Editor
  • Git
  • Gitg (a graphical tool to use with Git)
  • Enable copy and paste between the VM and the computer, and enable flexible display resolution.
Take your time and try and fail without worries. Virtual machines are created and deleted all the time. If you mess it up, delete it and try again.

[update: Part 2 is available now]