How to install RubyGems on Ubuntu Linux

ubuntu install rubygems

RubyGems is a package manager for Ruby, allowing developers to install, update, and manage Ruby libraries (gems) easily. If you’re working with Ruby on Ubuntu Linux, having RubyGems set up is essential for accessing the vast ecosystem of Ruby packages. This guide explains the steps to install RubyGems on Ubuntu, making it quick and hassle-free to get started.

What You’ll Need

Before proceeding, ensure:

  • You have an Ubuntu-based system (20.04, 22.04, or newer versions).
  • Administrative (sudo) privileges.

Step 1: Update Your System

Before installing Ruby or RubyGems, update the system to ensure all existing packages are current.

sudo apt update && sudo apt upgrade -y

Step 2: Check for Ruby Installation

RubyGems requires Ruby to function. Verify if Ruby is installed on your system by running:

ruby -v

If Ruby isn’t installed, the output will indicate a missing command.

Install Ruby (if not installed):

To install Ruby, run:

sudo apt install ruby-full -y

After installation, confirm Ruby is available:

ruby -v

This should display the installed Ruby version.

Step 3: Check for RubyGems Installation

RubyGems often comes pre-installed with Ruby. To check if RubyGems is already installed, use:

gem -v

If the version is displayed, RubyGems is installed and ready. If not, proceed to the next step.

See also  Solved! Installing sqlite3-ruby gem - extconf.rb mkmf LoadError

Step 4: Install RubyGems Manually

If RubyGems isn’t installed, follow these steps:

  1. Download RubyGems: Get the latest RubyGems package from the official site: wget https://rubygems.org/rubygems/rubygems-latest.tgz
  2. Extract the Package: Unpack the downloaded .tgz file: tar -xvzf rubygems-latest.tgz
  3. Navigate to the Directory: Move into the extracted folder: cd rubygems-*
  4. Install RubyGems: Run the setup script to install RubyGems: sudo ruby setup.rb
  5. Verify Installation: After the setup completes, confirm RubyGems is installed: gem -v

Step 5: Configure RubyGems

For smoother usage, you might want to adjust RubyGems settings:

  • Update RubyGems:
    To ensure you have the latest features and security fixes, update RubyGems: sudo gem update --system
  • Set Default Gem Directory (Optional):
    To install gems globally without sudo, you can set a default directory for gems:
    1. Create a .gemrc file in your home directory: nano ~/.gemrc
    2. Add the following line to configure a user-specific gem directory: gem: --user-install

Step 6: Install Your First Gem

To confirm everything is working, try installing a gem like bundler:

gem install bundler

If the installation is successful, RubyGems is correctly set up.


FAQs

Do I need sudo to install RubyGems or gems?

For system-wide installation, yes, you’ll need sudo privileges. However, user-specific installations can avoid this by configuring a .gemrc file.

How can I uninstall RubyGems?

RubyGems is bundled with Ruby. Uninstalling Ruby will remove it. Alternatively, remove the installed gems manually.

Is RubyGems compatible with all Ubuntu versions?

RubyGems works on all supported Ubuntu releases as long as Ruby is installed.

Can I install specific versions of gems?

Yes, use the following command: gem install <gem_name> -v <version>

With RubyGems installed on Ubuntu, you’re ready to explore the Ruby ecosystem and streamline your development workflow. Whether it’s web development with Rails or automation scripting, RubyGems has got you covered!

11 thoughts on “How to install RubyGems on Ubuntu Linux”

  1. I tried the yum on RHEL6 no go
    I tried the manual installation and I get
    rubygems-2.0.3]# ruby setup.rb
    RubyGems 2.0.3 installed
    ERROR: While executing gem … (Gem::DocumentError)
    RDoc is not installed: no such file to load — rdoc/rdoc
    rubygems-2.0.3]# ls

  2. RubyGems 2.5.1 installed
    ERROR: While executing gem … (NameError)
    uninitialized constant Gem::SourceIndex
    Did you mean? Gem::Source

Leave a Comment