Setup Ruby On Rails on
Ubuntu 13.10 Saucy Salamander

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.10 Saucy Salamander.

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.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 python-software-properties 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.0
rbenv global 2.5.0
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
curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm install 2.5.0
rvm use 2.5.0 --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.0.tar.gz
tar -xzvf ruby-2.5.0.tar.gz
cd ruby-2.5.0/
./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.1.4

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.1.4

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
Abe on

I believe in the line "sudo apt-get install postgresql-common -t raring," raring should actually be saucy. Also in the line "rails new myapp -d postgres," postgres should be postgresql.

Gravatar
Chris Oliver (159,850 XP) on

Fixed! Thanks for pointing those out Abe.


Gravatar
bingorabbit on

Thanks for the tutorial, I think in case you installed ruby from source, you will need to execute the "gem install rails" command using sudo, to be like this: "sudo gem install rails" :)

Gravatar
Chris Oliver (159,850 XP) on

Yes! That is correct. I'll try to update the tutorial soon to note that. Thanks!


Gravatar
Ricardo Enrique Berdejö Mora on

Thank you. I just moved ubuntu from Win8 and didn't know how to do it.


Gravatar
Caio Ribeiro Pereira on

Thanks! Awesome post! I'm using into my vagrant ubuntu 13.10 now!


Gravatar
Pamella Gaiguer (20 XP) on

all this packages are exists in ubuntu 13.10 ? When I try to apt-get on them, my ubuntu doesnt find this packages : sudo apt-get install zlib1g-dev libssl-dev libreadline-dev libyaml-dev libsqlite3-dev
libxml2-dev libxslt1-dev

Gravatar
Chris Oliver (159,850 XP) on

Try running sudo apt-get update and then the install again. Maybe your repository list is out of date.

Gravatar
Pamella Gaiguer (20 XP) on

i've tried today and before that, i run sudo apt-get update but not work


Gravatar
Daniel Alvarenga Lima on

Before gem install rails:
rbenv rehash
rbenv global 2.0.0-p247


Gravatar
Anjan Jagirdar (20 XP) on

Unable to locate package lib is what i get when I run
"

sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev lib
"

Gravatar
Chris Oliver (159,850 XP) on

Sorry, there's word wrapping there. Those two lines are meant to be one.

Gravatar
Anjan Jagirdar (20 XP) on

Hey thanks for your swift reply..i have got it done working fine ...thankyou so much guys

Gravatar
Chris Oliver (159,850 XP) on

I fixed the wrapping so everything is on one line now! :)


Gravatar
gomegam on

Admired the way you explained it. Super cool. Very well done.


Gravatar
gomegam on

Just a minor gripe: I tried to install postgres-9.3 as i was using 9.1 on saucy. The problem is it automatically removes the package logrotate and the packages that depend on them. In my case it removed riak (basho.com) and is needed for my work. You can add a warning in the 9.3 install and ask users to wait till Trusty tahr (or) use the default 9.1 bundle.

Gravatar
Chris Oliver (159,850 XP) on

Thanks for pointing that out. I wonder why it removes logrotate...That's definitely something you want to keep around unless they have a replacement.

Gravatar
gomegam on

It removes logrotate and its dependencies automatically.


Gravatar
gomegam on

If you have gem uglifier in your Gemfile, then you don't need to install nodejs.


Gravatar
Connor Leech on

I have a problem installing rails following the instructions:

$ gem install rails
Successfully installed rails-4.0.1
Parsing documentation for rails-4.0.1
Done installing documentation for rails after 1 seconds
1 gem installed
$ rails -v
The program 'rails' is currently not installed. You can install it by typing:
sudo apt-get install rails

little help?

Gravatar
Chris Oliver (159,850 XP) on

Did you install ruby with rbenv? If so, you need to run rbenv rehash to make the executable available.

Gravatar
Connor Leech on

no I installed ruby with rvm

Gravatar
Connor Leech on

victory! I had to add `source ~/.rvm/scripts/rvm` to .bashrc file

http://www.codelearn.org/bl...

Gravatar
Chris Oliver (159,850 XP) on

Oh of course! Thanks for catching that. I'll make sure to update the tutorial.


Gravatar
sebastian garcia on

Excellent explanation! Thank you very much


Gravatar
Miskolc on

First of all, thanks for the tutorial.

Secondly I run into a problem while trying to create my database.

I used your instructions to create my own postgres user but apparently I receive this error when I run rake db:create :

Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"appname_development", "pool"=>5, "username"=>"myusername", "password"=>"mypassword", "host"=>"localhost", "port"=>5432}

