Site5 - Built For Designers & Developers MENU
Home  ›  Ruby On Rails  ›  How to setup a custom RubyGems environment

How to setup a custom RubyGems environment

16 Comments

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. We don’t provide a global set of gems for Ruby 1.9.3 onwards, as the Ruby community standard is to manage gems locally, giving the developer full control of the gems available to the application. Most applications use Bundler to manage their gem dependencies – see this article on managing gems with Bundler for more information. 

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:

nano ~/.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:

nano ~/.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

Save that, then run the following command to let the server know we changed things.

source ~/.bash_profile

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

Still have a question? Or need help?
See an error in this article or want to suggest a missing topic? Please leave us a comment below! If you need technical support with your account, please email us, chat live with a representative, or call us!

16 Comments

  • 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

  • Great guide. That was my problem!

    Reply

  • The guide had us add this line twice, once before and once after rails installation:

    PATH=$HOME/.gems/bin/:$HOME/bin:$PATH

    When I follow the directions and enter the source command, which rails gives me the same result as before:

    Rails 3.0.1
    /usr/bin/rails

    Instead of the expected result:

    /home/USERNAME/.gems/bin/rails

    Reply

    • Hello Joe,

      I hope this message find you well!

      I have updated this article with a few changes. If possible, would you please try following the instructions again?

      The only way I was able to reproduce this was not replacing USERNAME with the correct username when editing the gemrc file. From there, so long as all the other steps are completed, the output should be as expected.

      If you have any questions, or if anything is unclear, please feel free to leave a comment here. You can also contact our support department directly by opening a ticket through Backstage.

      Happy Hosting!

      Reply

  • So it seems that I site5 can host a Rails app using ruby 1.9. or even 2.0? with rsync. So glad to hear that if so.

    Reply

    • Hello Lori,

      I hope this comment finds you well!

      The current Ruby version installed is 1.8.7 and unfortunately cannot be changed at this point in time.

      We are locked to version 1.8.7 because that is the default version shipped with cPanel. We’re currently testing the new version of Phusion Passenger, which allows the use of multiple Ruby versions. Once we’ve put it through the paces, we’ll get it deployed fleet-wide and make a blog post announcing its availability on our servers

      http://blog.site5.com

      However, you can use a custom version of Rails and RubyGems. For more information about how to upgrade the version of Rails, please see the following link…

      http://www.site5.com/blog/programming/how-to-enable-rails-3-0-on-site5/20101007/

      For more information about how to setup custom RubyGems, please see the article above.

      In regards to your comment about rsync, could you please provide more details with what you are attempting to do?

      We look forward to hearing from you!

      Reply

      • Actually, ever since I started with Rsync on Site5, everything is great with that particular project. But when I want to work on another project not hosted on Site5, and I try “gem install bundler” I get ‘not allowed at home/artandde/ so I am currently not able to install any other gems for local development use.

        01:22 PM basilking.net $ gem install singularitygs
        ERROR: While executing gem … (Errno::E045)
        Operation not supported – /home/artandde

        01:50 PM ~ $ gem install bundler –development
        ERROR: While executing gem … (Errno::ENOTSUP)
        Operation not supported – /home/artandde

        Reply

        • Hello Lori,

          I hope this comment finds you well!

          I am sorry to hear about the problems you are facing with bundler. Because of the nature of the issue, we ask that you open a ticket with our support department so that we can better assist you in troubleshooting this problem.

          You can open a ticket either through Backstage or by emailing us directly at support@site5.com

          We look forward to hearing from you and helping you resolve this issue.

          Reply

  • Just a comment that the directory structure may not be the same as this article…I’m a reseller and not all that familiar with Ruby and gems, but I’m installing Compass and Sass, and while they installed ok, I was getting “command not found” errors on compass.

    In the tutorial, it uses “.gems” in various places, but in my directory structure as I looked in File Manager, there was no .gems – things were in home/USERNAME/ruby/gems. So I changed every instance of .gems to ruby/gems, and things are working fine now.

    I don’t know if this is a user-specific case or not, but I’m just commenting to share what I had to do to get it working…

    Reply

    • Hi Michael,

      That would be user-specific, yes. You would set the path during this process, and can set this to something not mentioned here, if you like (though I would not recommend doing so, as this follows standards that other things will expect).

      Reply

  • HI

    I have tried installing ruby from source ,
    [user@host bin]$ ruby –version
    ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]

    I have some doubts ,
    1. Do i need to add nano ~/.gemrc this before installing ruby ?
    2. please find my gem env
    [user@host bin]$ ./gem env
    /scratch/user/RUBY/lib/ruby/1.9.1/yaml.rb:56:in `':
    It seems your ruby installation is missing psych (for YAML output).
    To eliminate this warning, please install libyaml and reinstall your ruby.
    RubyGems Environment:
    – RUBYGEMS VERSION: 1.8.11
    – RUBY VERSION: 1.9.3 (2011-10-30 patchlevel 0) [x86_64-linux]
    – INSTALLATION DIRECTORY: /scratch/user/RUBY/lib/ruby/gems/1.9.1
    – RUBY EXECUTABLE: /scratch/user/RUBY/bin/ruby
    – EXECUTABLE DIRECTORY: /scratch/user/RUBY/bin
    – RUBYGEMS PLATFORMS:
    – ruby
    – x86_64-linux
    – GEM PATHS:
    – /scratch/user/RUBY/lib/ruby/gems/1.9.1
    – /scratch/user/.gem/ruby/1.9.1
    – GEM CONFIGURATION:
    – :update_sources => true
    – :verbose => true
    – :benchmark => false
    – :backtrace => false
    – :bulk_threshold => 1000
    – REMOTE SOURCES:
    http://rubygems.org/

    why do i get this below error when i have that file in [user@host ~]$ ls /usr/include/yaml.h
    /usr/include/yaml.h

    /scratch/user/RUBY/lib/ruby/1.9.1/yaml.rb:56:in `':
    It seems your ruby installation is missing psych (for YAML output).
    To eliminate this warning, please install libyaml and reinstall your ruby.

    Please suggest a solution , i couldnt proceed further .

    Reply

    • Hello LKrishnan,

      I’m sorry for the troubles you are having. Based on the copy/paste information provided, though, it looks like you’re attempting this locally, on your workstation. Is that correct? If so, we would not be able to make any suggestions, as every environment is different.

      If you run into any issues with this on our servers, however, we would be more than happy to help, though your best bet would be to open a ticket with our support team, via BackStage.

      Reply

Add your comment below!