Site5 KnowledgeBase

Site5 KnowledgeBase

Home  ›  Ruby On Rails  ›  How to setup a custom RubyGems environment

How to setup a custom RubyGems environment

Why you need a custom RubyGems environment

If you want to add a RubyGem on your account, you will need to install it within your account. The reason Site5 does not install RubyGems by request (via the root user) is in the interest of ensuring future compatibility for all customers on the server. If we install a specific version of a RubyGem and that version is later updated, it could very easily break another customer’s Rails application due to compatibility reasons. The RubyGems you use in your Rails application should always be deployed within your own account. Depending on a globally installed RubyGem could result in a non-functioning application if you are ever migrated to a different server (upgrade to VPS).

Notice: This guide does assume that you have some basic knowledge of logging in to your account via SSH, basic command line tools and at least one command line editor (vim/emacs/nano).

Configuring your RubyGems environment

1. The first step is to ensure that your “.gemrc” file is configured properly. Open it up in your text editor:

vi ~/.gemrc

(if the file does not exist, simply create a new empty file and we’ll add in the required contents) Your “.gemrc” file should look like the below (make sure you replace USERNAME with your actual Site5 username):

---
gem: --no-ri --no-rdoc
gemhome: /home/USERNAME/.gems
gempath:
- /home/USERNAME/.gems
- /usr/lib/ruby/gems/1.8
:benchmark: false
:update_sources: true
:verbose: false
:backtrace: false
:sources:
- http://gems.rubyforge.org/
- http://rubygems.org/
:bulk_threshold: 1000

2. Save the changes to the “.gemrc” file and close the file.

3. You can confirm the changes worked by running the command below to view your “live” Gem environment:

gem env

The output should look something like this:

RubyGems Environment:
  - RUBYGEMS VERSION: 1.6.2
  - RUBY VERSION: 1.8.7 (2011-02-18 patchlevel 334) [x86_64-linux]
  - INSTALLATION DIRECTORY: /home/USERNAME/.gems
  - RUBY EXECUTABLE: /usr/bin/ruby
  - EXECUTABLE DIRECTORY: /home/USERNAME/.gems/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /home/USERNAME/.gems
     - /usr/lib/ruby/gems/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => false
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
     - "gempath" => ["/home/USERNAME/.gems", "/usr/lib/ruby/gems/1.8"]
     - "gem" => "--no-ri --no-rdoc"
     - :sources => ["http://gems.rubyforge.org/", "http://rubygems.org/"]
     - "gemhome" => "/home/USERNAME/.gems"
  - REMOTE SOURCES:
     - http://gems.rubyforge.org/
     - http://rubygems.org/

 

Updating your Bash environment to work with your RubyGems environment

The next step is to update our “.bash_profile” file so that if we install any RubyGems that include command line interfaces (binaries), we can run them easily without typing out the full path. This step will also set the GEM_HOME and GEM_PATH environment variables.

1. Open up your “.bash_profile” file in your favorite text editor:

vi ~/.bash_profile

If the file does not exist, create a new one and then make sure it contains the following (copy and paste everything from the #START to the #END lines):

# START
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
 
PATH=$HOME/.gems/bin:$HOME/bin:$PATH
 
GEM_HOME=$HOME/.gems
GEM_PATH=$HOME/.gems:/usr/lib/ruby/gems/1.8
 
export PATH GEM_HOME GEM_PATH
# END

2. Close the file and save the changes you made.

3. In order to initiate the changes to your Bash environment, you need to run the following command:

source ~/.bash_profile

 

Install latest version of Rails gem

It’s possible that your server might not always have the latest version of Rails installed, but that is ok! It’s better to maintain the gem  versions within your account so you have full control over what versions you are using. This ensures that you don’t ever have any surprises that could cause your applications to suddenly stop working due to an update or migration.

1. Firstly, let’s check the current version of Rails and where it’s installed by running the following command:

rails -v; which rails

The output may look something like the following, but the Rails version will likely be different:

Rails 3.0.1
/usr/bin/rails

As you can see, you are using the system “Rails” binary at “/usr/bin/rails” and soon we will be running our own Rails binary from within our home directory.

2. Now, let’s install our own version of Rails! If you want to get the latest version of Rails available, run the following command:

gem install rails

If you want to fetch a specific version of Rails, use the following syntax instead:

gem install rails -v 3.0.10

You should see something similar to the output below:

Successfully installed rails-3.1.0
1 gem installed

3. If you run the “which rails” command it will show you the location of the Rails binary you are now using:

# which rails
/home/USERNAME/.gems/bin/rails

4. You will also see that you are now using the latest version of Rails when you check a “rails -v” from the command line:

# rails -v
Rails 3.1.0

5 Comments + Add Comment

  • What about the situation if the app uses Bundler to maintain gem & dependencies to deploy path /vendor/bundle?

    Reply

    • If you’re using Bundler, you’ll just setup your Gemfile as you normally would. Your RubyGems environment is something completely separate for the most part. However, most people still need a properly configured RubyGems environment for RubyGems that include binaries or command line tools.

      Reply

  • how about changing the Ruby itself, since next version of rails will drop support for Ruby 1.8.7.

    Reply

    • At this time, it is not possible for users to change what version Ruby is running. However, we do have plans to move to 1.9.x in the near future. Keep an eye on the official Site5 Blog (http://www.site5.com/blog/) for when that release happens.

      Reply

      • We would offer both versions of Ruby, but it’s not currently supported in Phusion Passenger (for customers to be able to switch back and forth like with RVM or rbenv). We need to pick one version for all customers and 1.8.7 has the greatest compatibility at the moment.

        Reply

Add your comment below!

Money Back Guarantees Click to verify BBB accreditation and to see a BBB report. Safe Harbor