Ruby: 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. 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:
(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:
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:
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:
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.
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