How do I use gems with Ubuntu?
Where are my Gems?
You can find where your gems are stored using the gem environment
command. For example:
chris@chris-laptop:~$ gem environment
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.2
- RUBY VERSION: 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
- INSTALLATION DIRECTORY: /usr/lib/ruby/gems/1.8
- RUBY EXECUTABLE: /usr/bin/ruby1.8
- EXECUTABLE DIRECTORY: /usr/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-linux
- GEM PATHS:
- /usr/lib/ruby/gems/1.8
- /home/chris/.gem/ruby/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
If you look at the "GEM PATHS:" section you can see that gems can be stored in two places on my laptop: /usr/lib/ruby/gems/1.8
or in the .gem
directory in my home dir.
You can also see that executables are stored in EXECUTABLE DIRECTORY which in this case is /usr/bin
.
Because /usr/bin
is in my path this lets me run cap
, merb
, rails
etc.
Updating your PATH
If for some reason your EXECUTABLE DIRECTORY isn't on your path (for example if it is /var/lib/gems/1.8/bin) then you need to update your PATH variable.
Assuming that you are using the bash shell. You can do this quickly for the current session by typing the following at the shell prompt; let's pretend that you want to add /var/lib/gems/1.8/bin
to the path:
export PATH=$PATH:/var/lib/gems/1.8/bin
and press return. That appends the new directory to the end of the current path. Note the colon between $PATH
and /var/lib/gems/1.8/bin
To set the value for all sessions you will need to edit either your .profile
or .bashrc
file and add the same line to the end of the file. I usually edit my .bashrc
file for no reason other than that's what I've always done. When finished, save the file and then refresh your environment by typing:
bash
at the shell prompt. That will cause the .bashrc
to get reread.
At any point you can check the current value of $PATH
by typing
echo $PATH
at the shell prompt.
Here's a sample from one of my own servers, where my username is "chris" and the machine name is "chris-laptop":
chris@chris-laptop:~$
chris@chris-laptop:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
chris@chris-laptop:~$
chris@chris-laptop:~$ export PATH=$PATH:/var/lib/gems/1.8/bin
chris@chris-laptop:~$
chris@chris-laptop:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/var/lib/gems/1.8/bin
chris@chris-laptop:~$
My Gem won't load!
"Ruby gems won't load even though installed" highlights a common problem using multiple different versions of Ruby; Sometimes the Gem environment and Gem path get out of sync:
rb(main):003:0> Gem.path
=> ["/opt/ruby1.9/lib/ruby1.9/gems/1.9.1"]
irb(main):004:0> exit
Any Ruby process here is looking only in one place for its Gems.
:~/$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.7
- RUBY VERSION: 1.9.1 (2009-05-12 patchlevel 129) [x86_64-linux]
- INSTALLATION DIRECTORY: /opt/ruby1.9/lib/ruby/gems/1.9.1
- RUBY EXECUTABLE: /opt/ruby1.9/bin/ruby1.9
- EXECUTABLE DIRECTORY: /opt/ruby1.9/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /opt/ruby1.9/lib/ruby/gems/1.9.1
- /home/mark/.gem/ruby/1.9.1
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
Look carefully at the output of gem environment:
- GEM PATHS:
- /opt/ruby1.9/lib/ruby/gems/1.9.1
This isn't the same path as returned by Gem.path:
["/opt/ruby1.9/lib/ruby1.9/gems/1.9.1"]
It's hard to say what exactly caused lib/ruby
to change to lib/ruby1.9
but most likely the developer was working with multiple Ruby versions. A quick mv
or ln
will solve the problem.
If you do need to work with multiple Ruby versions then you really should be using rvm.
Can I install gems with apt-get on Ubuntu?
First, download *.tar file, unpack this file, then go to rubygems directory in your console, and type
ruby setup.rb
That's it :)
How to use gem to install Rails on Ubuntu
After upgrading to Rails 4.2.4 on Ubuntu 14.04 using RVM I needed to do this:
sudo apt-get install libgmp-dev
or
sudo apt-get install libgmp3-dev
Full stacktrace: http://ruby-on-rails-eq8.blogspot.co.uk/2015/10/solving-ubuntu-1404-ruby-c-dependancy.html
Ruby gem permission denied /var/lib/gems using Ubuntu
You need to prefix the gem
command with sudo
because /var/lib/gems
is owned by root
. You could also take a look at RVM which allows really easy installation and management of gems and Ruby versions. Best part, it's all in your home dir!
EDIT: per @AndrewMarshall's comment bellow, rbenv is an alternative to RVM.
How do I package a Ruby application for Ubuntu, including its gem dependencies?
Because apt-get
and gem
are both dependency resolving, you can just make a meta package that depends on ruby1.9.1
(which itself brings in Rubygems and everything else). Then in the post-install script, just do a sudo gem1.9.1 install maid
.
I can't lay out the whole process of making a package here, but there are a lot of good tutorials on it around the web.
You don't have write permissions for the /var/lib/gems/2.3.0 directory
You first need to uninstall the ruby installed by Ubuntu with something like sudo apt-get remove ruby
.
Then reinstall ruby using rbenv and ruby-build according to their docs:
cd $HOME
sudo apt-get update
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libxml2-dev libxslt1-dev libcurl4-openssl-dev libffi-dev
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL
rbenv install 2.3.1
rbenv global 2.3.1
ruby -v
The last step is to install Bundler:
gem install bundler
rbenv rehash
How can I get RubyGems 1.3.6 on Ubuntu 10.4
I got it working using
gem install rubygems-update
cd /var/lib/gems/1.9.1/bin
sudo ./update_rubygems
Related Topics
How to Write Postgresql Functions on Ruby on Rails
How to Create a Sha1 Hash in Ruby
Rails 4.0 Strong Parameters Nested Attributes With a Key That Points to a Hash
Controller Spec Unknown Keyword: Id
Why Is Sudo: Bundle Command Not Found
Rubyinstaller 2.2.1 and Rails - Rake Cannot Load Nokogiri
Extract Single String from HTML Using Ruby/Mechanize (And Nokogiri)
How to Find a Min/Max with Ruby
Why Is Sum So Much Faster Than Inject(:+)
Set Global Default Encoding For Ruby 1.9
How to Get Rid of Non-Ascii Characters in Ruby
Ruby 1.9.2 How to Install Rmagick on Windows
How to Remove the Bom from a Utf-8 Encoded File
Ruby: Class Instance Variables VS Instance Variables
Execute a Sudo Command in Ruby on Rails App
Setting Up Rake-Pipeline for Use with Handlebars Alongside Google App Engine