where myusername and mypassword are the ones I used to create my postgres user after configuring the database.yml file.

I then tried to see if the user is in the pg_roles database( http://ideone.com/HkDoXO ) and I tried to change the password in the console and restarted the server but I still got the error.

The only way I managed to make rake db:create work was to use the "postgres" user ( and I think this one was created by postgresql automatically).

Gravatar
Chris Oliver (159,850 XP) on

You may want to make sure that the user you created has admin rights to create databases. That could be the problem as it looks like only the postgres user has "Create DB" permissions.

Gravatar
Ankit Singhaniya on

can you tell how to give admin right to the user?

Gravatar
Chris Oliver (159,850 XP) on

See this for granting permission to a user inside of Postgres: http://www.cyberciti.biz/fa...


Gravatar
FinalDevil on

When i run gem install rails, I receive this error message:

ERROR: Error installing rails:
ERROR: Failed to build gem native extension.

Can you help me to overtake this issue? Thanks you.

Gravatar
Chris Oliver (159,850 XP) on

Make sure you installed all the dependencies. The native extensions it needs to build are likely the sqlite3 ones.


Gravatar
Gary Boicourt on

This is awesome Chris, I did a search for installing "ruby on rails ubuntu" and it popped up with a link and your image next to it. I've been staying up to date with the rails eco system and have built a few niche websites using rails but I"m glad to see you're still going strong.

Gravatar
Chris Oliver (159,850 XP) on

Woah, I'm way late on this. Good hearing from you again! Where are you at these days?


Gravatar
Guest on

you forgot to add nodejs package, a fundamental dependency to start webrick on ubuntu

Gravatar
Chris Oliver (159,850 XP) on

It's in there at the top of the Installing Rails section. :)


Gravatar
Adam Nickle on

Thanks a million! This process isn't exactly intuitive for someone who is just starting to learn Rails development, which is funny because starting Django on Ubuntu was a piece of the proverbial cake. Why the heck doesn't the Ubuntu team get their act together and make this just as easy?


Gravatar
Usman on

Hi Chris,
Thanks for this awesome guide.
I am facing problem for installing postgres sql,
its saying:
Package postgresql-9.2 is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'postgresql-9.2' has no installation candidate

Gravatar
Chris Oliver (159,850 XP) on

Try using "postgresql-9.3" instead of 9.2. It's been a little while that 9.3 has been out so the repository may have removed support for the older version. If that works, let me know and I'll make sure to update the tutorial. I'll try to get time to run through the tutorial again on my own to verify anything else that's broken. Thanks! :)


Gravatar
james on

Creating a new user in postgres did not provide the necessary permissions to create a database with "rake db:create." Perhaps modify it to make the user a superuser (-su) or explain how to alter the user role with "ALTER ROLE chris CREATEDB;"

Gravatar
Chris Oliver (159,850 XP) on

Yes, I think it used to ask before if you wanted to create a superuser but when I tried it again it didn't. Updated the instructions to include "-s". Thanks!


Gravatar
reza aulia on

thanks for your info.....bravo ubuntu 13.10 by achek LPMP-Aceh


Gravatar
$36836276 on

Hi, I got this error when entered this command:
sudo -u postgres createuser aaa -s
"could not change directory to "/root": Permission denied"

Gravatar
Chris Oliver (159,850 XP) on

Are you a regular user or root when you ran this? I found this when googling it http://www.postgresql.org/m...

Gravatar
$36836276 on

I did the whole process as root. Do I need to login as postgres user?

Gravatar
Chris Oliver (159,850 XP) on

Ideally you want to do this as your regular user. If you're root, you can do this instead:

su postgres
createuser aaa -s
Gravatar
$36836276 on

Thank but I redo the whole thing as a user (with root privileges). I am stuck at SSHing to GitHub (SSH Key is added to GitHub account).

ssh -T [email protected]
Host key verification failed.

Gravatar
Chris Oliver (159,850 XP) on

For that to work, you need to put your ~/.ssh/id_rsa.pub key into Github.

Gravatar
$36836276 on

