Setting up rails has never been known for its difficulty. Fedora being my primary OS, let’s get diving into configure rails with mysql on fedora.
We’ll use the following setup:
- Ruby – 1.9.3-p0
- rails 3.2.0rc1
- mysql 5.5.19
- WEBrick 1.3.1
- Fedora 16 64 bit
So here goes, step by step:
Step 1 , install and configure ruby via RVM:
Really, you aren’t thinking of compiling everything by yourself, are you? Every man and his dog in this nix world uses RVM (Ruby version manager) for ruby and rubygems (Ruby’s gem management system). To install RVM run the setup script with the following command:
user$ bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )
I recommend you issue this as normal user not super user so that the .rvm folder reside in your home. It guarantees easy dispose of it should you ever feel like. Oh, you need gcc, curl , make, git & openssl to compile ruby via RVM . RVM will tell you the dependency and give you a fancy command to execute.
Add RVM to your .bashrc so bash recognizes RVM :
user$ echo ‘[[ -s “$HOME/.rvm/scripts/rvm” ]] && . “$HOME/.rvm/scripts/rvm” # Load RVM function’ >> ~/.bash_profile
Then issue :
user$ rvm install 1.9.3
this will download the source and compile MRI (Matz ruby interpreter) ruby 1.9.3-p0 on your system. For a list of all possible ruby variants available via RVM , issue:
user$ rvm list known
Next up, issue :
user$ rvm default 1.9.2
The reason being, if you installed ruby via the package manager, you will still be on 1.8.* branch which is largely obsolete nowadays. You can check your ruby version with
user$ ruby -v
Step 2, Install rails & mysql2 with all the dependencies:
What good ruby is without rails ;) don’t get me wrong, I intend to pun. With RVM in it’s place , issue:
user$ gem install rails
This will download and install all the necessary gems required to operate rails. Then, issue this command to install mysql adapter which is a sort of interfacing system with mysql and rails :
user$ gem install mysql2
Neat. Note that rails comes with a simple web server called WEBrick. It’s not anywhere near apache , nginx & IIS but it gets the job done. I’ll cover configuring rails with IIS and nginx (I hate apache) later on.
Almost done. Let’s install and connect mysql.
Step 3, Install MySQL :
Install mysql devel, server & libs with the following command:
user# yum install mysql-server mysql-libs mysql-devel
You can setup password for your root MySQL account by issuing the following:
user# mysqladmin -u root -p “password” -h hostname password “password”
Then execute the following command to start MySQL :
user# service mysqld start
Followed by this command to make MySQL start at boot:
user# chkconfig mysqld on
Step 4, Create a sample rails app:
Cd to the folder you want to create your rails app and issue:
user$ rails new “your_app_name” -d mysql
The -d parameter tells it to default to MySQL, if we don’t specify it, it’ll default to sqlite3 . We can still change that, but the earlier , the better.
Let it to its thing and pass in your root password to run bundle command when it prompts.
That’s it. That easy. One last thing, creating and linking a database. But even without it, you can cd to the app and issue:
user$ rails s
then browse “localhost://3000” with your browser of choice to access your rails app.
Step 5, Create a database:
If you have set a password for root already, issue :
user$ mysql -u root -p
to log in. Or else, drop the last -p parameter. Next, logged into MySQL, issue:
mysql>CREATE DATABASE simple_app_development;
You can check with this command:
mysql> SHOW DATABASES;
Now we could log into that database as root. But that’s a very bad habit. So let’s create an user and give it permission to access that. For that you want to do this:
mysql> GRANT ALL PRIVILAGES ON simple_app_development.* TO ‘simple_app’@’localhost’ IDENTIFIED BY ‘password’;
Now check if you succeeded by this:
mysql> SHOW GRANTS FOR ‘simple_app’@’localhost’;
You’ll see something like, “GRANT ALL PRIVILAGES ON ‘simple_app_development’.* TO ‘simple_app’@’localhost’. Congratulation.
Last step, Connect the database:
Cd to the “config” folder under “your_app_name” and open up your “database.yml” with your favorite text editor. Then, under development, change the username with “simple_app” and password with “password”
Done, now save and close. Let’s test it with this command:
user$ rake db:schema:dump
if there isn’t any error, all done. We’ve successfully configured the database with rails.