Ruby on Rails (rbenv way) in Debian Stretch.

Since this blog was intended to be done in RoR (and obviously it isn't) I'm going to start it sharing my notes about having RoR in Debian Stretch running...before destroying them.

RoR is, in my opinion, a Developer's matter, obviously they put it into production (and it works!), but if you are (like me), a Debian purist that sticks to Debian proof, stable, system-wide, well-documented and standarized packaged binaries, you will have to to open veeeery much your mind, or think twice before getting too much involved with RoR... but I'm not here to talk about RoR, but to share how did I get it working on Debian Stretch!

Cleanup and getting sudo

Before starting, let me advise you about getting ride of any remains of Debian rails packages, and also, in the case you have tried (like I did) to set up your environment with RVM (It is the recommended way, and most Ubuntu guides go that way... but it failed miserably to me to compile any Rubies I tried in a vanilla Stretch) clean any RVM traces.

sudo apt-get remove --purge rails ruby
sudo apt-get autoremove
rm -rf ~/.rvm

RoR is a developer's toy, it is not meant (although Debian provides nice packages) to be used like php or python, but instead, most thing do occur in userspace (well... almost). RoR users do play with various environments, gemsets, and the like (for incredible flexibility and portability), so this is not like having your LAMP/LEMP server up and running system-wide as root... this is enemy territory, doing as root will fail... you need to "Ubuntize" your Debian server... use a non-root user and install sudo:

apt-get install sudo

Edit the file /etc/sudoers and add the user as sudoer:

someUser    ALL=(ALL:ALL) ALL

So we have the first part complete.

Install system wide dependency packages to build the ruby stuff.

Log-in as the user that will manage the ruby circus, and start using sudo.
First we perform a routine system update/upgrade:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade

And now we are going to install the required dependency packages wi will need to get rbenv in our system

sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev nodejs

Now we are ready to install rbenv!

Installing rbenv

Remember we are not root (although we eventually need-and-get root privileges through sudo) and rbenv will end up installed in ~./rbenv at current user's home, not system-wide, and the procedure is developer's style...

git clone ~/.rbenv

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc

git clone git:// ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

git clone ~/.rbenv/plugins/rbenv-gem-rehash


Testing rbenv successful operation

First try the following command:

type rbenv

It should yield something like this:

rbenv: is a function
rbenv () 
    local command;
    if [ "$#" -gt 0 ]; then
    case "$command" in 
        rehash | shell)
            eval "$(rbenv "sh-$command" "$@")"
            command rbenv "$command" "$@"

And the following command:

rbenv install --list

will allow you to get a long list of available rubies (ruby interpreters) to install...

Available versions:

If the commands did work, you're ready to install your Ruby on Rails environment.

Installing ruby interpreters

rbenv will allow us to install rubies present on its list.
Once some interpreters will be installed, rbenv allow us to declare one as default, and declare certain ones as prefered for certain folders.

Installing a new ruby interpreter (replace x.y.z for valid values):

rbenv install x.y.z -k

This will add x.y.z version of ruby interpreter at at ~/.rbenv/versions/x.y.z.
Check your current list of installed interpreters issuing the command:

rbenv versions

That would yield something like this (in this example, there are three rubies installed, and the last one is set as default)

* 2.3.0 (set by /home/youruser/.rbenv/version)

So, you need to set one as default. You can do it easily at any time issuing the command:

rbenv global x.y.z

But tipically, rail's developers like to play with the flexibility of handling several projects having each one its different interpreter as needed. To do so, you may assign an interpreter to a project's folder in that way:

cd ~/myproject
rbenv local a.b.c

To check which ruby interpreter is set at your current directory, just issue a version check agains ruby!

ruby -v


Preparing gem environment

Once you step in ruby, everything is up to gems...
So there are some steps I used to use when setting things up.
First disable installation of documentation on gem install:

echo "gem: --no-document" > ~/.gemrc

Second is to enable gemsets (or something similar) for rbenv, since there seems to be very cool having gems grouped in gemsets.
Gemsets are a RVM feature, and default rbenv does gem install userspace-wide., but there exists a plugin that will allow to add a similar feature:

git clone git:// $HOME/.rbenv/plugins/rbenv-gemset

Finally, we need to install the 'bundler'.
I think of it as a kind of ruby equivalent to php's composer.
bundler, of course, is a gem... a gem that provides a gem dependency handler!
Gems are installed using rbenv very easily:

gem install bundler

We are ready to install (or let bundler to do it) gems as needed.

Installing Rails

Rails is just a gem, so curiously installing is as simple and straightforward as:

gem install rails

Rails and any eventual dependency gems will be installed automagically...
We do have Ruby on Rails installed.

But remember this is just the 'develop' part of the you may look for information on how to make it served by your web server (no, neither Apache nor Nginx know how to handle a RoR project files), you will need to set up Passenger to make Apache2 serve your work, but that's another history.

Creating a new project!

So, now, basically, every time you need to start a new project, with its own locally defined and perpetual ruby interpreter, with its own self-contained independent gemset (so we will install bundler again, but locally in it) you need to execute the commands like in the following example:

rails new my_project
cd my_project

rbenv local x.y.z
echo '.gems' > .rbenv-gemsets

gem install bundler
bundle install --path .gems

...Have a nice coding!

comments powered by Disqus