Ruby on Rails Development On Ubuntu 16.04 Linux

Ruby on Rails is one of the most popular web development platforms today, with some of the hottest start-ups and tech giants employing it in their software stacks. One of the biggest selling points of Ruby on Rails is the ease of development. It is just as easy to get set up and start developing, especially on Linux.

Ruby on Rails running on Ubuntu 16.04

Installing the Packages

There are a couple of packages needed before Ruby can be installed in set up, and no, Ruby isn’t one of them. Since this tutorial is going to be using the Ruby Version Manager, or RVM, to manage Ruby, there’s no need to install the package through Ubuntu. There are a couple of packages that RVM needs in order to work and one that never seems to get pulled in by gem installs(nodejs).

# sudo apt-get install build-essential curl nodejs

Installing RVM

Once those are installed, getting RVM up and running on Ubuntu should be easy. RVM is a better way of handling Ruby installs because it generally has more up-to-date versions available, allows for multiple versions of Ruby to be installed at once, and acts to compartmentalize projects and users. The first step towards getting RVM installed is adding the GPG key from the RVM project.

# gpg --keyserver hkp:// --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

With the GPG key added, you can use Curl to pull the RVM scripts for installation along with the most recent stable of Ruby.

# curl -sSL | bash -s stable --ruby

That will take a few minutes and will install a few more packages to Ubuntu as well. The script actually pulls in and compiles Ruby from its source. In order for RVM to work, it has to be added to Bash as a source. The RVM install does this automatically, so if you close the terminal and re-open it RVM should be available. If you don’t want to do this just update the source.

# source ~/.rvm/scripts/rvm

To make sure that RVM is installed and working, run RVM’s help command.

# rvm help

If that’s working, check which version of Ruby is running. As of writing this, the latest stable release is 2.3, and that is the one RVM should pull. Of course, if you’re reading this later on, the version may be higher.

# ruby -v

If, for some reason, you need or want a different version of Ruby, you can check which ones are available easily in RVM.

# rvm list known

To install a different version of Ruby, just find the version number that you want and tell RVM to install it.

# rvm install 2.2

RVM will continue using the previously installed version of Ruby until it is told to do otherwise. This also can very easily be done.

# rvm use 2.2

In order to make a version of Ruby the default, just add the --default modifier to the end.

# rvm use 2.2 --default

Of course, the version of Ruby that was pulled in by default when RVM was installed will be the default unless you change it.

Installing Rails

Now that RVM is installed and configured and Ruby is on the system, you can install Rails. Rails is available as a Gem, which is a Ruby package. When Ruby is installed, its native Gem packaging system is also installed. Installing Ruby packages is very similar to any Linux package manager. To install the latest stable version of Rails, just type the install command.

# gem install rails

If you want a specific version of Rails, add that on the end.

# gem install rails -v 4.1

Rails will probably take a few minutes to install, since it’s rather large. Don’t worry if it seems like it’s hanging on any of the documentation. That’s fairly normal.

Set Up A Rails Project

With Rails installed, the only thing left to do is set up a Rails application to work on. Pick a folder to put your project and cd into.

# cd /folder/containing/yoursite

Rails has its own set of simple command line tools that make setting up and managing projects simple. In order to create a new Rails project, type the following line replacing name-of-site with the actual name of your project.

# rails new name-of-site

When that finishes installing, cd into the project. Rails creates the entire directory structure of the project automatically. Once in the project folder, you will have access to the project specific Rails commands.

# cd name-of-site
# ls

This is clearly not a full Ruby on Rails tutorial, so there’s no sense getting into all of the commands available, but you can test if everything is installed and working correctly by running the Rails development server.

# rails -s

That will bring up the Rails development server, accessible at localhost:3000. If you open that up in the browser, you will see a small message explaining that Rails is running along with the Rails logo. That’s it. If you see that message, Rails is all set up and ready for you to make your web application!