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.

1 comment:

  1. It is nice blog Thank you provide important information and i am searching for same information to save my time Ruby on Rails Online Training Bangalore