Ruby on Rails – Introduction/Setup
In this guide we will create a base installation for a Ruby on Rails application, to start with we will install a Ruby version manager, then configure the Rails dependencies, set up a database and finally create the application. In this guide I’ll be using CentOS 7, Ruby 2.4.0 and Rails 5.0.1 with MariaDB 5.5.52.
Model-View-Controller is a logical way of thinking about web application architecture, I find it best to vizualise as so:
Model – Your database, as the database determines the low level ‘Model’ of your site.
Controller – The section that serves web connections, and ‘Controls’ the data that is passed from the Model to the View.
Installing Ruby and the Version Manager
After you’ve set up your environment (I’ve begun with a blank install) we need to install a tool to manage our Ruby versions, this is enable to test between versions of Ruby, and also to seperate it from the system version of Ruby that other tools may be dependant on. Typically people go for one of three options:
RVM – Ruby Version Manager is the oldschool solution, it’s rather ‘fat’ compared to the other solutions.
rbenv – A more lightweight solution that RVM – ‘Do one thing and do it well’.
chruby – The most barebones solution on this list, is far more lightweight than the other two.
To begin with we will need to install git, we don’t need the most up to date version so we can just run:
yum install git
After installing git we will to clone the project from Github (I will be installing into the /opt/ruby/ directory, rather than the default directory):
git clone https://github.com/rbenv/rbenv.git /opt/ruby/rbenv/
Browse to the folder we just cloned and run:
./src/configure && make -C src
This will run the configuration script and build the software. Rbenv doesn’t come with a method of installing Ruby by default so we will have to use the ‘ruby-build’ project, the best way to configure this is to clone it directly into the rbenv/plugins folder so that rbenv can find it:
git clone https://github.com/rbenv/ruby-build.git /opt/ruby/rbenv/plugins/ruby-build
Since we aren’t using the default rbenv installation directory you should add these two lines to your
~/.bash_profile file so it knows where to look:
export RBENV_ROOT="/opt/ruby/rbenv" export PATH="/opt/ruby/rbenv/shims:/opt/ruby/rbenv/bin:$PATH"
Now that this is done refresh your shell and try running:
rbenv install -l
This should produce a list of all the avaliable ruby versions to install. I’m going to use version 2.4.0, and set it as the default ‘global’ versiom, we can enter:
rbenv install -v 2.4.0 rbenv global 2.4.0 rbenv rehash
You may need to reload your shell after doing this, make sure that when you run the command
ruby -v it is displaying the correct version. If you get a command not found error when running Ruby, try reloading the shell, if that doesn’t work type
echo $PATH and make sure the
/opt/ruby/rbenv/shims folder is in it somewhere.
If you wish to use different versions of Ruby for different projects I would highly reccomend using the ‘rbenv local 2.4.0’ command instead, this will create a file called
.ruby-version in the current directory (ie: your project directory) and will use that version by default. This will override the global default version, and in turn can be overwritten by either the
RBENV_VERSION environment variable or the ‘rbenv shell’ setting, which is the same conceptually but will only work within your current shell session.
yum install epel-release -y yum nodejs -y
This should be all you need to do with JS here. Next we will install and start the MariaDB database:
Installing the Database
yum install mariadb-server mariadb -y systemctl start mariadb
At this point we should make sure the DB is secure, to start with run the program ‘mysql_secure_installation’, if it’s not recognised in bash it’s normally located in
/usr/bin/mysql_secure_installation. At the prompt follow the instructions and enter a root password, you’ll also want to remove anonymous users, disable remote root login, remove the test databases and reload privileges to ensure everything is nice and secure. Verify that you can access the database after this is done with
mysql -u root -p, press enter and put your root password in at the prompt. If you get to the MariaDB CLI then everything worked, you can now close it (use an interrupt – hold
ctrl and press
c to return to bash) and run the following command:
systemctl enable mariadb
This will make the service start automatically.
Installing the Ruby Gems and Creating the Application
At this point we should install our gems for Ruby, to start with we will install Rails which is the main application itself, Bundler which will manage our application dependancies, and mysql2, which is a Ruby to mysql connector. Run:
gem install bundler gem install rails gem install mysql2
Now we can move on to actually creating our Rails application! Rails projects exist soley in the directory they are created in, and there are no other references to it. Because of this it’s important you’ve planned where you’re going to put your app before creating it. Also, since Rails uses SQLite by default, but we want to use MariaDB we will inneed to use the ‘-d’ flag, Rails doesn’t have a ‘mariadb’ option for the ‘-d’ flag, but since iMariaDB operates as a mysql drop in replacement we can use that for now. Navigate to the folder you’ve chosen and run:
rails new myapp -d mysql
Provided no errors arise there should now be a folder with the same name as your application, in that folder there should be a directory structure such as this:
The only thing missing is our database schema, Rails will implement this for us, however we need to allow it to talk to the database first. Open the file
database.yml in the config folder and find the block:
Add your password and save the file, after this you can run
rake db:create from within the application folder and it will build the database for you. After it has built the database we should change the DB user from root to a special account we create for this specific application in the interests of security. First of all, log into MariaDB again
mysql -u root -p amd enter your password. After you’re in the MariaDB CLI, enter the command
show databases; this should show you a list of all the databases on the server, typically you will have
performance_schema and two other databases as so:
<application name>)_development and
<application name>_test, these two are the ones we want to give our application DB user permissions on. You can use the following command to create a user, set a password and give them the appropriate permissions (replace
<appname> with your database name prefix and
<password> with your database password).
GRANT ALL PRIVILEGES ON `<appname>_%`.* TO '<appname>'@'localhost' IDENTIFIED BY '<password>'; FLUSH PRIVILEGES;
If there were no errors then this should have been succesful, open up the database.yml config file again and replace root with the username and password you just set, and save it.
Now we can test our application works, as a precaution we can turn off ‘firewalld’ (the CentOS 7 default firewall) with the command
systemctl stop firewalld (unless you’re on a local network do not leave this off, turn it back on after testing). Navigate to your application directory and run ip -a, get the IP address you want the webserver to listen on from the output and use it in this command:
rails server --binding=<IP>
This will start up the Rails webserver (usually this is either Puma or Unicorn) and start waiting for connections on port 3000, if you navigate to
http://<IP>:3000 in a browser or curl it from a command line you should be greeted by the Rails splash screen, this means everything is good and working!
That’s it for now, in the next post we will get started adding some basic visual stuff, and allocating parts of our application.
If you want to know more about the tools we used, try these links: