June 24, 2013

Beginner

How To 1-Click Install Ruby on Rails on Ubuntu 12.10 with DigitalOcean

We have created a one-click Rails installation that uses Nginx with Unicorn on Ubuntu 12.10 x64.

It is available from the Applications menu during droplet creation:



We went ahead and named our droplet RailApp.


Once you create the droplet, navigate to your droplet's IP address (http://IP), and verify that you have Rails running:


You should login to your droplet as root and read the Message of the Day, which contains important information about your installation:


Nginx


This application uses Nginx, which accepts incoming connections on port 80.
You can modify /etc/nginx/sites-enabled/default to customize the default configuration, add SSL certificates, and add other upstream servers:


Once Nginx receives a request to port 80, it serves the static content such as images, and forwards the request to Unicorn, which listens on localhost's port 8080.

Unicorn


Unicorn is used to serve Ruby on Rails applications and looks for files in /home/rails.

We added a minimal Unicorn configuration in /home/unicorn/unicorn.conf which specifies which port to listen to and where the log files and working directory are located:


The other important configuration file is in /etc/default/unicorn and specifies the configuration file location and, more importantly, Ruby's path:

Ruby Version Manager


Since some Rails applications may require different Ruby versions, we have used Ruby Version Manager (rvm) to install Ruby.
This allows you to have multiple Ruby versions installed on your server that are then used by different applications.

Ruby 2.0.0 is used as the default version.
If your application requires a different version of Ruby, for example 2.0.0, you can install it using RVM:
rvm install 2.0.0-p195



Modify your default Ruby version:
rvm use ruby-2.0.0-p195 --default
In this case, you would also need to add your Ruby file to Unicorn's path (/etc/default/unicorn). Find out where Ruby is installed by running which ruby:
which ruby



Now modify your PATH in /etc/default/unicorn:
PATH=/usr/local/rvm/rubies/ruby-2.0.0-p195/bin:/home/unicorn/.rvm/bin:/usr/local/sbin:/usr/bin:/bin:/sbin:$

Subsequently, install Rails and any other required gems for this new Ruby version:
gem install rails bundler unicorn rake i18n

Install any other dependencies specified in your Gemfile:
bundle install

You can find out which Gems you are missing by monitoring Unicorn's log file for lines like this:
Could not find i18n-0.6.1 in any of the sources (Bundler::GemNotFound)

Use the following command to continuously monitor the log output:
tail -f /home/unicorn/log/unicorn.log

If you install a new Gem, always be sure to restart Unicorn:
service unicorn restart

Modifying Rails Environment


By default, rails operates in a production environment, which can be changed by modifying /etc/default/unicorn and replacing -E environment on UNICORN_OPTS line.

For example, to change to development environment:
UNICORN_OPTS="-D -c $CONFIG_RB -E development"


You should restart Unicorn after making configuration changes:
service unicorn restart


Uploading Files


We recommend that you use SFTP (Secure FTP), SCP, or Rsync to upload your files, as they all use port 22 for encrypted transmission of data.

Windows users can upload files with WinSCP. For all other operating systems (including Windows) you can use FileZilla and Rsync.

For seasoned developers, we have an article on how to setup Git.

You should have your Rails application in /home/rails; Nginx and Unicorn will look for public pages in /home/rails/public — be sure to upload your files using SFTP accordingly:


Your SFTP credentials are provided in the Message of the Day (credentials are different and randomly generated for every droplet):
-------------------------------------------------------------------------------------
You can use the following SFTP credentials to upload your webpages (using FileZilla/WinSCP/Rsync):
Host: 192.241.196.63
User: rails
Pass: Wmy7hbTTuQ
-------------------------------------------------------------------------------------

Database Credentials

The one-click installation has also installed a MySQL database for you to use (credentials are different and randomly generated for each droplet):
-------------------------------------------------------------------------------------
The following MySQL database has been created for you to use with Rails:
Host: localhost
Database: rails
Username: rails
Password: LwsOH0ycxY
-------------------------------------------------------------------------------------

The One click Rails application allows you to quickly get started on your Rails application.

Leave a comment below, and let us know how we can improve it further.

Share this Tutorial

Vote on Hacker News

