Setup Ruby On Rails on
Ubuntu 14.10 Utopic Unicorn

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 14.10 Utopic Unicorn.

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/14.10/

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.

Installing with rbenv is a simple two step process. First you install rbenv, and then ruby-build:

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.

cat ~/.ssh/id_rsa.pub

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:

Since Rails ships with so many dependencies these days, we're going to need to install a Javascript runtime like NodeJS. This lets you use Coffeescript and the Asset Pipeline in Rails which combines and minifies your javascript to provide a faster production environment.

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:

rbenv rehash

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.

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

Installing the 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.

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.



Discussion


Gravatar

Gravatar

Gravatar

Hi, looks like it didn't want to create the database for me with the rake command. I had to log into postgresql and add it manually.

just thought you should know.

Gravatar

Yeah I got this too. I just closed then restarted the terminal and tried again - worked!

I'm a complete nube to the *nix environment, Ruby, etc., having used only Windows since 1990.

Gravatar

Thanks, yeah I didn't try that. Let me know if you run into any problems, I've been using *nix for awhile and am a Win sys admin by trade.

Gravatar

Thanks Jeb, will do.


Gravatar

Thanks for the steps. Worked like a charm :)


Gravatar

Wonderful guide, worked great for me. I was trying to set up a virtual instance on Amazon, first using their Amazon Linux AMI. Scapped two instances after the ruby on rails (dependencies, user rights, missing files, etc bla bla etc) was pretty broken and messy. Started a new virtual instance running Ubuntu instead and followed your steps. Much better. It's quite a lengthy process but in the end everything worked like a charm, thanks to you!


Gravatar

its saying rvm not found what to do

Gravatar

Execute: gpg --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3

After: source ~/.rvm/scripts/rvm


Gravatar

Great tutorial. Thank you very much.

As a side note, PostgreSQL offers repositories for recent Ubuntu versions. In the tutorial, you are using the Ubuntu Precise repository no matter which Ubuntu version one selects at the beginning oof tutorial. After some research I found out that PostregreSQL offers differents repositories depending on the Ubuntu version. That would be nice to add to the tutorial as well, just in case.

http://www.postgresql.org/d...

Gravatar

Thanks Jason! I think at the time they didn't have anything past precise. I'm glad they got Trusty and Utopic in there!

Gravatar

It's no big deal. I just tought it was worth mentioning. I'm learning RoR and this has been one of the best tutorials on how to setup your environment! Very easy. Thanks.


Gravatar
Rael Gugelmin Cunha

Other alternative is to use the RVM PPA (so, automatic updates): https://github.com/rvm/ubun...


Gravatar

Great article! Helped me a ton!

Just an update, the command below is deprecated:

echo "gem: --no-ri --no-rdoc" > ~/.gemrc

now it should be:

echo "gem: --no-document" > ~/.gemrc

Gravatar

Awesome, thanks for the heads up Isaac!


Gravatar

All the process went fine but for some reason I cant see the page on localhost:3000.
It´s giving me a "page not available" error.

Last terminal output is:

[email protected]:~/myapp$ rails server
=> Booting WEBrick
=> Rails 4.2.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2015-02-14 23:50:43] INFO WEBrick 1.3.1
[2015-02-14 23:50:43] INFO ruby 2.1.5 (2014-11-13) [x86_64-linux]
[2015-02-14 23:50:43] INFO WEBrick::HTTPServer#start: pid=18421 port=3000

Gravatar

I had the same problem, but only when trying to connect from another machine on my network. Tried from local and it works successfully. How to open up the server other hosts?

Gravatar

I found out I´m having the problem because my app is only accepting requests from local adapter. The work around I´m using now is to initialize the server with:
rails s - b 0.0.0.0

This will make webrick accept requests from any interface.
Check if this works for you.

Anyway, I´m still trying to discover how to make this the default behaviour for "rails server" command.

Gravatar

Also if your using any kind of virtual machine, make sure your local and VM ports are mapped.


Gravatar

up and running with Rails in less than 2 hours with your tutorial

1000 thank-yous....


Gravatar

thank you very much for saving my time :D


Gravatar

Question, for exporting the rbenv, what's the best way to handle both OSX and Ubuntu? Because I use dotfiles for my zsh and want to just have one file for my zshenv.

Gravatar

I personally store my configs in a Git repo, clone it on each machine, and symlink the configs.


Gravatar

Thanks very much! It didn't took even 20 minutes.


Gravatar

[SOLVED]
Question here.
Working on a Linux VM using VirtualBox.
I'm configuring Git and I added the key to my settings.

When I run:

ssh -T [email protected]

I get the following message:
[email protected]:~$ ssh -T [email protected]
Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.
Permission denied (publickey).

Any ideas on how to correct this? Also, can I continue the installation steps and fix this later?

Thanks!

Gravatar

SOLVED didn't copy my keys in the proper location


Gravatar

Thanks Chris! These are probably the clearest installation instructions I've read.

Gravatar

You're welcome! I'm glad I was able to help. :)

Gravatar

I agree. Totally new and the install process was very confusing when using www.railstutorial.org. Thanks for the clear instructions.


