WEB HOSTING SUPPORT NETWORK
     
 

Installing Ruby on Rails with RVM

This tutorial will show you how to install and configure a Ruby on Rails project within a web app.

If you do not have a Web Apps section in your hosting Control Panel, then this tutorial is not suitable for your particular hosting environment. You can ask your hosting provider for more information, or you can seek assistance through our ticketing system.

Important: Throughout this tutorial, 'example' is used as the username for the account. Wherever you see 'example', you should replace it with the actual username for your hosting account.

We will set up a Ruby environment by using Ruby Version Manager (RVM). RVM enables you to manage and work with multiple Ruby environments and allows you to switch between them. This way, you can have multiple projects on the same account, each using a different version of Ruby and different gems.

1. Connect to your account via SSH

2. Install and configure Ruby Version Manager (RVM)

Most of the installation steps have been obtained from the RVM installation documentation.

  • The RVM packages are signed with GnuPG. You need to configure GnuPG and download the required public keys. To set a custom directory for storing the keys, run the following command:

echo "export GNUPGHOME=$HOME/private/.gnupg" >> ~/.bashrcReload your bash profile:
source ~/.bashrcDownload and import the keys:

curl -sSL https://rvm.io/mpapis.asc | gpg --import - && curl -sSL https://rvm.io/pkuczynski.asc | gpg --import - && gpgconf --kill gpg-agent

  • To download and run the RVM installation script, run the following command:

curl -sSL https://get.rvm.io | grep -v 'partition=`df' | bash -s -- --path ~/private/.rvm --ignore-dotfiles

You should get the following output:

RVM was installed in the ~/private/.rvm directory on your account

* To start using RVM, you need to run the command listed below in all your open shell windows (in rare cases you need to reopen all shell windows):

source ~/private/.rvm/scripts/rvm

  • Add the RVM initialization scripts to the bash configuration file:

echo "source $HOME/private/.rvm/scripts/rvm" >> ~/.bashrc

  • Close all your SSH sessions and reconnect. You can test if RVM was loaded correctly in your new session by running:

type rvm | head -n 1

You should get the following output:

rvm is a function

  • RVM uses the /bin/df utility to check if there is enough free disk space on the server before installing a new version of Ruby. Unfortunately,  access to /bin/df is not allowed and the installation fails with an error. As of version 1.29.9, there is no way for this check to be disabled. Therefore, you need to patch RVM by removing the calls to __rvm_calculate_space_free. You can do this with the following command:

sed -i "/__rvm_calculate_space_free/c\__free_space=9999M" ~/private/.rvm/scripts/functions/manage/base_install ~/private/.rvm/scripts/migrate

The patch sets an arbitrary value of 9999M for the __free_space variable.

In case you update RVM to a newer version, you may need to apply the patch again.

  • Autolibs is a feature built into RVM that automatically installs system dependencies. As your user does not have sudo or root access this is not possible, thus you should disable the autolibs feature with the following command:

rvm autolibs 0

In case some gem installation fails or your project does not run due to a missing dependency, you can contact our support team for assistance. You can check for any missing dependencies with the following command:
rvm requirements

3. Install Ruby

RVM could run out of memory when compiling Ruby. Therefore, you should limit the compilation to 3 simultaneous threads by adding the following setting to ~/private/.rvm/scripts/functions/rvmrc:

echo "rvm_make_flags=( -j 3 )" >> ~/private/.rvm/scripts/functions/rvmrcTo make sure that all changes were applied, reload RVM:

rvm reload

You can now install the latest stable version of Ruby by running:

rvm install ruby --latest

RVM will compile Ruby from source and install a global gemset. At the time of this writing, the latest stable Ruby is 3.1.2. We will use this version in the examples that follow. You can switch to that Ruby version with:

rvm use 3.1.2

4. Install Rails and create a project.

You can install Rails directly into the global gemset. However, many developers prefer to keep the global gemset sparse and install Rails into a project-specific gemsets, so that each project has the appropriate version of Rails.

  • You need to create a directory for storing your project. All web app projects should be located in the ~/private directory. Create a new directory named ‘blog’ and switch to that directory:

mkdir ~/private/blog
cd ~/private/blog

  • Create a project-specific gemset:

rvm use ruby-3.1.2@blog --ruby-version --create

The name of our project and our gemset is blog.

The option “—ruby-version” creates .ruby-version and .ruby-gemset files in the root directory. RVM recognizes these files in an application’s root directory and loads the required version of Ruby and the correct gemset whenever you enter the directory.

  •  Proceed with installing Rails:

gem install rails

Rails will be installed in the gemset blog.

This can take a while as Rails has a lot of gem dependencies.

  • Now create a new Rails project:

rails new .

With '.' (a dot) as project name, the command creates a new project in the current directory, and the project name is the same as the name of the directory.

You may be asked if .ruby-version should be overwritten. The old and new files have the same content, so both 'yes' and 'no' are valid options.

In case some gems were not installed correctly locally, you can fix this by running the following command:

bundle install

5. Create and configure the Web App

Open the Web Apps section of your hosting Control Panel and create a new app with the following settings:

• Choose Custom as the engine.
• Enter a name for your app. It is only for internal reference to the app. In this example, it is Blog.
• Choose a subdomain at which you wish to access the app. In this example, we choose the "www" subdomain: www.example.com.
• Leave the default web access path / (forward slash). For example, if you enter /rails, you will be able to access the app at http://www.example.com/rails. You do not need to create the directory.
• A port will be automatically assigned to the new project. It will be transparent to your users. It will be displayed in the app list after creating the project. You should note it as you will need it at the next step. In this example, it is 44181.
• For deployment directory select the directory where the project is located. In this case it is /private/blog.

• The Rails web server can be started with the following command:

rails server

However, to run a Ruby application as a web app with RVM, you need to first load the corresponding RVM environment and then run the executable of your app. For every installed ruby and gemset, RVM creates environment files. You can list the environment file for this project by running the following command in the project directory:

rvm env --path

RVM will return the path to the environment file:

/home/example/private/.rvm/environments/ruby-3.1.2@blog

You can use the 'source' command to load this environment file when starting Rails. Both commands combined are:

source /home/example/private/.rvm/environments/ruby-3.1.2@blog && rails server

You should enter this combined command in the Start command field of the web app.

Once you are finished, the project should look like the following:

 Web App

  • Before starting the web app, you need to configure the Rails app to use the correct port. The port setting is located in ~/private/blog/config/puma.rb. Change the default port 3000 to the one of your web app (in this example 44181):

    port ENV.fetch("PORT") { 44181 }

  • Add the following line to your ~/private/blog/config/application.rb file in the "class Application < Rails::Application" code block to allow requests from your domain/subdomain to your Ruby on Rails application:
    config.hosts << "www.example.com"
  • Start the web app by clicking on the red 'Enable this app' button. If everything is working correctly, you should see the following when you open the app URL in a browser:

Ruby on Rails 7.0.3.1