Solarian Programmer

My programming ramblings

Install Ruby 2.5 on macOS, Windows 10 and Ubuntu 18.04

Posted on September 22, 2018 by Paul

In this article I will show you how to install the latest stable version of Ruby, which is 2.5.1 at the time of this writing, on macOS, Windows 10 and Ubuntu 18.04.

Ruby 2.5 on macOS:

Start by installing the Command Line Tools for macOS. Please note, that you will need the Command Line Tools even if you’ve already installed Xcode. Open a Terminal and write:

1 xcode-select --install

Once the Command Line Tools are installed you will have a relatively new version of Ruby installed on your machine, 2.3 at the time of this writing. However, latest stable version of Ruby, 2.5.1, is faster and more up-to-date. Easiest way to have an up-to-date Ruby installed on macOS is to install rbenv which will let you chose between Ruby installations. The idea is to keep the system Ruby untouched and have a more modern version available.

In order to install rbenv we need to install Homebrew first:

1 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Next, we are ready to install rbenv:

1 brew install rbenv
2 rbenv init
3 echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile

Restart the Terminal, or write:

1 source ~/.bash_profile

If you want to install a particular version of Ruby you can use:

1 rbenv install 2.5.1
2 rbenv global 2.5.1

Check the Ruby version:

1 ruby -v

The above commands will install Ruby 2.5.1 and make it globally available. If you want to revert to the system Ruby version, use:

1 rbenv global system

If, for some reason, you want to be able use a different Ruby version per project, you can do it. Assuming that you have also installed Ruby 2.4.0, you can enable this locally in a project folder with:

1 cd my_project_path
2 rbenv local 2.4.0

After the above, Ruby 2.4.0 will be used in my_project_path and all subfolders of this path.

Ruby 2.4 on Windows 10 native install:

For historical reasons, Ruby always had better support on Unix like operating systems (e.g. Linux, macOS, …). If you can, I would recommend to install Ruby on WSL if you want to be able to follow most Ruby books and tutorials. Please note that in order to be able to use WSL you need a Windows 10 machine. That being said, let’s proceed with installing Ruby natively on Windows.

You can get the latest stable Ruby binary for Windows from RubyInstaller. I strongly recommend to chose one of the WITH DEVKIT options especially if you plan to install third party Ruby gems. In my case, I’ve used Ruby+Devkit 2.4.4-2 (x64). At the time of this writing Ruby+Devkit 2.5.1-2 (x64) has problems with gems like sqlite3 and rails. As a general rule, you should use the version that is recommended on the RubyInstaller download page.

Use the downloaded kit to install Ruby. Make sure to check Use UTF-8 as default external encoding and keep the defaults for the remaining settings.

In the last phase of the installation, after you’ve pressed Finish, you will be asked to chose between three MSYS2 options. Press Enter to select all 3 options. After the installation is finished, simply close the cmd.exe window.

Test your installation by using Start Command Prompt with Ruby, you should get a Windows Command Prompt in which you can use the ruby command. Check the version you’ve installed with:

1 ruby -v

You can install gems and use the ruby command in the above Start Command Prompt with Ruby. As mentioned before, Ruby is extensively tested on Unix. On Windows Ruby it is more of a second class citizen, so expect problems with some gems. (Personally, I’ve tested gosu, sinatra and rails with the native Ruby 2.4.4 installation.)

If you need to use an MSYS2 command, prefix it with ridk exec, e.g.:

1 ridk exec pacman --version

to see the version of pacman which is the MSYS2 package manager.

Ruby 2.5 on Windows 10 with WSL:

Install the Windows Subsystem for Linux, or WSL. You can check my previous article for a step by step tutorial. Once you have WSL installed, use the same steps as for a normal Ubuntu 18.04 system (see next part of this article).

Ruby 2.5 on Ubuntu 18.04:

Make sure you have an up-to-date system:

1 sudo apt update
2 sudo apt upgrade

Next, install some prerequisites:

1 sudo apt install -y build-essential libssl-dev libreadline-dev zlib1g-dev

Easiest way to have an up-to-date Ruby installed on Ubuntu is to install rbenv which will let you chose between Ruby installations. The idea is to keep the system Ruby untouched and have a more modern version available.

In order to install rbenv use:

1 wget -q https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-installer -O- | bash
2 echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
3 rbenv init
4 echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bashrc
5 source ~/.bashrc

If you want to install a particular version of Ruby you can use:

1 rbenv install 2.5.1
2 rbenv global 2.5.1

Check the Ruby version:

1 ruby -v

The above commands will install Ruby 2.5.1 and make it globally available. If you want to revert to the system Ruby version, use:

1 rbenv global system

If, for some reason, you want to be able use a different Ruby version per project, you can do it. Assuming that you have also installed Ruby 2.4.0, you can enable this locally in a project folder with:

1 cd my_project_path
2 rbenv local 2.4.0

After the above, Ruby 2.4.0 will be used in my_project_path and all subfolders of this path.


Show Comments