|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Using rbenv to manage your rubies |
| 4 | +author: Ignacio Galindo |
| 5 | + |
| 6 | +avatar: 6be5df410f2695b1341f0c359bc9b461 |
| 7 | +--- |
| 8 | + |
| 9 | +While working with ruby, you have some alternatives to manage your binaries, |
| 10 | +but as you think of them, I hope you are not really considering delegating this |
| 11 | +task directly to your OS package manager since it is discouraged, otherwise you |
| 12 | +would end up with a messy workstation. |
| 13 | + |
| 14 | +There are a few tools that allow you to manage your rubies and gems, the most |
| 15 | +popular among them is without doubts [RVM](http://beginrescueend.com/) |
| 16 | +(Ruby Version Manager) which in all fairness is good, it provides a CLI to |
| 17 | +switch between your rubies and gemsets. If you haven't used RVM, you should |
| 18 | +read [this](http://blog.crowdint.com/2010/07/28/getting-started-with-rvm.html). |
| 19 | + |
| 20 | +Even when RVM is great there are a couple things that I don't like about it: |
| 21 | + |
| 22 | +* Personally, I had a painful situation as Linux user, every time I wanted to |
| 23 | + get a fresh ruby version with support for readline and zlib libraries and its |
| 24 | + dependencies. |
| 25 | + |
| 26 | +* Its gemset management feature tends duplicate gems across your projects. I |
| 27 | + get it, sometimes you need to isolate your gems to keep them compatible. But |
| 28 | + there is another player in the field, called bundler. (mention down below) |
| 29 | + |
| 30 | +* Plus that last bullet, some colleages have mentioned having a 5~6 GB .rvm |
| 31 | + folder. |
| 32 | + |
| 33 | +A couple days ago, I was struggling tracking down a gem that I wasn't sure |
| 34 | +where exactly came from, anyway I was about to create a new gemset for a fresh |
| 35 | +started when someone adviced me to check out rbenv. |
| 36 | + |
| 37 | +## The rbenv way |
| 38 | + |
| 39 | +A highlight in favor of rbenv is that you don't actually need to worry about |
| 40 | +maintaing your gemsets, since it relays on [bundler](http://gembundler.com/) |
| 41 | +who takes care of all your application dependencies. Letting you care about |
| 42 | +just the version of the ruby you want to use globally, locally and in a per |
| 43 | +project basis. Let's check out [rbenv](http://gituhub.com/ssthepenson/rbenv). |
| 44 | + |
| 45 | +*RVM and rbenv aren't friends :(* |
| 46 | + |
| 47 | +First of all, you better avoid using both in the same environment because they |
| 48 | +are incompatible. Don't say I didn't warn you. |
| 49 | + |
| 50 | +## Installation |
| 51 | + |
| 52 | +### 1. Get rid of RVM by running: |
| 53 | + |
| 54 | +{% highlight bash %} |
| 55 | +$ rvm implode |
| 56 | +{% endhighlight %} |
| 57 | + |
| 58 | +### 2. To install rbenv, must be at ~ and clone it: |
| 59 | + |
| 60 | +{% highlight bash %} |
| 61 | +$ git clone git://github.com/sstephenson/rbenv.git ~/.rbenv |
| 62 | +{% endhighlight %} |
| 63 | + |
| 64 | +### 3. Add scope for rbenv binaries to your $PATH |
| 65 | + |
| 66 | +{% highlight bash %} |
| 67 | +$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> .bash_profile |
| 68 | +# be sure of do this to your bash source file (e.g. .bashrc, .profile) |
| 69 | +{% endhighlight bash %} |
| 70 | + |
| 71 | +### 4. Setup bash autocompletion: |
| 72 | + |
| 73 | +{% highlight bash %} |
| 74 | +$ echo 'eval "$(rbenv init -)"' >> .bash_profile |
| 75 | +{% endhighlight bash %} |
| 76 | + |
| 77 | +### 5. Restart your shell. |
| 78 | + |
| 79 | +{% highlight bash %} |
| 80 | +$ exec |
| 81 | +{% endhighlight bash %} |
| 82 | + |
| 83 | +There are two ways of installing rubies with rbenv. From source and *make it* |
| 84 | +into "~/.rbenv/versions/<x.x.x-pxxx>" or the one I prefer using *ruby-build* |
| 85 | + |
| 86 | +### 6. Install ruby-build |
| 87 | +{% highlight bash %} |
| 88 | +$ git clone git://github.com/sstephenson/ruby-build.git ~/.ruby-build |
| 89 | +$ cd ~/.ruby-build |
| 90 | +$ ./install.sh |
| 91 | +# you may need to run with sudo, since it installs a binary in /usr/local/bin |
| 92 | +{% endhighlight bash %} |
| 93 | + |
| 94 | +### 7. Install a ruby |
| 95 | + |
| 96 | +Now, we are ready to install a ruby. |
| 97 | + |
| 98 | +*Note:* After a couple times trying to get a ruby with readline support |
| 99 | +for my *irb*, I googled and found a way. |
| 100 | + |
| 101 | +*For Ubuntu* I used my readline path: |
| 102 | + |
| 103 | +{% highlight bash %} |
| 104 | +$ CONFIGURE_OPTS="--with-readline-dir=/usr/include/readline" rbenv |
| 105 | +install 1.9.3-preview1 |
| 106 | +{% endhighlight bash %} |
| 107 | + |
| 108 | +And there we go, we give it some time, get a coffee or play a ping pong |
| 109 | +match. Once it rbenv finishes, and every time after installing a ruby |
| 110 | +you need to run: |
| 111 | + |
| 112 | +{% highlight bash %} |
| 113 | +$ rbenv rehash |
| 114 | +{% endhighlight bash %} |
| 115 | + |
| 116 | +I have to mention, that it seems tricky, but you can set an alias in |
| 117 | +your `~/.bash_profile` or export an environment variable. |
| 118 | + |
| 119 | +## Usage |
| 120 | + |
| 121 | +Let's suposse you've got some more rubies, now, how do we specify the |
| 122 | +version of ruby we want to use: |
| 123 | + |
| 124 | +*To setup a global ruby you do something like:* |
| 125 | + |
| 126 | +{% highlight bash %} |
| 127 | +$ rbenv global 1.9.3-preview1 |
| 128 | +{% endhighlight bash %} |
| 129 | + |
| 130 | +*To setup a local (per-project) ruby you do:* |
| 131 | + |
| 132 | +{% highlight bash %} |
| 133 | +$ rbenv local 1.9.2-p290 |
| 134 | +# this creates a rbenv-version file in the current folder |
| 135 | +{% endhighlight bash %} |
| 136 | + |
| 137 | +*What version of ruby am I using?* |
| 138 | +{% highlight bash %} |
| 139 | +$ rbenv version |
| 140 | +{% endhighlight bash %} |
| 141 | + |
| 142 | + |
| 143 | +*What versions of ruby do I have?* |
| 144 | +{% highlight bash %} |
| 145 | +$ rbenv versions |
| 146 | +{% endhighlight bash %} |
| 147 | + |
| 148 | + |
| 149 | +## Conclusions |
| 150 | + |
| 151 | +There are a couple things that remain unexplored, but for now this is a getting |
| 152 | +started. In case you miss the gemset, [this](http://github.com/jamis/rbenv-gemset) |
| 153 | +is something you might want to look at. |
| 154 | + |
| 155 | +So, give it a try, you might like it. For now I'm happy with my fresh |
| 156 | +rbenv install. Let us know your rbenv experience. |
0 commit comments