Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I install the mysql2 gem on ubuntu, when I'm using rails3 via rvm?

I'm trying to figure my way around the vastly complex maze that is rails configuration. So far, I'm managed to set up rvm on ubuntu (for some reason, ruby is outdated in the ubuntu repo's). I've managed to set up a rails project. I want my test project to use mysql rather then mysqlite.

When I tried 'rake db:migrate', I got an error: "!!! Missing the mysql2 gem. Add it to your Gemfile: gem 'mysql2'"

When I try 'gem install mysql', I get an error, telling me that I need to provide parameters to the installation command. However, the list of parameters is huge, and I have no idea which ones to select.

How can I get rails3 via rvm running on ubuntu with mysql ?

Thanks.

like image 311
bob Avatar asked Jun 09 '11 04:06

bob


3 Answers

I had the same problem, all you need to do is to install the libmysqlclient-dev first.

cheers

like image 167
thermosilla Avatar answered Oct 14 '22 08:10

thermosilla


First, you need mysql installed. You can install it using Ubuntu's package manager. No special steps needed. You also need to initially create your database and user using the mysql tool. This link shows how to do that:

http://www.tutorialspoint.com/ruby-on-rails/rails-database-setup.htm

Second, you need to have the mysql2 gem listed in your Gemfile. This tells Rails to go ahead and use that gem. You need a line like this:

gem 'mysql2', '< 0.3'

I'm specifying the version to be less than 0.3 because I'm using Rails 3.0.7 and version 0.3 and higher are for Rails 3.1. Also, be sure to use the mysql2 gem and not mysql - it seems to handle character encoding better.

Third, run "bundle install" so Rails downloads and installs the mysql2 gem.

Lastly, you need to change your database.yml file to put in the connection information for your database like so:

development:
  adapter:  mysql2
  database: your_database_name
  username: your_username
  password: your_password
  encoding: utf8

The encoding part is just what I'm using, you may need something different. This entry tells Rails how to find your database in the development environment.

Once that's all in place, things should work.

like image 30
jdc Avatar answered Oct 14 '22 08:10

jdc


sudo apt-get install libmysql-ruby libmysqlclient-dev

If the above command does not work because libmysql-ruby cannot be found, the following should be sufficient:

sudo apt-get install libmysqlclient-dev

On Red Hat/CentOS and other distributions using yum:

sudo yum install mysql-devel

On Mac OS X with Homebrew:

brew install mysql

then run

bundle install

to install to gems as listed in gemfile

like image 20
Shalto Avatar answered Oct 14 '22 07:10

Shalto