Gravatar

Вы забыли упомянуть "Terminal" -> Profile -> Parameters profile -> Heading and command -> Tick this To start a command as an envelope of an input.


Gravatar

Gravatar

i have a problem....please help me


Gravatar

When I type:

echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL I get an error message: line 124 unepected EOF while looking for matching '"' and line 125 syntax error unexpected end of file.


Gravatar

thx a lot my friend., work like charm without errors on ubuntu mate 14.10. cheers


Gravatar
Pierre-Yves Mathieu

Hi Chris,

Thank you very much for GoRails, I have been using it to setup all my
project. Lately, I switched to Docker and tought Your community would
probably be interested in a faster and more consistent way to set up a
development environement. Please let me know what you think of this
simple docker box I created.

https://github.com/pywebdes...

Install Docker and docker-compose, try google!

1 - run: . setup.sh
2 - wait..., say yes, wait...
then start the dev server:
3 - docker-compose up

look your new server is running on localhost:3000

there's even a box for developping rails gem

https://github.com/pywebdes...

I would be very happy to hear from you, If you have any concerns, please tell me!

Gravatar

This is awesome and I'm definitely going to check it out tomorrow!

Gravatar
Pierre-Yves Mathieu

Have you been able to use it? I would love your feedback to make it easier and better.


Gravatar
José D. Romero Vega

Thanks is a great TUTORIAL! but i have a problem

SOLVED

I do this:
~$ rails new myapp -d mysql

and after I get this in the last part (after create all the stuff)

Fetching gem metadata from https://rubygems.org/............
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies...
Using rake 10.4.2
Using i18n 0.7.0

Bundler::GemspecError: Could not read gem at /home/jodarove/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/cache/json-1.8.2.gem. It may be corrupted.
An error occurred while installing json (1.8.2), and Bundler cannot continue.
Make sure that `gem install json -v '1.8.2'` succeeds before bundling.
run bundle exec spring binstub --all
bundler: command not found: spring
Install missing gem executables with `bundle install`

so I read in another blog the solution ( https://taimoorchangaizpuci... )

I need do this to clear the cache
~$ rm -rf /home/jodarove/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/cache/
where says jodarove change for your user name
and after that I execute again: rails new myapp -d mysql


Gravatar

Great information, on installing ROR in ubuntu.

Ruby on Rails Development in India


Gravatar

i have followed all steps. whenever i run "rake db:create".
It will returns following error:

Couldn't create database for {"adapter"=>"postgresql", "pool"=>5, "username"=>"postgres", "password"=>nil, "database"=>"homeporte_development"}
WARNING: password file "/home/saravanan/.pgpass" has group or world access; permissions should be u=rw (0600) or less
FATAL: Peer authentication failed for user "postgres"


Gravatar

rbenv install 2.2.2
This part took my whole life and am still waiting for its completion buddy


Gravatar

Hi! Thanks for the instructions. But, i have a problem. I'm installing on Ubuntu 14.04 clear system, but, when I put the rbenv install 2.2.3 command, in terminal shows "Installing ruby-2.2.3..." and stays here more of 2 hours. Why is too slow the installation? It's good or something else is bad in the installation? I will appreciate the help

Gravatar

That seems awfully long. Usually it's about 15 minutes or so. Sometimes it can stall because of low ram. You might restart it and see if it finishes sooner. I'd also recommend trying the $10/mo server because it has a bit more power for compiling.

Gravatar

Something I mistake: I have installed Ubuntu on VMWare Virtual Machine. I had add more memory, on limit of 32bit O.S., but, i had the same problem. After that, i should install from source, and now it's working. I hope that will works finally.
Edit: Finally, works. But, only i need install from source. rbenv doesn't work.
Second edit: When you go to install rails 4.2.4, the command should be "sudo gem install rails -v 4.2.4", if not insert "sudo" access, will show an error with the install.


Gravatar

thx for Tut. Works great on deepin with rvm method!


Gravatar

Thank you very much! It's much simple and powerfull article!


Gravatar

Thank you! I think every new project I start begins with visit this page.

Gravatar

Whoo! Thank you thank you! :)


Gravatar

Gravatar

Thanks dude. This tutorial saved my day!


Gravatar

Hi, When i run

ssh -T [email protected],

I have the following message:

/etc/ssh/ssh_config: line 54: Bad configuration option: subsystem

Unsupported Match attribute group

/etc/ssh/ssh_config line 55: Bad Match condition

I try to gererate another Key and i receive the same message ?

I edit the ssh_config file and on line 54 and 55, i have :

Match group ftpaccess
ChrootDirectory %h

So what is the problem ?


Gravatar

Hi All,
i have install ruby from source
after instillation when i run "gem install bundler" it gives below error

ERROR: Loading command: install (LoadError)

cannot load such file -- zlib

ERROR: While executing gem ... (NoMethodError)

undefined method `invoke_with_build_args' for nil:NilClass

i check zlib but it is already installed
Please help to resolve this issue


Gravatar

o meu nao deu certo


Login or create an account to join the conversation.