Yes I did add key to GitHub (http://puu.sh/6rnjF.png).
It was not a problem when I did that with root but as a new user, I got this problem.


Gravatar
Gordo on

I am new to both ruby and ubuntu and am just wondering where the default directory is for the config/database.yml? Apologies in advance if its quite simple

Gravatar
Chris Oliver (159,850 XP) on

It's located inside your Rails application. It will be generated when you run the "rails new" command.

Gravatar
Gordo on

Oh awesome, cheers! I thought that might have been the case but just wanted to see if it wasn't made in the install and I missed it.

Thanks


Gravatar
Ping^Spike on

Great guide for a *nix n00b like me!!

Only one issue, the "add-apt-repository" stuff was reporting "command not found" so I had to search elsewhere to sort that.

But again, thanks my friend, best guide ever :)

Gravatar
Chris Oliver (159,850 XP) on

Thanks Rich! Did you have to install the python-software-properties or software-properties-common package(s)? Also which version of ubuntu were you on? I'm surprised it didn't come with Ubuntu 13.10

Gravatar
Ping^Spike on

Hey Chris. I had to install both the python-software-propertiesand the software-properties-common packages. It's possible I only needed one, but I forget which one I installed first so I may not have needed it. Sorry, forget which one though - but I definitely needed one of them.

I installed Rails on a clean Ubuntu 13.10 x64 Server edition (no X).

Thanks again, without this guide I wouldn't now be running my own Diaspora* pod :)

Gravatar
Chris Oliver (159,850 XP) on

Fantastic. :) Thanks for the tip. I'll make sure to get that fixed shortly.


Gravatar
Arjun Menon on

if i copy a new rails app and then cd in and type bundle install, it says "The program 'bundle' is currently not installed. You can install it by typing:

sudo apt-get install ruby-bundler"

how can i get it work.
infact doing rails on a new folder says i got to install rails using"sudo apt-get install rails". why like this?
(things were easier with windows)

Gravatar
Arjun Menon on

okay it seems i did all this in a new tab wch was causing it to behave that way. but i got reasons to believe when i start my comp the next time, that's what i will get when i try starting the app.


Gravatar
martian packets on

Setting up LAMP, PHP, and Ruby/Rails on an Amazon EC2 instance, running Ubuntu:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 13.10
Release: 13.10
Codename: saucy

so this is cool. Installing liquid template (Shopify's templating language / framework), drupal, wordpress, and other options. Be fun to set up an old-skool slash-dot server maybe later.

Sound interesting to anyone? If so I'll post my ongoing results here. So far -- stumbles over Amazon's documentation on getting started... but once you have your instance up, everything is straight out of the web, As Amazon claims, it is like having the server in front of you, So (not to beat a dead horce here) It is as if I have my work server here on my coffee table which works for me!
Oh yeah my micro-instance is free for 12 months while I try to do something with it ;)

Gravatar
Chris Oliver (159,850 XP) on

Sure! I'd love to hear how it goes.

Gravatar
martian packets on

Well I actually got a couple of small paid contracts to work on! So I am setting this aside for a bit. I will be getting back to it however. I'll post back to here.


Gravatar
Jagan Ganti on

You should have told us how to click "Ok" in terminal. :D


Gravatar
kachi on

Hi Chris, thanks for the tutorial. I'm new to ruby on rails and is currently trying to set it up on ubuntu 13.10. I've gotten as far as setting up the postgresql but i keep getting this:

try wget --help for more options
gpg: no valid open PGP data found

after typing :

sudo sh -c "echo 'deb http://apt.postgresql.org/p... precise-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
wget --quiet -O - http://apt.postgresql.org/p... | sudo apt-key add -

What do i do?

Gravatar
Chris Oliver (159,850 XP) on

You're welcome! Make sure you run the wget command exactly as it is written. Missing a "- " can cause it to fail. If you need to, download the file yourself, and run

sudo apt-key add ACCC4CF8.asc

where ever you download it to.

Gravatar
kachi on

Thanks, didn't type it in properly.


Gravatar
JT on

Hey thanks for posting this! I set up a user with full permissions on my Linode VPS and then disabled root access for security reasons. Maybe for that reason I am getting "rbenv not found" after I type "rbenv install 2.1.0". Any ideas?

Gravatar
Chris Oliver (159,850 XP) on

If you're getting rbenv not found, then go back and run the echo lines to make sure that rbenv is added to your user's bashrc. That's what makes rbenv and all the rubies you install available to that user.

Gravatar
JT on

Thanks for pointing me in the right direction. Sorry, I'm quite a newbie when it comes to using a server of any kind so it's not too obvious for me... ;-)

Gravatar
Chris Oliver (159,850 XP) on

You may want to walk through the whole rbenv installation again just to make sure it's setup right.

Gravatar
JT on

It worked, thanks!

In case anyone finds this useful, I set up my user as follows at the start:

