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.
Table of Contents
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.
Step 4: Install RubyGems Manually
If RubyGems isn’t installed, follow these steps:
- Download RubyGems: Get the latest RubyGems package from the official site:
wget https://rubygems.org/rubygems/rubygems-latest.tgz
- Extract the Package: Unpack the downloaded
.tgz
file:tar -xvzf rubygems-latest.tgz
- Navigate to the Directory: Move into the extracted folder:
cd rubygems-*
- Install RubyGems: Run the setup script to install RubyGems:
sudo ruby setup.rb
- 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:- Create a
.gemrc
file in your home directory:nano ~/.gemrc
- Add the following line to configure a user-specific gem directory:
gem: --user-install
- Create a
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.
How do I troubleshoot “gem command not found”?
This error often means RubyGems isn’t installed or isn’t added to your PATH
. Reinstall or adjust the PATH
variable in your .bashrc
or .zshrc
file.
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!
yum : No package rubygems available.
Tyler: What OS and version are you running?
Centos 5.7
Tyler,
Have you tried the ‘Manual Installation’ method listed above?
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
Thanks it worked fine for me on ubuntu.
FWIW, I get the same behavior as Tyler on RHEL 6.
undefined method `map’ for Gem::Specification:Class (NoMethodError)
on ubuntu
undefined method `map’ for Gem::Specification:Class (NoMethodError)
in ubuntu 14.04
RubyGems 2.5.1 installed
ERROR: While executing gem … (NameError)
uninitialized constant Gem::SourceIndex
Did you mean? Gem::Source