Install Ruby on Rails with Rbenv on CentOS 7
Introduction
Ruby on Rails is a popular open-source web framework that can help Ruby programmers develop web applications rapidly. However, during the development of Ruby on Rails applications, a common demand is to test application's compatibility among various versions of Ruby, which can be satisfied with rbenv's Ruby version management ability.
In this article, I will show you how to install Ruby on Rails with rbenv on a Vultr CentOS 7 server instance, building a solid Ruby development environment.
Prerequisites
Before we move on, I assume you have:
Deployed a CentOS 7 Vultr server instance from scratch.
Logged in as a non-root user with sudo privileges. You can find how to create a non-root user in other articles at Vultr Docs by searching "non-root system user".
Step one: Install and configure git
One of the best coding practices for programmers is to pick a handy version control tool. Here, I chose git.
The easiest way to install git is with yum:
sudo yum update
sudo yum install git
Check whether git is installed properly:
git --version
Configure git:
git config --global user.name "Your Name"
git config --global user.email "xxxxx@xxx.com"
Replace the contents in quotes with your own information.
Verify your configuration:
git config --list
Step two: Install rbenv and its ruby-build plug-in
First, you need to install dependencies for rbenv and Ruby:
sudo yum install -y git-core zlib zlib-devel gcc-c++ patch readline readline-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison curl sqlite-devel
Install rbenv and ruby-build, be sure that you are in your non-root sudo user's home directory:
cd ~
git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile
Step three: Install Ruby with rbenv
You need to determine the version of Ruby that you need. You can list available Ruby versions for installation with the following command:
rbenv install -l
Here, I will install the latest stable version, Ruby 2.2.3:
rbenv install -v 2.2.3
rbenv rehash
Remember to run the command rbenv rehash
every time after you install a new version of Ruby or a gem that provides commands.
If you want to use another version, just install the version as above:
rbenv install -v 2.2.0
rbenv rehash
You can check all the versions you have installed with:
rbenv versions
The version with *
is the active version.
In rbenv, the level of versions can be global, local (application-specific), or shell; each of them with an increasing priority. That's to say, rbenv will try to find and use the version in the order of shell version > local version > global version. If none of the three versions were found, rbenv assumes you want to use the "system" Ruby - meaning whatever version would be run if rbenv weren't in your path.
So, at the very least, you need to set your favorite version as global version for daily use:
rbenv global 2.2.3
Verify your choice with:
ruby -v
Also, you need to install the bundler gem to manage your application dependencies:
gem install bundler
Step four: Install Rails
Install the latest version of Rails:
gem install rails
rbenv rehash
Still, remember to use the command rbenv rehash
here.
Check if Rails is installed properly:
rails -v
Step five: Install Node.js for full functionality
Some Rails features rely on a Javascript runtime to provide functionality. Node is a reasonable choice.
sudo yum install nodejs
Step six: Create a pilot application
You can create a pilot application in your home directory to make sure your installation is successful.
cd ~
rails new pilotapp
cd pilotapp
rake db:create
If you are running a firewall on your server, configure it to allow traffic to TCP port 3000.
Start your pilot application:
rails server --binding=[YourServerIP]
Replace [YourServerIP]
with your server IP.
Visit http://[YourServerIP]:3000
from your browser. If you see the "Welcome aboard" message, your Ruby on Rails installation is successful.
When finished, you can press Ctrl + C to stop your application.