Ruby on Rails With Apache/MySQL on Mac OS X (Snow Leopard)
January 18th, 2011 | Development , Ruby , Tips and Tricks , Tutorials
Install a Path Line Using Text Editor
Such as TextMate, TextWrangler, BBEdit or vi (My favorite is TextMate and “
mate
” command or just use “vim
” command).
Remove Mysql 5.5 if you have (Mysql 5.1 is the most stable version)
For some reason I faced some problems with MySQL 5.5 so I installed the most stable version : MySQL 5.1 64 Bit .
Use mysqldump to backup your databases to text files! After that :
sudo /usr/local/mysql/support-files/mysql.server stop
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
rm -rf ~/Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm -rf /var/db/receipts/com.mysql.*
mate /etc/hostconfig
Remove the line
MYSQLCOM=-YES-
Download and install Mysql 5.1 [Mac OS X ver. 10.6 (x86, 64-bit), DMG Archive]
http://www.mysql.com/downloads/mysql/5.1.html#downloads
Configure Path , download and Install Macports
mate ~/.bash_profile
Add the following to the end of file and save
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
export MANPATH=/opt/local/share/man:$MANPATH
http://www.macports.org/install.php
(MacPorts-1.9.2-10.6-SnowLeopard.dmg)
Download and Install Ruby
sudo port -v install ruby
Download , Install and Update RubyGems
mkdir ~/src
cd ~/src
curl -O http://production.cf.rubygems.org/rubygems/rubygems-1.3.7.tgz
tar xzvf rubygems-1.3.7.tgz
cd rubygems-1.3.7
sudo ruby setup.rb
sudo gem update --system
Install rails, rake, bundle, rspec etc.
sudo gem install rake rails thin tzinfo capistrano ruby-debug rspec
Install Mysql Gem
sudo env ARCHFLAGS="-arch x86_64" gem install --no-rdoc --no-ri mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
You can use
sudo gem uninstall mysql
to uninstall
Test mysql gem
irb
irb > require 'rubygems'
irb > require 'mysql'
irb > Mysql.new('localhost', 'root', 'password', 'test')
Install Sqlite gem
sudo gem install sqlite3-ruby
Install Passenger gem
sudo gem install passenger
Build passenger for apache
(Just hit enter on prompts)
sudo passenger-install-apache2-module
Include Passenger config file
mate /private/etc/apache2/httpd.conf
Add the following to the end of file and save
Include /private/etc/apache2/extra/httpd-passenger.conf
Create Passenger config file for Apache:
My Passenger version is : 3.0.2 so you should edit the code below if yours is a different version
mate /etc/apache2/extra/httpd-passenger.conf
Add the following to the file and save (Change passenger directory string if your have a different version. Learn it by typing
passenger-config --root
)
LoadModule passenger_module /opt/local/lib/ruby/gems/1.8/gems/passenger-3.0.2/ext/apache2/mod_passenger.so
PassengerRoot /opt/local/lib/ruby/gems/1.8/gems/passenger-3.0.2
PassengerRuby /opt/local/bin/ruby
PassengerMaxPoolSize 6
PassengerMaxInstancesPerApp 2
RailsFrameworkSpawnerIdleTime 1800
RailsAppSpawnerIdleTime 600
PassengerPoolIdleTime 600
PassengerMaxRequests 1000
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "/Users/goksel/Sites/rails_app/public"
ServerName rails_app
<Directory /Users/goksel/Sites/rails_app/public>
Options -MultiViews
AllowOverride All
</Directory>
</VirtualHost>
Add your local domain to hosts:
mate /etc/hosts
Add the following to the end of file and save
127.0.0.1 rails_app
Restart Apache
apachectl configtest
(If you see “Syntax OK” everything is done right)
sudo apachectl stop
sudo apachectl start
Create a Mysql User if you need one
mysql -u root -p
(If you dont have a root password you should create it)
mysql>CREATE USER 'railsuser'@'localhost' IDENTIFIED BY 'pass';
FLUSH PRIVILEGES;
exit
Create a Rails app
cd ~/sites
rails new rails_app
Configure database.yml
cd rails_app
mate config/database.yml
Remove the content and add the following into file
development:
adapter: mysql
database: railsapp_dev
username: root
password: password
host: localhost
test:
adapter: mysql
database: railsapp_test
username: root
password: password
host: localhost
production:
adapter: mysql
database: railsapp_prod
username: root
password: password
host: localhost
Create an empty database
cd ~/sites/rails_app
rake db:create
Clean & simple.
BTW I experienced same issues with 5.5 :-/
Thanks Sebastien!
Remove Mysql 5.5 if you have (Mysql 5.1 is the most stable version)
Is not necesary!!
[1] gem install mysql
[2] you will see like error syntac but dont pay attention to it
[3] find LIBMYSQL.DLL from your mysql/bin folder in your mysql folder
not ruby folder.
[4] copy & paste that file to ruby/bin
[5] restart your server or console, if you are already run it.
http://www.ruby-forum.com/topic/150580#664497
dll?
this is mac osx installation dude!
Same issuses with MySQL 5.9 on macosx 10.6 … tried everything to make ruby 1.8.7 + mysql gem and ruby 1.9.2 with mysql2 gem running …
FINALLY THIS HELPED.
thanks
Glad that it helped bro…
This is awesome. You are awesome. Thanks. I have been trying to get this working for 5 hours and finally got it by following your detailed instructions.
Hi i tried so many time. i got this error. Please help me.
ERROR: Error installing mysql:
ERROR: Failed to build gem native extension.
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb –with-mysql-dir=/usr/local/mysql
extconf.rb:4: warning: Insecure world writable dir /usr/local in PATH, mode 040777
extconf.rb:4: warning: Insecure world writable dir /usr/local in PATH, mode 040777
extconf.rb:7: warning: Insecure world writable dir /usr/local in PATH, mode 040777
extconf.rb:17: warning: Insecure world writable dir /usr/local in PATH, mode 040777
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lm… yes
…
Did you check this page ?:
http://stackoverflow.com/questions/2167598/mysql-install-error-failed-to-build-gem-native-extension
Perhaps I’m just showing my newbie feathers, but after following these directions I was still getting
> rake db:create
(in /Users/jrepenning/stats/codesion/blog)
db/test.sqlite3 already exists
rake aborted!
uninitialized constant Mysql2
I got past that by adding
gem ‘mysql2’
to Gemfile.
All adapter fields should be set as mysql in database.yml
development:
adapter: mysql
database: railsapp_dev
username: root
password: password
host: localhost
test:
adapter: mysql
database: railsapp_test
username: root
password: password
host: localhost
production:
adapter: mysql
database: railsapp_prod
username: root
password: password
host: localhost
I followed the instructions above but I get an error at rake db:migrate:
“Could not find gem ‘sqlite3 (>= 0)’ in any of the gem sources listed in your Gemfile.”
I have SQLite version 3 so I’m unsure what I’m missing.
Thanks in advance for your help!
Did you
sudo gem install sqlite3-ruby
?.Check out this link
Hi…
First: Thank you for this tutorial.
Second: I have a problem with “irb > Mysql.new(‘localhost’, ‘root’, ‘password’, ‘test’)”. It always tells me: “Mysql::Error: Access denied for user ‘root’@’localhost’ (using password: YES)”
Do you have any idea how I can solve this problem? mysql server is running.
Regards,
Tobias
That means you successfully installed everything Tobias. You just need your mysql root password. Check out this link.
Ok, thanks for this Post. I hope I’ll finally be able to create my first RoR test app.
I’m getting problems at:
$ rake db:create
(in /Users/diego/Sites/rails_app)
rake aborted!
uninitialized constant Mysql
What am I missing here?
…still not being able to run rake db:create went to http://127.0.0.1 to see the app, then clicked on my app environment link and got this:
!!! Missing the mysql2 gem. Add it to your Gemfile: gem ‘mysql2’
Rails.root: /Users/diego/Sites/rails_app
May you provide instructions on mysql2 gem?
Diego, you got to install mysql gem with the right flags.
sudo env ARCHFLAGS="-arch x86_64" gem install --no-rdoc --no-ri mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
Thanks for the prompt answer, I uninstalled the mysql gem (using the posted command) and then used the given line… same result.
I followed all the instructions given on this entry (even uninstalling Mysql 5.5 which I had) but still missing something.
Can you check if your mysql version is 5.1
sudo mysql --version
It seems the problem is with mysql, I installed mysql version 5.1.56 x86_64 (I checked that mysql server instance is running) but:
$ sudo mysql –version
sudo: mysql: command not found
Then something went wrong while you are installing mysql.
Ok, mysql installation seem to be ok. I believe the problem is with the path… if I enter:
$ /usr/local/mysql-5.1.56-osx10.6-x86_64/bin/mysql –version
I get:
/usr/local/mysql-5.1.56-osx10.6-x86_64/bin/mysql Ver 14.14 Distrib 5.1.56, for apple-darwin10.3.0 (i386) using readline 5.1
So, I’ll need to find out how not to need to write the whole path to execute mysql commands.
Any ideas?
Diego, sorry for late response. Did you try to install mysql gems with different flags?
Awesome thanks – I was having loads of problems and downgrading from Mysql 5.5 to 5.1 fixed it!
Many thanks
Jonno
Thanks a lot !!
previously I stuck at mysql….
I step through your instruction….
its working fine….
Thanks Pradeep!
Hey – Thank you for this! I couldn’t get MySQL 5.5 to work either no matter what I tried on snow leopard. I read about the archflags and config path options stuff on other websites, but it didn’t work for me. MySQL 5.1 finally did. Cheers!
Glad that it helped Greg!
You just saved my day! Thank you! I was having the same issues with MySQL 5.5!
Thanks man!
Respect to you!
It works.
Downgrading from MySQL 5.5 to 5.1 was the key for me. Thanks!
Worked for me too. Thanks! You need to put a paypal tip jar on your site. I’m sure folks like me would be plenty happy to show their gratitude.
Glad that it helped Mike!
Thank you awesome 🙂
Dude, you save me! after 5hs I got here and it works!, thanks!
Glad that I helped man!
gem install rails -v 3.2.3