Try this tutorial on an SSD cloud server.

Includes 512MB RAM, 20GB SSD Disk, and 1TB Transfer for $5/mo! Learn more

Create an account or login:

76 Comments

Write Tutorial
  • Gravatar josh 10 months

    Adding postgres to that one-click solution would be pretty awesome!

  • Gravatar Enes Kaya 10 months

    I noticed that the rails app instance runs in the development environment. Isn't this a security issue? Maybe it should automatically start in the production environment! How do I change this manually?

  • Gravatar josh 10 months

    Enes,

  • Gravatar josh 10 months

    Well that was weird. Enes, you may want to take a look at this railscast to get a better understanding of Unicorn and Nginx and how they relate to your Rails app. http://railscasts.com/episodes/293-nginx-unicorn

  • Gravatar bulat 10 months

    Enes, you can add "-E production" to UNICORN_OPTS in /etc/default/unicorn and restart Unicorn (service unicorn restart): UNICORN_OPTS="-D -c $CONFIG_RB -E production"

  • Gravatar Marcelo Gonçalves 10 months

    +1 for PostgreSQL!

  • Gravatar altuzar 10 months

    Hi! Didn't find the MySql password for 'root' user. Can you add the password somewhere in the login message? This image is the best for Rails. Thanks!

  • Gravatar Kamal Nasser 10 months

    @altuzar: See if you can find it in /root/.my.cnf - I believe it's there.

  • Gravatar altuzar 10 months

    @Kamal I'll look for it next time. I just uninstalled mysql-server and deleted all those directories like /etc/mysql and /var/lib/mysql and reinstalled it from scratch.

  • Gravatar sankalpk 10 months

    If I add -E production to UNICORN_OPTS, I get "We're sorry, but something went wrong." when I visit the server. How do I get the app to run in production?

  • Gravatar Kamal Nasser 10 months

    @sankalpk: Check your error logs - do you see any errors?

  • Gravatar gbloodworth 9 months

    Yeah - one more vote for Postgres !!

  • Gravatar alex.lafroscia 9 months

    @sankalpk: I'll bet it is an issue with your assets being precompiled (or rather, not being precompiled). I had the same problem, and that's what it was for me.

  • Gravatar anstak 9 months

    Thank you! It's amaizing! I am novice in RoR, and you make really great thing!

  • Gravatar karangb 8 months

    I was wondering, how does nginx find the unicorn app? Is it via the proxy_pass http://app_server ? If so, where is app_server defined? Thanks!

  • Gravatar Kamal Nasser 8 months

    @karangb: Yes. app_server is defined as an upstream. I think it's in /etc/nginx/nginx.conf.

  • Gravatar Francis Brunelle 8 months

    +1 for Postgres!!!

  • Gravatar cwilliams.allancraig 8 months

    +1 for Postgres

  • Gravatar erdoss 8 months

    +1 for Postgres

  • Gravatar Viktor Prevaric 8 months

    Hi, how can I have multiple domains, I have problem with upstream app_server, no matter what domain I configure it always looks in /home/rails (because of upstream app_server directive, I think).

  • Gravatar Kamal Nasser 8 months

    @Viktor: If you want to host multiple domains on one droplet, you'd have to create additional nginx/unicorns configs for each domain, you'd also have to launch a separate unicorn process that listens on another port and not 8080 for each domain so you will need a separate service to launch the second process. The easiest way would be creating a separate droplet for the second domain.

  • Gravatar rrodriguez 8 months

    Where can i find my mysql credential ?? i get this error /Users/fred/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.6/lib/mysql2/client.rb:37:in `connect': Access denied for user 'root'@'localhost' (using password: NO) (Mysql2::Error)

  • Gravatar Kamal Nasser 8 months

    @rrodriguez: The MySQL credentials are in the MOTD. The MOTD is the piece of text that is output once you log in to your droplet.

  • Gravatar Troy Martin 8 months

    +1 for Postgres

  • Gravatar abachuk 7 months

    after I replaced default rails 3 with rails 4, I can access the app only on port 3000, san't access on port 80. I restarted nginx, unicorn, droplet, still not working, what am I missing? thanks.

  • Gravatar Kamal Nasser 7 months

    @abachuk: Do you get any errors when you try to access it on port 80? Is nginx running?

    sudo netstat -plutn | grep :80
    sudo service nginx restart

  • Gravatar abachuk 7 months

    I tried `sudo netstat -plutn | grep :80 sudo service nginx restar` still the same, do I need to start rails on rails s -p 80 ? I see nginx 504 error "504 Gateway Time-out"

  • Gravatar latimerscope 7 months

    What if am using postgres will this work for me. Or i have to do it from scratch.

  • Gravatar Kamal Nasser 7 months

    @latimerscope: You can then uninstall mysql

    sudo apt-get remove mysql-server
    and install Postgres instead: https://www.digitalocean.com/community/articles/how-to-install-and-use-postgresql-on-ubuntu-12-04

  • Gravatar abachuk 7 months

    I was able to to figure it out, now only "public" folder html pages working only on port 80 after I start rails in production environment (rails s -e production) domain.com:3000/static_pages/home -works domain.com/static_pages/home -doesn't work domain.com/index.html -works

  • Gravatar Kamal Nasser 7 months

    @abachuk: Please pastebin your nginx config

  • Gravatar superadmin 7 months

    Can i change the unicorn to passenger, after 1 click installation. and also the mySQL to postgreSQL, is there any risk uninstalling then installing the new rails/sql server?

  • Gravatar Kamal Nasser 7 months

    @superadmin: You certainly can but I usually recommend staying with the defaults or at least taking a snapshot of your droplet in a working state so that if you ever break anything you would still be able to restore it from a working snapshot.

  • Gravatar latimerscope 7 months

    + 1 for postgres one click installer. Its just crazy hard to get it working with rails .

  • Gravatar me.programmers 6 months

    + 1 too for postgres.

  • Gravatar ghugon 6 months

    +1 for postgres. Then, any tip on how to run more than one rails application with unicorn? How can I do that? Help much appreciated.

  • Gravatar mklintmalm 6 months

    ++ for Postgres ... Anyone listening?

  • Gravatar Kamal Nasser 6 months

    Re: postgres, please see my comment above (Posted September 7th, 2013 15:18).

  • Gravatar [email protected] 6 months

    i ran "service unicorn restart" and i get following error. * Restarting Unicorn web server unicorn start-stop-daemon: warning: failed to kill 24100: No such process master failed to start, check stderr log for details how do i fix this?

  • Gravatar Kamal Nasser 6 months

    @[email protected] What's the output of

    tail /home/unicorn/log/unicorn.log
    ?

  • Gravatar jon 6 months

    rvm as root? rails app in home directory? building my own from scratch.

  • Gravatar harrisrobinkalash 6 months

    I have followed the tutorial and installed all the gems, however I keep on getting this whenever I try to bundle install : root@montrealfixed:~# bundle install Bundler::GemfileNotFound Here is the output of my log I, [2013-11-01T16:37:37.842833 #3929] INFO -- : worker=0 spawning... I, [2013-11-01T16:37:37.845118 #3929] INFO -- : worker=1 spawning... I, [2013-11-01T16:37:37.847094 #3929] INFO -- : master process ready I, [2013-11-01T16:37:37.852229 #3932] INFO -- : worker=0 spawned pid=3932 I, [2013-11-01T16:37:37.859884 #3934] INFO -- : worker=1 spawned pid=3934 I, [2013-11-01T16:37:37.869478 #3932] INFO -- : Refreshing Gem list I, [2013-11-01T16:37:37.876426 #3934] INFO -- : Refreshing Gem list I, [2013-11-01T16:37:41.489375 #3934] INFO -- : worker=1 ready I, [2013-11-01T16:37:41.490536 #3932] INFO -- : worker=0 ready 96.127.229.50 - - [01/Nov/2013 16:38:35] "GET /rails.png HTTP/1.0" 304 - 0.4569 I even tried refreshing unicorn.

  • Gravatar teamriab 6 months

    @urchin2000, try killing ruby processes first, then restart unicorn: killall ruby service unicorn restart

  • Gravatar archiwista 6 months

    Hey guys, I'm trying to use something like this in Bundle ``` gem 'spree', github: 'spree/spree', branch: 'master' ``` and I get this error: ``` ERROR -- : git://github.com/spree/spree.git (at master) is not checked out. Please run `bundle install` (Bundler::GitError) ``` For Spree is very important to use ``` branch ``` so "gem install spree" for me is useless. What must I do to be able to install gems via github and branch?

  • Gravatar archiwista 6 months

    ...in Gemfile of course

  • Gravatar Kamal Nasser 6 months

    @archiwista: See if http://stackoverflow.com/questions/5022779/what-does-this-error-mean helps.

  • Gravatar alexandrebournier 5 months

    If I remove the default app, and want to create a new one, I have not the rights: /home$ rails new app create /usr/local/rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/fileutils.rb:245:in `mkdir': Permission denied - /home/app (Errno::EACCES) I added sudo before but doesn't work. What do to ?

  • Gravatar Kamal Nasser 5 months

    @alexandrebournier: You shouldn't remove the default app. Just replace the default app's files with the new app's files.

  • Gravatar alexandrebournier 5 months

    OK. I have created a new droplet. Thanks. Another problem: I'm logged with my username, not root (but same rights). If I want to install ruby 1.8.7, I need to be sudo else I have an access denied. So I did: rvmsudo install 1.8.7 And I got: install: missing destination file operand after `1.8.7' Any idea ?

  • Gravatar chris 5 months

    After editing the /etc/default/unicorn as stated above, then reinstalling gems now that i have ruby 2.0.0 being used, I cannot start unicorn. I get the message cannot load such file -- unicorn/launcher (LoadError) I checked the unicorn path and it appears right and matches "which ruby", and I have reinstalled the gem which is all I could find on google regarding this

  • Gravatar IT Cinema 5 months

    For the various issues above, that people seem to be having: the incorrect ruby version loaded or "Bundler::GemfileNotFound try to load the correct bash profile. Run this: /bin/bash --login Afterwards use rvm to set the correct ruby version. You can run for example: rvm use 2.0.0-p15 To find available versions that are installed run: rvm list

  • Gravatar willjbarker 5 months

    Needs postgres

  • Gravatar Kamal Nasser 5 months

    @willjbarker: Please see my comment above (Posted September 7th, 2013 15:18).

  • Gravatar nahtnam 5 months

    Hey guys. Tutorial worked great for me, but if you are having issues at the end, follow these steps. 1. "cd /home/rails" 2. "rm default/index.html" It should work after that!

  • Gravatar descovi 5 months

    @chris I solved your problems with this solution http://stackoverflow.com/questions/19188846/rvm-system-wide-install-warning-path-is-not-properly-set-up/19189111#19189111 i've added to PATH of config file /usr/local/rvm/bin:$PATH

  • Gravatar Adrian Rangel 5 months

    i tried upgrading to ruby-2.0.0 using rvm it did install but unicorn doesn't work i did change the path at /etc/default/unicorn

  • Gravatar Kamal Nasser 5 months

    @Adrian: Did you try rebooting your droplet?

  • Gravatar idejuan 5 months

    Hello, I would like to have a replica of this stack in my local computer so I can use it while developing. Is it possible? Does it exist as a Vagrant box? I have seen that it is possible to deploy it in the cloud using Vagrant, but I would need to run it locally. Thanks

  • Gravatar Kamal Nasser 5 months

    @ignaciodejuan: Unfortunately we do not offer a Vagrant image of this one click application image.

  • Gravatar idejuan 5 months

    I have updated the ruby version in rvm to 2.0.0, and since I did it I cannot run Unicorn. I have the exact same problem when I try to start Unicorn server Starting Unicorn web server unicorn /usr/local/rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- unicorn/launcher (LoadError) from /usr/local/rvm/rubies/ruby-2.0.0-p353/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /usr/bin/unicorn:3:in `' Nevertheless, Unicorn works fine when I run it from the rails app folder (/home/rails), simply using "unicorn" command. I have tried installing again the gem unicorn gem install 'unicorn' But it does not change the result I check what gem is installed: which gem 'unicorn' /usr/local/rvm/rubies/ruby-2.0.0-p353/bin/gem /usr/local/rvm/gems/ruby-2.0.0-p353/bin/unicorn There seems to be some problem with the PATH of the default configuration file (/etc/default/unicorn): PATH=/usr/local/rvm/rubies/ruby-2.0.0-p353/bin:/home/unicorn/.rvm/bin:/usr/local/sbin:/usr/bin:/bin:/sbin:$ Because since I updated it with the new installed version I get the error message shown at the top. If I remove it, Unicorn "apparently starts" but the log shows a crazy activity and nginx remains stalled until timeout, just showing an empty screen in the browser. Thanks for your help

  • Gravatar david.rossellat 5 months

    yes, same problem as ignaciodejuan - running unicorn_rails -E production however seems to do the trick for me

  • Gravatar idejuan 5 months

    @david.rossellat ok, thanks, I understand you are running the unicorn command from the rails app home directory (/home/rails by default), right? I have also achieved it that way. You can also put -D to detach it off the console. The only issue then is that if you restart the server you will have to manually run that command. You can also run it as a daemon at start. But then it is like rebuilding a bit what is already done... this is why I would like to fix the original solution made by the DO team. I keep on searching a solution.

  • Gravatar idejuan 5 months

    ok! issue is fixed now. You need to create a wrapper: rvm wrapper 2.0.0 ruby-2.0.0 unicorn then replace the line DAEMON=/usr/bin/unicorn with this one: DAEMON=/usr/local/rvm/bin/ruby-2.0.0_unicorn And now the command service unicorn start works nicely! more on rvm wrapper here: https://rvm.io/integration/init-d

  • Gravatar david.rossellat 5 months

    you're a genius. this works. thanks a lot @ idejuan

  • Gravatar sergey.alekseev.minsk 4 months

    @idejuan, thanks a lot!

  • Gravatar Adrian Rangel 4 months

    Thanks idejuan !!

  • Gravatar nahtnam 4 months

    @idejuan What file is the `DAEMON=/usr/bin/unicorn` located in? Im so close yet so far :(

  • Gravatar nahtnam 4 months

    Nvm, I found it.

  • Gravatar bulat 4 months

    Thank you for your suggestions, we have updated the 1-click Rails Application and this article :)

  • Gravatar nahtnam 4 months

    No problem. Why not update the tutorial for ruby 2.0.0-p353 and rails 4.0.2. Also you should add 'rvm rubygems current' before installing rails so you get the latest rubygems for the default ruby.

  • Gravatar Adrian Rangel 3 months

    FYI. to set a env variable it's in /etc/default/unicorn: export SECRET_TOKEN=4gdjfkadjflakdfa

  • Gravatar maximski 2 months

    Is there somewhere a preset unicorn init shell script in this rails image?

  • Gravatar timothy 2 months

    Found out today that DO updated their image to default to Ruby 2.0.0 In order to get 1.9.3 or something else working, I also found I had to open /etc/init.d/unicorn and cook these lines: export GEM_HOME=/usr/local/rvm/gems/ruby-2.0.0-p353 export GEM_PATH=/usr/local/rvm/gems/ruby-1.9.3-p484:/usr/local/rvm/gems/ruby-1.9.3-p484/gems:/usr/local/rvm/gems/ruby-1.9.3-p484@global/gems DAEMON=/usr/local/rvm/gems/ruby-2.0.0-p353/bin/unicorn you have to leave the unicorn and GEM_HOME alone, but the GEMPATH you have to change to your ruby version. It seemed that doing it all in my ruby version was giving me crazy errors. Would appreciate some better clarification on this from DigitalOcean… this was really wonky to have to figure out

  • Gravatar dave about 1 month

    I have just created a new droplet using this installation - what would be the steps to upload an app I have on my local Windows machine ? Thanks, Dave

  • Gravatar John C. Bland II about 1 month

    The nginx in use is pretty old. It uses 1.2.1.

  • Gravatar g.lazaridis 22 days

    This might be obvious (please note I'm still a spring chicken) but could it be that most people are getting an error: Bundler::GemfileNotFound when at step: Install any other dependencies specified in your Gemfile: bundle install because they are not running bundle install from their /home/rails directory (i.e. where the gemfile is located) presumably entering first: cd /home/rails and then running bundle install should solve the issue

Leave a Comment

Create an account or login:
Ajax-loader