rbenv Install Ubuntu 2004
21st November 2020
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 Ubuntu, 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.
- Ubuntu - 2004
- rbenv - 1.1.2-36-g60c9339
- ruby - 2.7.1
Install the required dependencies.
sudo apt install git curl autoconf bison build-essential \ libssl-dev libyaml-dev libreadline6-dev zlib1g-dev \ libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev
The following installer script will install rbenv and the ruby-build plugin
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-installer | bash
Update Shell Environment
Update your ~/.zshrc file with the following content.
# ~/.zshrc export PATH="$HOME/.rbenv/bin:$PATH" eval "$(rbenv init -)"
rbenv-doctor is used to detect common issues.
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash
Install an alternate version of Ruby with the
rbenv install <ruby-version>
rbenv install 2.7.1 # output Downloading ruby-2.7.1.tar.bz2... -> https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.1.tar.bz2 Installing ruby-2.7.1... Installed ruby-2.7.1 to /home/bradmin/.rbenv/versions/2.7.1
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.7.1
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.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]
which ruby # output /home/bradmin/.rbenv/shims/ruby
which gem # output /home/bradmin/.rbenv/shims/gem
Confirm gems can be successfully installed as a non root user.
gem install bundler # output Fetching bundler-2.1.4.gem Successfully installed bundler-2.1.4 Parsing documentation for bundler-2.1.4 Installing ri documentation for bundler-2.1.4 Done installing documentation for bundler after 1 seconds 1 gem installed
which bundle # output /home/bradmin/.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.