Showing posts with label debian. Show all posts
Showing posts with label debian. Show all posts

Aug 10, 2008

Multiple Versions of Ruby On Ubuntu #2


This post is from a time before RVM or rbenv you should check out those instead..



I recently changed how I'm handling multiple simultaneous Ruby installations and I'd like to share.

What I needed was to make it convenient to switch between the system provided packages and specific, from source, installations. After some experiments I decided to use 'update-alternatives' to do it.

Here's a quick walk through...

First I removed all of my Ruby and RubyGem environment variables, they're not needed.

Then I installed Ubuntu's default Ruby packages via apt-get.
$ sudo apt-get install ruby irb ri rdoc libruby-extras rubygems ruby1.8-dev

Next I downloaded and installed alternate versions of Ruby from source. In this example I'm going to use two additional versions; the newest stable release and the newest development release.
$ cd /tmp
$ wget -c ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p71.tar.gz
$ tar -xvzf ruby-1.8.7-p71.tar.gz
$ cd ruby-1.8.7-p71
$ ./configure --prefix=/opt/ruby-1.8.7-p71
$ make
$ sudo make install
$ wget -c ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.0-3.tar.gz
$ tar -xvzf ruby-1.9.0-3.tar.gz
$ ./configure --prefix=/opt/ruby-ruby-1.9.0-3
$ make
$ sudo make install

At this point I have three versions of Ruby installed and each can be accessed through it's full path.
$ /usr/bin/ruby --version
# ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]
$ /opt/ruby-1.8.7-p71/bin/ruby --version
# ruby 1.8.7 (2008-08-08 patchlevel 71) [i686-linux]
$ /opt/ruby-1.9.0-r18217/bin/ruby --version
# ruby 1.9.0 (2008-07-25 revision 18217) [i686-linux]

You'll also notice that the default installation is the one provided by Ubuntu.
$ ruby --version
# ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]

Next we'll use 'update-alternatives' to make it a bit easier to switch between them. You could do this on the command line but it becomes a fairly long nasty command so I found it easier to write a quick shell script and run it. The script:
update-alternatives --install \
/usr/local/bin/ruby ruby /usr/bin/ruby 100 \
--slave /usr/local/bin/erb erb /usr/bin/erb \
--slave /usr/local/bin/gem gem /usr/bin/gem \
--slave /usr/local/bin/irb irb /usr/bin/irb \
--slave /usr/local/bin/rdoc rdoc /usr/bin/rdoc \
--slave /usr/local/bin/ri ri /usr/bin/ri \
--slave /usr/local/bin/testrb testrb /usr/bin/testrb

update-alternatives --install \
/usr/local/bin/ruby   ruby /opt/ruby-1.8.7-p71/bin/ruby 50 \
--slave /usr/local/bin/erb erb /opt/ruby-1.8.7-p71/bin/erb \
--slave /usr/local/bin/gem gem /opt/ruby-1.8.7-p71/bin/gem \
--slave /usr/local/bin/irb irb /opt/ruby-1.8.7-p71/bin/irb \
--slave /usr/local/bin/rdoc rdoc /opt/ruby-1.8.7-p71/bin/rdoc \
--slave /usr/local/bin/ri ri /opt/ruby-1.8.7-p71/bin/ri \
--slave /usr/local/bin/testrb testrb /opt/ruby-1.8.7-p71/bin/testrb

update-alternatives --install \
/usr/local/bin/ruby   ruby /opt/ruby-1.9.0-r18217/bin/ruby 25 \
--slave /usr/local/bin/erb erb /opt/ruby-1.9.0-r18217/bin/erb \
--slave /usr/local/bin/gem gem /opt/ruby-1.9.0-r18217/bin/gem \
--slave /usr/local/bin/irb irb /opt/ruby-1.9.0-r18217//bin/irb \
--slave /usr/local/bin/rdoc rdoc /opt/ruby-1.9.0-r18217/bin/rdoc \
--slave /usr/local/bin/ri ri /opt/ruby-1.9.0-r18217/bin/ri \
--slave /usr/local/bin/testrb testrb /opt/ruby-1.9.0-r18217/bin/testrb

What that does is create a group of applications under the generic name Ruby. In addition each application has several slave applications tied to it; erb, irb, etc... In defining each application we specify what symbolic link it will be accessed through and where the application is actually installed. In my case Ubuntu installed Ruby in /usr/bin and the source installed versions are in /opt. All of the installations will be accessed through the generic name Ruby and will have there symbolic links created in /usr/local/bin. I choose /usr/local/bin because it supercedes /usr/bin in the default path.

Before moving on make sure that 'update-alternatives' sees all of our Ruby installations:
$ update-alternatives --list ruby
# /opt/ruby-1.9.0-r18217/bin/ruby
# /opt/ruby-1.8.7-p71/bin/ruby
# /usr/bin/ruby

Now switching between them is as easy as running the 'update-alternatives' command and selecting the number of the installation you'd like to use. Example:
$ sudo update-alternatives --config ruby

It's important to keep in mind that each installation is separate. So for example if you install RubyGems while using /usr/bin/ruby it will not be available to /opt/ruby-1.9.0-r18217/bin/ruby, or /opt/ruby-1.8.7-p71/bin/ruby, etc....

While it's probably possible to use a shared repository for RubyGems across multiple installations I haven't tried it and instead have choosen to use multiple separate RubyGem installs, one for each Ruby installation.

Also RubyGem's bindir will most likely not be in your path. To get around this I created a short script called 'gemexec' in /usr/local/bin
#!/usr/bin/env ruby

require 'rubygems'

