6 min read, 987 words
Ruby Version Management using rbenv
Excerpt : Easy ruby version switching using rbenv
So, In this guide, we gonna be exploring rbenv for ruby version management. This guide assumes that you are using any Debian or Ubuntu Linux. Currently i am using Ubuntu 20.04 LTS You can check this out by :
$ lsb_release -a` No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focal
This will print out your current Linux OS version:
Great! Let’s get started
$ sudo apt install rbenv
$ rbenv init
This will print the following to the terminal :
Load rbenv automatically by appending the following to ~/.bashrc:
eval "$(rbenv init -)"
Okay let’s do that :
$ nano ~/.bashrc
and add the line “eval “$(rbenv init -)” to the end of file. save it. close it.
Close terminal, Open new terminal, run the command :
$ rbenv rbenv 1.1.1 Usage: rbenv <command> [<args>] Some useful rbenv commands are: commands List all available rbenv commands local Set or show the local application-specific Ruby version global Set or show the global Ruby version shell Set or show the shell-specific Ruby version install Install a Ruby version using ruby-build uninstall Uninstall a specific Ruby version rehash Rehash rbenv shims (run this after installing executables) version Show the current Ruby version and its origin versions List all Ruby versions available to rbenv which Display the full path to an executable whence List all Ruby versions that contain the given executable See `rbenv help <command>' for information on a specific command. For full documentation, see: <https://github.com/rbenv/rbenv#readme> verify if rbenv is properly setup using this rbenv-doctor script : run the below command : $ curl -fsSL <https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-doctor> | bash This prints out : Checking for `rbenv' in PATH: /usr/bin/rbenv Checking for rbenv shims in PATH: OK Checking `rbenv install' support: not found Unless you plan to add Ruby versions manually, you should install ruby-build. Please refer to <https://github.com/rbenv/ruby-build#installation> Counting installed Ruby versions: none There aren't any Ruby versions installed under `/home/accesss/.rbenv/versions'. You can install Ruby versions like so: rbenv install 2.4.1 Checking RubyGems settings: OK Auditing installed plugins: OK
Since it shows in the rbenv install support as not found, Let’s install ruby-build :
Heading over to https://github.com/rbenv/ruby-build#installation
It says we can install it as an rbenv plugin :
let’s run the following commands:
$ mkdir -p "$(rbenv root)"/plugins $ git clone <https://github.com/rbenv/ruby-build.git> "$(rbenv root)"/plugins/ruby-build
Ok let’s test it by running our rbenv-doctor script again:
$ curl -fsSL <https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-doctor> | bash Checking for `rbenv' in PATH: /usr/bin/rbenv Checking for rbenv shims in PATH: OK Checking `rbenv install' support: /home/accesss/.rbenv/plugins/ruby-build/bin/rbenv-install (ruby-build 20210928-4-g2b9424d) Counting installed Ruby versions: none There aren't any Ruby versions installed under `/home/accesss/.rbenv/versions'. You can install Ruby versions like so: rbenv install 3.0.2 Checking RubyGems settings: OK Auditing installed plugins: OK
That’s it. Installing rbenv includes ruby-build. Great we now have the ability to install ruby versions using rbenv directly
Installing Ruby Versions:
Now we have the ability to
$ rbenv install --list # lists all available versions of Ruby $ rbenv install 2.2.0 # installs a Ruby version 2.2.0 to ~/.rbenv/versions
$ rbenv install -l # lists all available version
For upgrading / uninstalling you can checkout rbenv’s readme https://github.com/rbenv/rbenv
Let’s install the latest stable ruby version (v 3.0.2)
$ rbenv install 3.0.2 Downloading ruby-3.0.2.tar.gz... -> <https://cache.ruby-lang.org/pub/ruby/3.0/ruby-3.0.2.tar.gz> Installing ruby-3.0.2... Installed ruby-3.0.2 to /home/accesss/.rbenv/versions/3.0.2 Let’s see if it is installed, run the following command, to check the list of installed ruby versions in our system, asterisk * represents the currently active : $ rbenv versions system (set by /home/accesss/.rbenv/version) 3.0.2 Let’s install one more ruby version (2.5.5), to test switching ! Also since i had a old project that needs to be upgraded from 2.5.5 to 3.0.2, so particularly this version : $ rbenv install 2.5.5 Downloading ruby-2.5.5.tar.bz2... -> <https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.bz2> Installing ruby-2.5.5... WARNING: ruby-2.5.5 is past its end of life and is now unsupported. It no longer receives bug fixes or critical security updates. Installed ruby-2.5.5 to /home/accesss/.rbenv/versions/2.5.5
Now if we check our installed versions it should show both the versions :
$ rbenv versions * system (set by /home/accesss/.rbenv/version) 2.5.5 3.0.2
This will set our ruby version for global use (for any shell) in our system by default :
Sets the global version of Ruby to be used in all shells by writing the version name to the ~/.rbenv/version file. This version can be overridden by a per-project .rbenv-version file, or by setting the RBENV_VERSION environment variable.
$ rbenv global 3.0.2 $ rbenv versions system 2.5.5 * 3.0.2 (set by /home/accesss/.rbenv/version) $ rbenv global 3.0.2
This sets our global environment to use 3.0.2
This will set our ruby version for a local (project level/directory basis) in our project, you can add a .ruby-version file and the version in to that file. so that whenever you want to tell rbenv to run that project, it will use the version mentioned in .ruby-version.
Sets a local per-project Ruby version by writing the version name to an .rbenv-version file in the current directory. This version overrides the global, and can be overridden itself by setting the RBENV_VERSION environment variable or with the rbenv shell command.
$ cd /yourproject$ rbenv local 2.5.5
This will setup local project ruby version to 2.5.5, you can check that using:
$ rbenv local 2.5.5 `` If you want to switch to 3.0.2 you can do : ```bash $ rbenv local 3.0.2
Great ! you are all set 🙂 Happy Hacking !