Posts Tagged ‘ruby on rails’

How to install Ruby on Rails 3.2 with MySQL on Debian/Ubuntu
This is the way I managed to install Ruby on rails 3.2 with MySQL on Debian/Ubuntu.

Other articles you may be interested in:
ROR with SQLite3 for Ubuntu 11 .10 (the easiest way to install and use ror)
ROR with SQLite3 for Fedora 15

Pay attention: if you had already installed Ruby on rails with SQLite3 and you just want to add mysql database you can skip some steps, but make sure to install the required libraries.

1. INSTALL THE NEEDED LIBRARIES

Type the following command in the console:

sudo apt-get install zlib1g zlib1g-dev build-essential openssl libssl-dev libmysqlclient18 libmysqlclient-dev libyaml-dev

2. DOWNLOAD AND INSTALL RUBY 1.9.3

Download the last version Ruby 1.9.3 in the “Compiling Ruby” section of the web page
Unzip the archive
Install running:
$ ./configure
$ make
take a coffee
$ sudo make install

Check the successful installation running ruby -v: it should answer with “ruby 1.9.3p…”

3. DOWNLOAD RUBY GEM: THE STANDARD RUBY PACKAGE MANAGER

Download ruby gem version 1.8.25 (rubygems-1.8.25.tgz)
Unzip the archive somewhere
Put the console in the unzipped folder
Install running: $ sudo ruby setup.rb
Check the successful installation running gem -v: it should reply “1.8.25”

4. INSTALL RAILS

Install Rails running on the console: $ sudo gem install rails
(it takes a while, don’t worry)
Check if everything is ok using the commands “rails -v” (you should get “rails 3.2.XX”) or “gem list”

5. INSTALL MYSQL

Install running on the console: $ sudo apt-get install mysql-client-5.5 mysql-server-5.5
During the installer it asks for a mysql root-user password (type for ex. admin)
Try to start and stop the mysql server using the commands:

$ sudo service mysql stop
$ sudo service mysql start

6. INSTALL MYSQL GEM

In order to install the mysql gem you could use:
$ sudo gem install mysql2
$ sudo gem install therubyracer

7. CREATE A NEW RAILS PROJECT

$ rails new yourappname -d=mysql
When you see run bundle install wait some seconds.
Put the console in myapp folder

It could be useful also to run:
$ sudo bundle install

because maybe the project requires other gems (in my case it installed a new version of rake gem)

8. DATABASE SETTINGS ON THE NEW RAILS APPLICATION

Open the file myapp/config/database.yml and put your
mysql root-user password
for the test and development database
You must leave a blank character after the ‘password’ keybord. So you should get something like this:

adapter: mysql2
encoding: utf8
reconnect: false
database: myapp_development
pool: 5
username: root
password: your-password
socket: /var/run/mysqld/mysqld.sock

9. CREATE THE BLANK DATABASE

Start the mysql database using: sudo service mysql start
Put the console in myapp folder and run: $ rake db:create
Check the step 12 if you get an error here.

in order to create the databases myapp_test and myapp_development.
If creates the db is correctly creates, it replies with anything…

It could be useful to check if the db is created or not: do like this:

$ mysql -u root -p // open mysql console
$ Enter password: type-your-password
mysql> show databases;

and you should find the myapp_test and myapp_development db.
mysql> \q // close mysql console

10. RUN YOUR RAILS APP

Put the console in myapp folder and run $ rails server
Open the browser
http://localhost:3000
and you should see the Ruby on Rails: welcome page;-)

I suggest you to continue the ROR learning on the official documentation, in particular on the getting started page.

11. SOME USEFUL MYSQL COMMANDS

$ mysql -u root -p // open mysql console
mysql> show databases; //show all the databases
mysql> use database-name; //select a database
mysql> show tables; //show the tables of the selected db
mysql> describe table-name; //show the attributes of the selected table
If you need to change the mysql root password type this command:
$ mysqladmin -u root -p password enter-your-new-password
Enter password: enter-your-old-password

12. !! COMMON ISSUES THAT MIGHT OCCUR !!

Running rake db:create or rake db:migrate you could get the error: Could not find a JavaScript runtime.
put in the first line of your Rakefile: gem ‘therubyracer’
run sudo bundle install
Running rake db:create or rake db:migrate you could get the error: uninitialized constant Rake::DSL. The solution is to put in the first line of your Rakefile: require ‘rake/dsl_definition’

In the browser at localhost:3000 you could get the error: “no such file to load — openssl”. The solution is to go through the installation directory of ruby: ruby-1.9.2-p180/ext/openssl and run:

ruby extconf.rb

make
sudo make install

 

 

Also read:

Advertisements

Name

bundle-update – Update your gems to the latest available versions

SYNOPSIS

bundle update*gems [–source=NAME]

DESCRIPTION

Update the gems specified (all gems, if none are specified), ignoring the previously installed gems specified in the Gemfile.lock. In general, you should use bundle install(1) to install the same exact gems and versions across machines.

You would use bundle update to explicitly update the version of a gem.