adduser example_user_name
usermod -a -G sudo example_user_name


Gravatar
Jon on

Thanks for this great article. I'm a newbie and moving pretty slowly in getting the environment set up, so this really helps. At this point, it looks like I have Ruby and Rails installed, I've set up myapp and done the "rails server" command at the end, but the http://localhost:3000 doesn't show me anything ("Problem loading page" message). Do you have any idea what might be wrong? Thanks!

Gravatar
Chris Oliver (159,850 XP) on

Make sure the rails server command is still running. The command will say it is listening on http://0.0.0.0:3000 which means it's running successfully.


Gravatar
Fernando Rosentalski on

Nice post, you could improve adding imagemagick or graphicsmagick as well


Gravatar
Hassan Mohamad on

How to install script on apache2 ubuntu 13.10


Gravatar
Myster on

Rails setup has never been easier! Well done mate - you are legendary!


Gravatar
Lasha on

very cool, big thanks!


Gravatar
Guest on

when i restart terminal it doesn't see ruby anymore :(

Gravatar
Chris Oliver (159,850 XP) on

Make sure your ~/.bashrc has the export PATH lines in it for rbenv or rvm (whichever you were using).


Gravatar
Sarath Uch on

This is the very cool every, Thanks for your nice tuts.


Gravatar
Rico Suave on

Everything worked fine until I tried to add test the website (http://localhost:3000). I received an error that stated "no Rakefile found (looking for: rakefile, Rakefile, rakefile.rb, Rakefile.rb). I ran a trace, but it seems that everything else loaded properly. Do I need to add the rakefile?

Gravatar
Chris Oliver (159,850 XP) on

Hey Rico, there should be a Rakefile automatically generated when you create your rails app. What command were you running that gave this error?

Gravatar
Rico Suave on

Hey Chris. I ran the command "rake db:create"

Gravatar
Chris Oliver (159,850 XP) on

Okay cool, so make sure you're inside the rails application when you run that command.


Gravatar
Eduardo D Jr Barrete on

Hi Chris,

I have this error : No binary rubies available for: ubuntu/13.10/i386/ruby-2.1.0.

Gravatar
Chris Oliver (159,850 XP) on

Hey Eduardo, I believe that is rvm telling you that you don't have all the necessary to compile from source so it is looking for a binary instead. Double check that you installed all the packages with apt-get and the other parts of the tutorial before that.

Gravatar
Eduardo D Jr Barrete on

Hi Chris thanks for your reply. I just reinstalled my ubuntu and now its working fine. Thanks again!


Gravatar
Vitorrio Brooks on

Thanks for that!


Gravatar
Alex Novik (10 XP) on

Thank you! :)


Gravatar
pipevators on

Very helpful guide. Works on my ubuntu 13.10. Thanks!


Gravatar
Tin Le on

Awesome! Many thanks, Chris


Gravatar
Kasey Jean Robinson on

This worked great, thank you so much!!!

I had a little trouble on how to save the git SSH key to a /home/user/ folder, but figured it out with a friend's help. Thanks!

Gravatar
Chris Oliver (159,850 XP) on

Great! Let me know if there's anything I can make more clear for that part.


Gravatar
Dana Nourie (930 XP) on

Geez, wish I'd seen this when I was setting up Ruby & RoR on Ubuntu. What is the reason for installing node.js? I've noticed I always have to install the ruby racer for JS stuff, but I don't on our work server. At work we have node.js installed. Does node.js provide JS abilities (if that's the right word) for process .js files?

Gravatar
Chris Oliver (159,850 XP) on

The ruby racer can be a replacement for Node.js but Node doesn't require you to add a gem to your app. The reason you need one of these is so that you can compile Coffeescript to Javascript (and then combine and minify it in the asset pipeline when you deploy). Node is a Javascript runtime that isn't in the browser and therubyracer gem provides one as well.

Gravatar
Dana Nourie (930 XP) on

Thanks, that's great to know. I'll add node.js and remove the ruby racer gem. BTW, I really like the way you structured this page with the various version at the top, and the tabs for whatever Ruby management tool we use.

Gravatar
Chris Oliver (159,850 XP) on

I was hoping people would find those useful! :)


Gravatar
gpopo on

Chris - 'setup' is a noun, and 'set up' is a verb, and you've misused 'setup' in this article, in other articles you've written on gorails.com, and in replies to this article. Please take a few minutes to learn the correct the usage of 'set up' and 'setup'.


Gravatar
Prabhjot Singh on

Thank you!
Great Job


Login or create an account to join the conversation.