if ARGV.size > 1
exec "#{Gem.bindir}/#{ARGV.shift}",ARGV.join(" ")
else
exec "#{Gem.bindir}/#{ARGV.shift}"
end

This script uses the RubyGems installation of the currently selected Ruby to determine where the executable gem should be found, then runs it with any additional command line arguments provided. example:
$ gemexec rake --version
# rake, version 0.8.1

With all that in place the only thing to watch out for is other peoples scripts that hardcode the shebang line with something like "#!/usr/bin/ruby". What I do myself, and prefer in general, is to use "#!/usr/bin/env ruby".

The previous 'multiple versions of ruby on ubuntu'.

Dec 27, 2007

Multiple Versions of Ruby On Ubuntu



This post is from a time before RVM or rbenv you should check out those instead..

With Ruby 1.9 out there's the obvious possiblitity some people will want to run multiple versions of Ruby so I thought I'd share this.

My goal is to have the Ubuntu Ruby packages installed along side the most current releases of 1.8 and 1.9

First I'll install using apt to create my default ruby install.

$> sudo apt-get install ruby irb ri rdoc libruby-extras rubygems ruby1.8-dev
$> sudo gem install rake

Next I'll install the most current release packages of 1.8 and 1.9 in to /opt/ruby1.8.6 and /opt/ruby1.9.0 respectively.

Before doing that I'll make sure I have all the necessary build dependencies for both packages
$> sudo apt-get build-dep ruby1.8 ruby1.9

Next I downloaded both packages into a working directory and decompress them
$>mkdir temp; cd temp
$>wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.6-p111.tar.gz
$>tar -xvzf ruby-1.8.6-p111.tar.gz
$>wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.0-0.tar.gz
$>tar -xvzf ruby-1.9.0-0.tar.gz

Next I build each of the packages.
$> cd ruby-1.8*
$> ./configure --prefix=/opt/ruby1.8 --program-suffix=1.8.6
$> sudo make && make install
$> cd ../ruby-1.9*
$> ./configure --prefix=/opt/ruby1.9 --program-suffix=1.9.0
$> sudo make && make install
Ruby 1.8 doesn't have built in support for gems like Ruby 1.9 so we'll have to install it.
$> wget http://rubyforge.org/frs/download.php/29548/rubygems-1.0.1.tgz
$> tar -xvzf rubygems-1.0.1.tgz
$> cd rubygems*
$> sudo /opt/ruby1.8.6/bin/ruby1.8.6 setup.rb
Notice that I specified the entire path to the ruby executable while installing RubyGems for 1.8.6

The next thing I do is create some symbolic links to make life a little bit easier.
$> sudo ln -s /opt/ruby1.8.6/bin/* /usr/local/bin
$> sudo ln -s /opt/ruby1.9.0/bin/* /usr/local/bin
Now test things a bit to make sure everything makes sense
$> which ruby
# /usr/bin/ruby
$> gem env
# ....
# GEM PATH: /var/lib/gems/1.8
# ....

$> which ruby1.8.6
# /usr/local/bin/ruby1.8.6
$> gem1.8.6 env
# ....
# GEM PATH: /opt/ruby1.8.6/lib/ruby/gems/1.8
# ....

$> which ruby1.9.0
# /usr/local/bin/ruby1.9.0
$> gem1.9.0 env
# ....
# GEM PATH: /opt/ruby1.9.0/lib/ruby/gems/1.8
# ....

A couple of things to watch out for:
  • Make sure you don't have any RubyGems environment variables set. They're not needed for any of this and most likley will mess something up.
  • Don't install any gems until after you create your symbolic links or the executable commands may clobber each other.
  • You have to specify the full path to an executable gem because they don't play nicely with program suffixes. So for example '/opt/ruby1.8.6/bin/rake' will do what you expect but just typing 'rake' may not.
One last little tidbit. I'm not sure if the Ubuntu RubyGems packages deal with fixing up the $PATH yet? Regardless the fix is easy; add this little bit of code to the bottom of your ~/.bashrc file
if [ -d /var/lib/gems/1.8/bin ]; then
PATH=/var/lib/gems/1.8/bin:"${PATH}"
fi
export PATH

Customizing Debian Packages

Debian's packaging system makes it really easy to modify packages if you want to. In this little howto I modify ffmpeg to add MP3 support but the process can easily be adapted to any package.

First create a work directory and drop down into it. This is just to make cleanup easy.

$> mkdir temp; cd temp

Then make sure the ffmpeg build dependencies are installed
$> sudo apt-get build-dep ffmpeg

Then download the source package
$> apt-get source ffmpeg

Then install the extra (codec) libraries we're adding support for
$> sudo apt-get install liblame-dev libfaad2-dev libfaac-dev libxvidcore4-dev

Then drop down into the ffmpeg source directory
$> cd ffmpeg*

Now you will have to edit the first line of the the debian/changelog and modify the package version. If you don't system updates will want to 'replace' your modified package. The first line should look something like this:
ffmpeg (3:0.cvs20070307-5ubuntu4) gutsy; urgency=low
. You will want append something at the end of the part in parentheses. In my case I use:
ffmpeg (3:0.cvs20070307-5ubuntu4-mg1) gutsy; urgency=low

It's my initials followed by a revision number.

Once you've modified the change log you can build the package but before we do we set an environment variable used by the build scripts that indicates we want the 'risky' libraries included.
$> DEB_BUILD_OPTIONS=risky && fakeroot debian/rules binary

Now we just need to install the packages. The packages were placed up one directory during the build process so we just move up and install.
$> cd .. && sudo dpkg -i *.deb

Now we just clean up after ourselves
$> cd .. && rm -rf temp