Tag: Ruby

Install ruby 3.2.0 on asdf

Like clockwork, ruby 3.2.0 has been released on December 25 and given that our build tools now support it, it’s time to start trialing if our projects will be able to run on it without issue.

I’m using asdf to manage my node and ruby versions – switched them from rbenv and nvm respectively to make things a bit simpler.  Here’s what happened when I tried to install ruby 3.2.0 even after making sure asdf and the ruby plugin are updated.

That’s not good obviously.

From reading the snippet of the build error, we see two things it seems to be reinstalling openssl and is having trouble finding libyaml – both errors shouldn’t occur since my system already them  installed via homebrew.

Thankfully, the fix for this is simple.  We just need to make sure to point to existing libraries.

The lib that compiles ruby for us, ruby-build has a way to provide options for it’s builds, via RUBY_CONFIGURE_OPTS.  In my case all I needed to do is run this:

export RUBY_CONFIGURE_OPTS="--with-zlib-dir=$(brew --prefix zlib) --with-openssl-dir=$(brew --prefix [email protected]) --with-readline-dir=$(brew --prefix readline) --with-libyaml-dir=$(brew --prefix libyaml)"

After adding the ruby config opts, we’re now able to finally install ruby 3.2.0!

Add Gitlab CI to your Hanami project

I host my personal projects in gitlab because it’s free and it gives you free runners for your ci.  I recently started studying the Hanami framework in ruby and when it was time to build the test suite, I initially couldn’t properly get it to work because of a strange error.

My initial gitlab-ci.yml was as follows

image: ruby:2.5

services:
  - postgres:latest

variables:
  DATABASE_URL: 'postgresql://postgres/budget_tracker_test'
  POSTGRES_DB: budget_tracker_test
  HANAMI_ENV: test

cache:
  paths:
    - vendor/ruby

rspec:
  before_script:
    - gem install bundler  --no-ri --no-rdoc 
    - bundle install -j $(nproc) --path vendor --quiet

  script:
    - echo 'Trying to create db for develop'
    - bundle exec hanami db prepare
    - echo 'Trying to create db for test'
    - HANAMI_ENV=test bundle exec hanami db prepare
    - rspec spec

Pretty straightforward right? But for some reason I was encountering this error:

Could not find executable in your PATH: `createdb`
/builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-model-1.2.0/lib/hanami/model/migrator/postgres_adapter.rb:104:in `rescue in call_db_command'
	/builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-model-1.2.0/lib/hanami/model/migrator/postgres_adapter.rb:97:in `call_db_command'
	/builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-model-1.2.0/lib/hanami/model/migrator/postgres_adapter.rb:36:in `create'
	/builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-model-1.2.0/lib/hanami/model/migrator.rb:287:in `create'
	/builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-model-1.2.0/lib/hanami/model/migrator.rb:332:in `prepare'
	/builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-model-1.2.0/lib/hanami/model/migrator.rb:247:in `prepare'
	/builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-1.2.0/lib/hanami/cli/commands/db/prepare.rb:26:in `prepare_database'
	/builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-1.2.0/lib/hanami/cli/commands/db/prepare.rb:17:in `call'
	/builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-1.2.0/lib/hanami/cli/commands/command.rb:85:in `call'
	/builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-cli-0.2.0/lib/hanami/cli.rb:57:in `call'
	/builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/gems/hanami-1.2.0/bin/hanami:6:in `<top (required)>'
	/builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/bin/hanami:23:in `load'
	/builds/sylv3rblade/budget_tracker/vendor/ruby/2.5.0/bin/hanami:23:in `<top (required)>'
	/usr/local/lib/ruby/site_ruby/2.5.0/bundler/cli/exec.rb:74:in `load'
	/usr/local/lib/ruby/site_ruby/2.5.0/bundler/cli/exec.rb:74:in `kernel_load'
	/usr/local/lib/ruby/site_ruby/2.5.0/bundler/cli/exec.rb:28:in `run'
	/usr/local/lib/ruby/site_ruby/2.5.0/bundler/cli.rb:424:in `exec'
	/usr/local/lib/ruby/site_ruby/2.5.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
	/usr/local/lib/ruby/site_ruby/2.5.0/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
	/usr/local/lib/ruby/site_ruby/2.5.0/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
	/usr/local/lib/ruby/site_ruby/2.5.0/bundler/cli.rb:27:in `dispatch'
	/usr/local/lib/ruby/site_ruby/2.5.0/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
	/usr/local/lib/ruby/site_ruby/2.5.0/bundler/cli.rb:18:in `start'
	/usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.2/exe/bundle:30:in `block in <top (required)>'
	/usr/local/lib/ruby/site_ruby/2.5.0/bundler/friendly_errors.rb:124:in `with_friendly_errors'
	/usr/local/lib/ruby/gems/2.5.0/gems/bundler-1.16.2/exe/bundle:22:in `<top (required)>'
	/usr/local/bundle/bin/bundle:23:in `load'
	/usr/local/bundle/bin/bundle:23:in `<main>'
ERROR: Job failed: exit code 1

So I looked up some of the discussions on the issue tracker on Hanami’s github and I found this still open issue with Hanami still requiring a local postgres instance :|. Anyway, cart before the horse, I ended up using the postgres service while manually installing postgres in the ruby image just so I could run my specs proper.y

Here is my `gitlab-ci.yml` now

image: ruby:2.5

services:
  - postgres:latest

variables:
  DATABASE_URL: 'postgresql://postgres:[email protected]/budget_tracker_test'
  HANAMI_ENV: test

cache:
  paths:
    - vendor/ruby

before_script:
  - apt-get update -qq && apt-get install -y -qq postgresql postgresql-contrib libpq-dev cmake
  - gem install bundler  --no-ri --no-rdoc
  - bundle install -j $(nproc) --path vendor --quiet 

rspec:
  script:
  - bundle exec hanami db prepare
  - bundle exec rspec spec

After pushing this to gitlab, viola!