Ruby on windows, a week later

11:22 PM May 10 2020 code

A few days ago I wrote about how to get started on ruby using just windows (no WSL2). And so far it was a good experience, but with a few gotchas when I tried to get a client project working on the env.

No RVM or rbenv

Those tools are specific for linux/macos, but don't worry, the solution provided for windows works excellent: Let me introduce you to URU

If you installed ruby following my last post, you only need to add it bin path to uru.

To install uru you can just download the binary and add it to a folder that is already in your PATH env (so you can start using the cli in your terminal), or install it through scoop:

scoop install https://bitbucket.org/jonforums/uru/downloads/uru.json

Then you can add your existent ruby version to it with this command:

uru admin add C:\Ruby24-x64\bin

Then to install other ruby versions just download the installer from https://rubyinstaller.org/downloads/, while installing make sure that you don't overwrite any other stuff and that it just installs in a different folder. After you are done just run uru admin add <the_other_ruby_version_bin_path>

If you want to save disk space by re-using the devkit through different ruby versions, follow this guide. After that just install ruby versions without the devkit, they will fallback automatically to use it from the shared parent folder.

You can check your available ruby versions with uru ls and change to a different one using the names listed from that command, like this:

$ uru ls
    224p230     : ruby 2.2.4p230 (2015-12-16 revision 53155) [x64-mingw32]
    2410p364    : ruby 2.4.10p364 (2020-03-31 revision 67879) [x64-mingw32]
    266p146     : ruby 2.6.6p146 (2020-03-31 revision 67876) [x64-mingw32]

$ uru 2410p364
---> Now using ruby 2.4.10p364 tagged as `2410p364`

$ ruby --version
ruby 2.4.10p364 (2020-03-31 revision 67879) [x64-mingw32]

Gemsets will be scoped to those ruby versions. You can manage different gemsets for that ruby version using uru admin gemset init your_gemset_name in your specific project folder.

OpenSSL, rubygems and bundler

This seems to be a popular one, and a easy one to fix tho. In my project I have something like this in my gemfile:

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end

gem 'some_gems_from_github', github: '/usr/repo', branch: 'master'

source 'https://rails-assets.org' do
  gem 'rails-assets-tether', '>= 1.3.3'
end

The issue here, at least in fresh installations, will be related to https and openssl.
It will complain about the certificates while trying to fetch from these sources.

I highly suggest to use Chocolatey and Scoop to install this kind of stuff. In this case, we need OpenSSL: choco install openssl

If the error persists even after installing openssl (and restarting your terminal) you can try with regular http sources, or go with a better long term solution: fix your certificates.

In unix systems we usually have rvm or rbenv, and at least with rvm you have a script ready that fixes the rubygems certificates for you if the problem arises. In windows you will need to do that manually, but there is a really good step by step guide:

https://bundler.io/v2.0/guides/rubygems_tls_ssl_troubleshooting_guide.html#automated-ssl-check

The step that actually did the trick for me is to download the cacert.pem file in my ruby directory, and set a SSLCERTFILE env variable with the path to it.

Puma

So we had multiple workers and daemonized setup for development in this project, and neither of those options are available for windows.

This step is simple, just change your puma config to run normally with 1 worker for windows. Rubygems actually has a helper to check for windows os locally with Gem.win_platform?, so you can have a different puma setup for windows.

ActiveAdmin and method calls without parenthesis

Is a common practice in ActiveAdmin to write stuff like this:

#syntax error on windows, unexpected '{'
scope :completed { |scope| scope.where(completed: true) }

And I still can't figure out if the error belongs to the version of ruby that I'm using on this windows machine or to something else related with ActiveAdmin dsl... but it expects to have the explicit parenthesis on that scope method call, like this:

#This works fine on windows
scope(:completed) { |scope| scope.where(completed: true) }

Linux paths

This last one is obvious, but if you have a gem that requires something from linux systems (like custom loggers and stuff like that), they won't work. You can bypass them using again the Gem.win_platform? helper in their initializers or even in the Gemfile:

gem 'i_use_linux_paths' unless Gem.win_platform?

Final thoughts

Like I said in the first post, this is not intended to replace your Unix development environment, and it shouldn't since your code is 99% sure going to run in some kind of linux distro when you deploy it to production.

Still, is really handy to be able to do some quick maintenance work without having to jump into my macbook for it. The problems that I listed here were specific to this project, which only knew unix until this week (and its 3 years old). I expect to find different issues in different projects, and I plan to document them here if they pop up in the future.

Lastly, if you start a fresh new app in windows