rbenv is a utility for installing multiple ruby versions on a host machine. Using rbenv allows you to install ruby in a path you have ownership over so you can install gems without having to have sudo or root privileges. rbenv also allows you to target the exact ruby version in development that's in use in production deployments potentially avoiding nefarious bugs due to Ruby version mismatches.
In this post I will install rbenv on openSUSE, install an alternate Ruby version to the system Ruby and cover some basic usage.
For reference the following software will be used in this post.
Install the required dependencies.
sudo zypper install git gcc automake gdbm-devel libyaml-devel ncurses-devel readline-devel zlib-devel libopenssl-devel readline-devel
Clone the rbenv git repository.
git clone https://github.com/rbenv/rbenv.git ~/.rbenv # output Cloning into '/home/vagrant/.rbenv'... remote: Counting objects: 2744, done. remote: Compressing objects: 100% (5/5), done. remote: Total 2744 (delta 0), reused 5 (delta 0), pack-reused 2739 Receiving objects: 100% (2744/2744), 516.62 KiB | 867.00 KiB/s, done. Resolving deltas: 100% (1717/1717), done.
Compile dynamic bash extension to speed up rbenv.
cd ~/.rbenv && src/configure && make -C src # output make: Entering directory '/home/vagrant/.rbenv/src' gcc -fPIC -c -o realpath.o realpath.c gcc -shared -Wl,-soname,../libexec/rbenv-realpath.dylib -o ../libexec/rbenv-realpath.dylib realpath.o make: Leaving directory '/home/vagrant/.rbenv/src'
Update the ~/.bashrc to add the
utility to your path.
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(rbenv init -)"' >> ~/.bashrc
Reload your shell to load the rbenv parameters into the environment.
exec $SHELL -l
Install the ruby-build plugin.
mkdir -p "$(rbenv root)"/plugins git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build # output Cloning into '/home/vagrant/.rbenv/plugins/ruby-build'... remote: Counting objects: 9159, done. remote: Compressing objects: 100% (176/176), done. remote: Total 9159 (delta 202), reused 82 (delta 62), pack-reused 8918 Receiving objects: 100% (9159/9159), 1.91 MiB | 99.00 KiB/s, done. Resolving deltas: 100% (5920/5920), done.
Install an alternate version of Ruby with the
rbenv install <ruby-version>
rbenv install 2.4.0 # output Downloading ruby-2.4.0.tar.bz2... -> https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.0.tar.bz2 Installing ruby-2.4.0... Installed ruby-2.4.0 to /home/vagrant/.rbenv/versions/2.4.0
After installing new version of Ruby or a gem that provides new commands, use the
rbenv rehash command to install shims for all executables known to rbenv.
Create a project folder and set a default ruby version with the
rbenv local <ruby-version> command.
mkdir ~/ruby-project && cd ~/ruby-project rbenv local 2.4.0
This will create a .ruby-version file in the project directory overriding the global Ruby version.
Confirm the Ruby version installed and location of the Ruby binary is in the users home directory.
ruby --version # output ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]
which ruby # output /home/vagrant/.rbenv/shims/ruby
which gem # output /home/vagrant/.rbenv/shims/gem
Confirm gems can be successfully installed as a non root user.
gem install bundler # output Fetching: bundler-1.16.3.gem (100%) Successfully installed bundler-1.16.3 Parsing documentation for bundler-1.16.3 Installing ri documentation for bundler-1.16.3 Done installing documentation for bundler after 3 seconds 1 gem installed
which bundle # output /home/vagrant/.rbenv/shims/bundle
rbenv is a useful utility to managing multiple Ruby installs on a host machine. This allows you to keep your system Ruby install clean and also use the same Ruby version in your development environment as you would use in a production deployment.