OPTIONS

--source=<name>
The name of a :git or :path source used in the Gemfile. For instance, with a :git source of http://github.com/rails/rails.git, you would call bundle update --source rails

UPDATING ALL GEMS

If you run bundle update with no parameters, bundler will ignore any previously installed gems and resolve all dependencies again based on the latest versions of all gems available in the sources.

Consider the following Gemfile:

source "http://rubygems.org"

gem "rails", "3.0.0.rc"
gem "nokogiri"

When you run bundle install(1) the first time, bundler will resolve all of the dependencies, all the way down, and install what you need:

Fetching source index for http://rubygems.org/
Installing rake (0.8.7)
Installing abstract (1.0.0)
Installing activesupport (3.0.0.rc)
Installing builder (2.1.2)
Installing i18n (0.4.1)
Installing activemodel (3.0.0.rc)
Installing erubis (2.6.6)
Installing rack (1.2.1)
Installing rack-mount (0.6.9)
Installing rack-test (0.5.4)
Installing tzinfo (0.3.22)
Installing actionpack (3.0.0.rc)
Installing mime-types (1.16)
Installing polyglot (0.3.1)
Installing treetop (1.4.8)
Installing mail (2.2.5)
Installing actionmailer (3.0.0.rc)
Installing arel (0.4.0)
Installing activerecord (3.0.0.rc)
Installing activeresource (3.0.0.rc)
Installing bundler (1.0.0.rc.3)
Installing nokogiri (1.4.3.1) with native extensions
Installing thor (0.14.0)
Installing railties (3.0.0.rc)
Installing rails (3.0.0.rc)

Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

As you can see, even though you have just two gems in the Gemfile(7), your application actually needs 25 different gems in order to run. Bundler remembers the exact versions it installed in Gemfile.lock. The next time you run bundle install(1), bundler skips the dependency resolution and installs the same gems as it installed last time.

After checking in the Gemfile.lock into version control and cloning it on another machine, running bundle install(1) will still install the gems that you installed last time. You don’t need to worry that a new release of erubis or mail changes the gems you use.

However, from time to time, you might want to update the gems you are using to the newest versions that still match the gems in your Gemfile(7).

To do this, run bundle update, which will ignore the Gemfile.lock, and resolve all the dependencies again. Keep in mind that this process can result in a significantly different set of the 25 gems, based on the requirements of new gems that the gem authors released since the last time you ran bundle update.

UPDATING A LIST OF GEMS

Sometimes, you want to update a single gem in the Gemfile(7), and leave the rest of the gems that you specified locked to the versions in the Gemfile.lock.

For instance, in the scenario above, imagine that nokogiri releases version 1.4.4, and you want to update it without updating Rails and all of its dependencies. To do this, run bundle update nokogiri.

Bundler will update nokogiri and any of its dependencies, but leave alone Rails and its dependencies.

OVERLAPPING DEPENDENCIES

Sometimes, multiple gems declared in your Gemfile(1) are satisfied by the same second-level dependency. For instance, consider the case of thin and rack-perftools-profiler.

gem "http://rubygems.org"gem "thin"
gem "rack-perftools-profiler"

The thin gem depends on rack >= 1.0, while rack-perftools-profiler depends on rack ~> 1.0. If you run bundle install, you get:

Fetching source index for http://rubygems.org/
Installing daemons (1.1.0)
Installing eventmachine (0.12.10) with native extensions
Installing open4 (1.0.1)
Installing perftools.rb (0.4.7) with native extensions
Installing rack (1.2.1)
Installing rack-perftools_profiler (0.0.2)
Installing thin (1.2.7) with native extensions
Using bundler (1.0.0.rc.3)

In this case, the two gems have their own set of dependencies, but they share rack in common. If you run bundle update thin, bundler will update daemons, eventmachine and rack, which are dependencies of thin, but not open4 or perftools.rb, which are dependencies of rack-perftools_profiler. Note that bundle update thin will update rack even though it’s also a dependency of rack-perftools_profiler.

In short, when you update a gem using bundle update, bundler will update all dependencies of that gem, including those that are also dependencies of another gem.

In this scenario, updating the thin version manually in the Gemfile(7), and then running bundle install(1) will only update daemons and eventmachine, but not rack. For more information, see the CONSERVATIVE UPDATING section of bundle install(1).

In general, when working with an application managed with bundler, you should use the following workflow:

  • After you create your Gemfile for the first time, run$ bundle install
  • Check the resulting Gemfile.lock into version control$ git add Gemfile.lock
  • When checking out this repository on another development machine, run$ bundle install
  • When checking out this repository on a deployment machine, run$ bundle install –deployment
  • After changing the Gemfile(7) to reflect a new or update dependency, run$ bundle install
  • Make sure to check the updated Gemfile.lock into version control$ git add Gemfile.lock
  • If bundle install(1) reports a conflict, manually update the specific gems that you changed in the Gemfile(7)$ bundle update rails thin
  • If you want to update all the gems to the latest possible versions that still match the gems listed in the Gemfile(7), run$ bundle update