Introduction

In this article we will show you how to set up a Ruby on Rails development environment in Ubuntu 16.04. In addition, this article will show you how to install and set up the following software:

  • git
  • Ruby 2.3.3
  • Rails 5
  • rbenv
  • ruby-build
  • A Database Server, The following database servers are covered:
    • PostgreSQL 9.5.x
    • MySQL 5.7.x
  • An Editor, The following editors are covered:
    • Visual Studio Code
    • Atom
    • Sublime Text

Let's get started.

Step 1: Install Dependencies

The first thing we need to do is install a few required packages. Run the command below to install these now. A description of each of these is below.

  
    sudo apt install -y git make gcc g++ libssl-dev libreadline-dev zlib1g-dev sqlite3 libsqlite3-dev

The git package is used for source control. The make package is used to direct compilation of software packages. The gcc/g++ packages are compilers for the C and C++ languages. The libssl-dev, libreadline-dev, and zlib1g-dev are dependencies that are required to build Ruby. The sqlite3 and libsqlite3-dev packages are for SQLite3. By default Rails uses SQLite 3 as the database unless you specify otherwise.

All of the above packages are required to build and install Ruby.

Step 2: Install rbenv

rbenv is used to manage multiple Ruby versions. With rbenv, you can have multiple versions of Ruby installed at any given time and switch between them as needed. I find rbenv along with ruby-build to be the best tools for installing, managing, and updating Ruby.

The rbenv source code is on GitHub, so we need to clone it to our local machine. The default installation directory for rbenv is in ~/.rbenv. This is because rbenv is installed on a per user basis. Run the command below to install rbenv.

Terminal Commands:
  
    git clone https://github.com/rbenv/rbenv.git ~/.rbenv
  

Now run the commands below to add rbenv to your path and enable it when the terminal opens.

Terminal Commands:
  
    echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
    echo 'eval "$(rbenv init -)"' >> ~/.bashrc
  

Close and reopen the terminal window after this change or run source ~/.bashrc to make the changes take effect.

This next step is optional. Run the commands below to compile a bash extension to accelerate rbenv. Don't worry if this step fails, rbenv will still work fine.

Terminal Commands:
  
    cd ~/.rbenv && src/configure && make -C src
  

Great, now let's install ruby-build.

Step 3: Install ruby-build

ruby-build is an rbenv plugin designed to download, build, and install various ruby versions. Run the command below to download and install it.

Terminal Commands:
  
    git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
  

There is no additional setup required.

Step 4: Install Ruby 2.3.3

Now it's time to install Ruby. The latest version of Ruby as of this writing is 2.3.3. Run the command below to install Ruby.

Terminal Commands:
  
    rbenv install 2.3.3
  

Now let's tell rbenv to use Ruby 2.3.3 globally. Run the command below to do this now.

Terminal Commands:
  
    rbenv global 2.3.3
  

The next step will show you how to disable local documentation. Downloading and building documentation can take a ton of time. It usually takes far longer to generate the documentation then to simply view it online when needed. If you are going to be developing on a machine with access to the internet, then perform this step. If you will be spending large amounts of time offline then you should skip this step and let the documentation install. Run the command below to disable local documentation.

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

Now we need to check that RubyGems is up to date. I also like to update the pre-installed gems as well. Run the commands below to perform both of these tasks. Push y for any prompts that come up asking you to overwrite.

Terminal Commands:
  
    gem update --system
    gem update
  

Now it's time to install a database server.

Database Servers

The two most popular database servers to use for Rails Development are PostgreSQL and MySQL. We will show you how to install both. If you aren't sure which one to use, install PostgreSQL or just install both.

Install PostgreSQL

We need to install two packages here. The libpq-dev package contains development heads and binaries that are necessary for building the pg gem. More on the pg gem in a moment. The postgresql-server package contains the PostgreSQL server.

Terminal Commands
  
    sudo apt install libpq-dev postgresql-server
  

Now run the following command to create a local user and database for your username.

Terminal Commands
  
    sudo -u postgres createuser -s $(whoami); createdb $(whoami)
  

Rails talks to PostgreSQL using the pg gem. I like to install the gem globally to make sure that it doesn't fail installing. Run the command below to install this gem now.

Terminal Commands
  
    gem install pg
  

The pg gem should install successfully, however database gems in general are a fickle bunch. If the gem fails to install, make sure you installed libpq-dev as mentioned earlier. Otherwise, try a google search for the error. You can also try asking on Stack Overflow or in the comments section below.

MySQL

