Setup Ruby On Rails on
Ubuntu 13.04 Raring Ringtail
A guide to setting up a Ruby on Rails development environment
This will take about 30 minutes.
We will be setting up a Ruby on Rails development environment on Ubuntu 13.04 Raring Ringtail.
The reason we're going to be using Ubuntu is because the majority of code you write will run on a Linux server. Ubuntu is one of the easiest Linux distributions to use with lots of documentation so it's a great one to start with.
You'll want to download the latest Desktop version here: http://releases.ubuntu.com/13.04/
Some of you may choose to develop on Ubuntu Server so that your development environment matches your production server. You can find it on the same download link above.
Choose the version of Ruby you want to install:
The first step is to install some dependencies for Ruby and Rails.
To make sure we have everything necessary for Webpacker support in Rails, we're first going to start by adding the Node.js and Yarn repositories to our system before installing them.
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list sudo apt-get update sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev nodejs yarn
Next we're going to be installing Ruby using one of three methods. Each have their own benefits, most people prefer using rbenv these days, but if you're familiar with rvm you can follow those steps as well. I've included instructions for installing from source as well, but in general, you'll want to choose either rbenv or rvm.
Choose one method. Some of these conflict with each other, so choose the one that sounds the most interesting to you, or go with my suggestion, rbenv.
rbenv is a simple two step process. First you install
rbenv, and then
cd git clone https://github.com/rbenv/rbenv.git ~/.rbenv echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(rbenv init -)"' >> ~/.bashrc exec $SHELL git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc exec $SHELL rbenv install 2.5.1 rbenv global 2.5.1 ruby -v
The installation for
rvm is pretty simple:
sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB curl -sSL https://get.rvm.io | bash -s stable source ~/.rvm/scripts/rvm rvm install 2.5.1 rvm use 2.5.1 --default ruby -v
Arguably the least useful Ruby setup for development is installing from source, but I thought I'd give you the steps anyways:
cd wget http://ftp.ruby-lang.org/pub/ruby/2.5/ruby-2.5.1.tar.gz tar -xzvf ruby-2.5.1.tar.gz cd ruby-2.5.1/ ./configure make sudo make install ruby -v
The last step is to install Bundler
gem install bundler
rbenv users need to run
rbenv rehash after installing bundler.
We'll be using Git for our version control system so we're going to set it up to match our Github account. If you don't already have a Github account, make sure to register. It will come in handy for the future.
Replace my name and email address in the following steps with the ones you used for your Github account.
git config --global color.ui true git config --global user.name "YOUR NAME" git config --global user.email "[email protected]" ssh-keygen -t rsa -b 4096 -C "[email protected]"
The next step is to take the newly generated SSH key and add it to your Github account. You want to copy and paste the output of the following command and paste it here.
Once you've done this, you can check and see if it worked:
ssh -T [email protected]
You should get a message like this:
Hi excid3! You've successfully authenticated, but GitHub does not provide shell access.
Choose the version of Rails you want to install:
To install NodeJS, we're going to add it using the official repository:
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - sudo apt-get install -y nodejs
And now, without further adieu:
gem install rails -v 5.2.0
If you're using rbenv, you'll need to run the following command to make the rails executable available:
Now that you've installed Rails, you can run the
rails -v command to make sure you have everything installed correctly:
rails -v # Rails 5.2.0
If you get a different result for some reason, it means your environment may not be setup properly.
Setting Up MySQL
Rails ships with sqlite3 as the default database. Chances are you won't want to use it because it's stored as a simple file on disk. You'll probably want something more robust like MySQL or PostgreSQL.
There is a lot of documentation on both, so you can just pick one that seems like you'll be more comfortable with. If you're coming from PHP, you may already be familiar with MySQL. If you're new to databases, I'd suggest skipping to setting up PostgreSQL.
You can install MySQL server and client from the packages in the Ubuntu repository. As part of the installation process, you'll set the password for the root user. This information will go into your Rails app's
database.yml file in the future.
sudo apt-get install mysql-server mysql-client libmysqlclient-dev
libmysqlclient-dev gives you the necessary files to compile the
mysql2 gem which is what Rails will use to connect to MySQL when you setup your Rails app.
When you're finished, you can skip to the Final Steps.
Setting Up PostgreSQL
For PostgreSQL, we're going to add a new repository to easily install a recent version of Postgres.
sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main' > /etc/apt/sources.list.d/pgdg.list" wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add - sudo apt-get update sudo apt-get install postgresql-common sudo apt-get install postgresql-9.5 libpq-dev
The postgres installation doesn't setup a user for you, so you'll need to follow these steps to create a user with permission to create databases. Feel free to replace
chris with your username.
sudo -u postgres createuser chris -s # If you would like to set a password for the user, you can do the following sudo -u postgres psql postgres=# \password chris
And now for the moment of truth. Let's create your first Rails application:
#### If you want to use SQLite (not recommended) rails new myapp #### If you want to use MySQL rails new myapp -d mysql #### If you want to use Postgres # Note that this will expect a postgres user with the same username # as your app, you may need to edit config/database.yml to match the # user you created earlier rails new myapp -d postgresql # Move into the application directory cd myapp # If you setup MySQL or Postgres with a username/password, modify the # config/database.yml file to contain the username/password that you specified # Create the database rake db:create rails server
You can now visit http://localhost:3000 to view your new website!
Now that you've got your machine setup, it's time to start building some Rails applications.
If you received an error that said
Access denied for user 'root'@'localhost' (using password: NO) then you need to update your config/database.yml file to match the database username and password.
when installing ruby using rbenv, at the step of "rbenv install 2.0.0-p195", it shows the results below:
Inspect or clean up the working tree at /tmp/ruby-build.20130612202212.21299
Results logged to /tmp/ruby-build.20130612202212.21299.log
Last 10 log lines:
checksum mismatch: ruby-2.0.0-p195.tar.gz (file is corrupt)
expected 0672e5af309ae99d1703d0e96eff8ea5, got 9c6ff20645c3da426ade1bceb2e696b9
Looks like the ruby 2.0 tarball is corrupt. You can try downloading the ruby 2.0 source tar.gz file and saving it to ~/.rbenv/cache like they suggest here: http://stackoverflow.com/qu...
Let me know if that works for you! It could also be curl causing a problem or a network issue.
Thanks Chris, but as I am new to setting up environments on linux, I couldn't figure out what to do to install ruby 2 from source. I installed ruby 1.9.3 as I will continue with the tutorial at : http://ruby.railstutorial.o...
Thanks for help! :)
Hello and thank you very much for your guide!
I followed all the steps mentioned on my ubuntu 13.04 amd64 laptop.
it seems everything installs correctly, but when I get to "rails -v" to check if rails is installed I get the following:
The program 'rails' can be found in the following packages:
Try: sudo apt-get install <selected package="">
I have done some research but I am not sure what this is. if you have any suggestion I would appreciate it, and thank you anyway!
Did you use rbenv? Make sure you run
rbenv rehash if you did, this command makes sure that the executables it installs are available to your user.
Nice site, thanks.
It would be pretty awesome if you would put it on GitHub so we can do a pull requests ;)
ruby-build can be installed in one line like this:
git clone https://github.com/sstephen... ~/.rbenv/plugins/ruby-build
instead of creating and cd-ing into directory
Had a problem following the "Using rbenv" section. 'rbenv install' didn't work as it wasn't included in the plugins dir being cloned from GitHub to Ubuntu (at least for me).
Might want to add '/ruby-build/' to that command (or as the next line) to ensure that install does work.
Un excelente tutorial, aunque deberias mencionar que hay que crear las base de datos, o bueno no se si ahora es necesario crearlas y configurarlas myapp_test; myapp_production; myapp_development ya sea para mysql o psql......
En definitiva gracias por el tutorial excelente aporte para las personas que estamos comenzando en este mundo de rails
Following the instructions to a "T" ended with me getting Rails 4.0. Not sure, but I have had a heck of a time getting either Postgres or MySQL apps running. I am in an endless loop of errors after starting the rails server and visiting the localhost:3000 site.
Does Rails 4 have a different setup for MySQL than what is above?
I know the feeling. :) So if you go with MySQL, you need to update your database.yml to use the "mysql2" adapter and install the "mysql2" gem with your Gemfile. By default it's going to use sqlite3. You should be able to run fine with sqlite3 so it shouldn't be having any errors. Does your log/development.log file contain any info that might help debug what's wrong?
Hey Chris...sorry about the late reply, but I was able to get everything going after changing from Rails 4 to Rails 3. Go figure. :)
This is pretty exciting because I'm working on my first application of any kind since college. Forgot how much I liked programming. Your post on multiple databases and migrations is helping me through my next little road bump.
Keep up the great work with this site and excid3!
That's fantastic to hear! :) Programming really is a blast and I'm glad you got Rails 3 going. You'll be able to upgrade it later on fairly easily anyways. Good luck with everything and let me know if you have any more questions!
I followed the steps but when i run ruby file in geany or scite editors in linux it does not run. Error message like ruby not found. It will run if i use the terminal to run the ruby file. help please?
I think it's likely that the editors are looking for the Ruby executables. rbenv and rvm both install hooks into the shell so that you can switch between active Ruby versions. The editors most likely don't execute your shell configuration so it doesn't find them. My suggestion in this case is to choose the version of Ruby you're planning on using and find their binary inside either the .rbenv or .rvm folders inside your home directory. That way it can find the executable directly without having to jump through one of the managers.
Thanks for one of the most straightforward, current, simple and complete guides. Just one thing as it seems rails has moved to 4.0.0 is that I had to install bundler and update. Now "rake server" is working with Rails 4.0.0 and mysql2:
gem install bundler && bundle update
I encourage you to update your guide for 4.0.0 :-)
rbenv install 2.0.0-p247
Inspect or clean up the working tree at /tmp/ruby-build.20130814225651.28393
Results logged to /tmp/ruby-build.20130814225651.28393.log
Last 10 log lines:
installing default gems: /home/piavgh/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0 (build_info, cache, doc, gems, specifications)
The Ruby openssl extension was not compiled. Missing the OpenSSL lib?
After "rbenv install 2.0.0-p247" I get this. Did I miss something?
It sounds like you're missing the openssl lib and that's causing it to fail. Can you check to make sure you have libssl-dev installed?
Thanks so much. I was using rvm usually. But rbenv is excellent. no errors. Mine is a chinese laptop worth only 160 euros including shipping. I am using ubuntu 12.04 lts. My earlier trials for instling rails 4 and ruby 2 ended up unsuccessfully. Next I need to install rspec. Last month it created a problem with these new versions of ruby and rails. hope it ends in success.
Thanks for this guide. I followed step by step and chose MySQL install instead of PostgreSQL
Visiting http://localhost:3000 gives me the following error:
Access denied for user 'root'@'localhost' (using password: NO)
That's saying that your database connection to the database isn't working. You may have specified a password when you installed MySQL or a different username than root. Update your database.yml to specify the database, make sure you have the mysql2 or pg gem installed in the Gemfile, and you'll be good to go.
No, actually you have to set it up. That's sort of where my tutorial leaves off I guess.
gem "mysql2" to your gemfile, change the adapter in database.yml to
adapter: "mysql2" and add a
password line that has the database password. Run
bundle install and restart your Rails app and you should be good to go!
You may also need to run rake db:create to have Rails create the database for you.
why does using "curl -L https://get.rvm.io | bash -s stable"
throws error "curl: (56) Received HTTP code 407 from proxy after CONNECT"
Note: I correctly set proxy address, username and password from terminal and was able to successfully update using "sudo apt-get update" thereby ensuring my internet working.
Sounds like curl isn't able to authenticate with your proxy. You might just try adding the proxy credentials into the curl command. You can run
man curl in terminal to get the docs on it.
Chris, it's rare to find instructions that are brief but functional and easy to follow--great job. I was up and running in less than 30 minutes. If only I could learn Ruby and Rails that quickly! As a long time Java developer, I'll have a lot of adjustments to make. Thanks so much for helping me get started with a frustration-free installation.
Thanks for the kind words Nes! I've been planning how we're going to do the Rails content with the same sort of clarity and we will definitely be taking you along step by step. Hop on the mailing list and I'll be sure to let you know when they are out. :)
Inspect or clean up the working tree at /tmp/ruby-build.20130909154456.11617
Results logged to /tmp/ruby-build.20130909154456.11617.log
Last 10 log lines:
Cloning into 'ruby-2.1.0-dev'...
/tmp/ruby-build.20130909154456.11617/ruby-2.1.0-dev /tmp/ruby-build.20130909154456.11617 ~
/home/surya/.rbenv/plugins/ruby-build/bin/ruby-build: line 393: autoconf: command not found
why was it ??
Great tutorial - working setup on xubuntu 13.04 with postgres, couple of points I needed to change
after creating user i couldn't create a database, needed to login into psql and
alter user chris with superuser
also the line
rails new myapp -d postgres
rails new myapp -d postgresql
One thing I'm not going to post is any of the problems I had getting this installed, even though there were one or two minor issues that cropped up that require a little Google "intuition". What I am going to say is that this post made my life so very, very, very much easier getting up and running. And for that you have my thanks and plenty of it.
this method works great (thank you very much!!! i needed it to compile my university's thesis!), but i have a little problem:
when i go to type "gem install rails", the system give me this output:
"Succesfully installed rails-4.0.0
1 gem installed"
and it's ok
and then, when i check the version with "rails -v":
how can i resolve this?
Make sure you haven't installed Rails from another method. It seems as if you've got two copies of Rails installed.
I've just removed RoR from my Ubuntu 13.04 for installing update version of RoR. But how can I be sure that its completely removed?
When i'm trying to setup my ubuntu 13.04 gives me this !!! what should I do?
"Downloaded archive checksum did not match, archive was removed!
If you wish to continue with not matching download add '--verify-downloads 2' after the command.
There has been an error fetching the ruby interpreter. Halting the installation."
First of all it is a great tutorial brother i just want to confirm that after installing rails when i check rails version it shows 2.3.14 i try to re install rails it shows that "successfully installed rails 4.0.0" but again the version is 2.3.14. I am new to rails so please help me how to sort out this problem.
Listen!!!! I have been work for three plus years supporting a Rails server and application and your instruction were bang on!. I knew where the typos were, and made the required changes. I could not have asked for a better set of instructions.
Thanks a million
I'm using rbnev,
At the step of: "$gem install rails", i get this type of error: "Could not find a valid gem 'rails' (>= 0), here is why:
Unable to download data from https://rubygems.org/ - SSL_connect SYSCALL returned=5 errno=0 state=SSLv3 read server session ticket A (https://bb-m.rubygems.org/q...".
By the way, if you're using this guide to set up a dev environment on a version other than 13.04 or 14.04 (I was using 10.04) be careful not to add this repository:
sudo sh -c "echo 'deb http://apt.postgresql.org/p... precise-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
when installing PostgreSQL 9.3. It will break your packages since it installs libs from Ubuntu Precise on your 10.04 installation. In this case I had to purge the installed packages and do a "sudo apt-get update && sudo apt-get dist-upgrade" to resync my packages to the correct version. Besides that, the rest of the guide works pretty good on 10.04.