For MySQL, we will install two packages. The first package, libmysqlclient-dev contains development headers and binaries that will be required by the mysql2 gem. More on the mysql2 in a moment. The mysql-server package contains the MySQL server itself.

Terminal Commands
  
    sudo apt install libmysqlclient-dev mysql-server
  

You will be prompted to create a root password for MySQL. Choose a password you can remember, then click 'Ok'. You will then be asked to confirm the password, enter that same password and click 'Ok' again.

Now we need to install the mysql2 gem. The mysql2 gem is the gem that Rails uses to communicate with the MySQL server. Why mysql2? The mysql gem is currently un-maintained and the mysql2 gem is much faster anyway. Run the following command to install the mysql2 gem.

Terminal Commands
  
    gem install mysql2
  

If this step fails, make sure you installed the libmysqlclient-dev package as mentioned earlier. If it still fails, do a Google search or post a question at Stack Overflow or here.

Step 6: Install Rails

Installing Rails at this point is simple. Simple run the command below.

Terminal Commands:
  
    gem install rails
  

Installing An Editor

There are many editors out there to choose from. Some people prefer something as hardcore as vim or emacs. That's fine and all, but not great for beginners since each of those editors have their own learning curve. Here we will cover the basic installation of 3 different editors. Visual Studio Code, Atom, and Sublime Text 3. Visual Studio Code and Atom are both free and open source, while Sublime Text 3 is commercial product.

Note:  For those that are curious, I use Visual Studio Code if I have a GUI and vim if i'm using the terminal.

Visual Studio Code

Visual Studio Code screenshot

Visual Studio code is an awesome new open source editor built by Microsoft. It is extremely powerful, yet very user friendly. I recommend this over the other editors. If you don't want to use Visual Studio code, we will also show you how to install Atom as well as Sublime Text 3.

Visit the Visual Studio Code website at https://code.visualstudio.com and download the .deb package.

Visual Studio Code home page

Click the File browser files icon, then click Downloads on the left side of the file browser window that pops up.

VS code package in file browser

Finally, double click the package to install it. When the software manager window comes up, click the install button. Note that you will be asked to enter the root password to install the package.

Visual Studio Code screenshot

After installing Visual Studio code, click the extensions icon (or go to View -> Extensions) and install the following extensions by searching for them and clicking install: Ruby, SCSS Intellisense, Slim, and Ruby Haml Colorization. A screenshot of the extensions window is below for reference.

Install Ruby extension screen

Visual Studio Code is now ready to go.

Atom

Atom screenshot

Atom is another free, user friendly editor. Installing Atom is pretty easy. Simply visit the Atom Home Page and click the link that says Download .deb.

Atom home page

Click the File browser icon files icon, then click Downloads on the left side of the file browser window that pops up.

Atom code package in file browser

Finally, double click the package to install it. When the software manager window comes up, click the install button. Note that you will be asked to enter the root password to install the package.

Atom screenshot

The atom default packages include full Ruby on Rails support, so you are good to go.

Sublime Text

Sublime Text 3 screenshot

Sublime Text is an older commercial offering. It is still very popular and used by many users. To install it, visit the Sublime Text 3 home page. Then click the Ubuntu link that corresponds to your platform.

Sublime Text 3 home page

Click the File browser icon files icon, then click Downloads on the left side of the file browser window that pops up.

Sublime Text 3 package in file browser

Finally, double click the package to install it. When the software manager window comes up, click the install button. Note that you will be asked to enter the root password to install the package.

Install Sublime Text 3 screenshot

Once installed, you will need to install package control. To do this, open Sublime Text, click on the View menu, and choose Show Console. Next, paste the code below into the input box at the very bottom of the window. A screenshot is below the code for reference.

  
import urllib.request,os,hashlib; h = 'df21e130d211cfc94d9b0905775a7c0f' + '1e3d39e33b79698005270310898eea76'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)
  
Install Sublime Text 3 package control

Once the process completes, close and re-open sublime text.

Once the package manager installed, there are a couple packages that you will need to install in order to ensure an optimal experience. First we need to install support for SASS/SCSS. Push CTRL+P to open the command palette (or go to Tools -> Command Palette) and type IP. An option called Package Control: Install Package should be highlighted, simply push enter to choose this option. Next, type SASS. A package called SASS should pop up, push enter to install it.

We also need to install support for CoffeeScript. To do this, open the command palette again and choose install package as mentioned in the paragraph above. Type better coffeescript. The Better CoffeeScript package should pop up, push enter to install it.

Sublime text is now ready to go.

Conclusion

At this point, you should be ready to start developing both Ruby and Rails apps. If you have questions or comments, please be sure to leave a comment below. Thanks for reading!

Resources