Dev environment setup failed on bundle install

AI-generated summary

In a discussion about setting up a Rails development environment on a Macbook Air M1, uxgnod encountered an error during bundle install related to OpenSSL version compatibility, specifically failing to find OpenSSL >= 1.0.1 and < 3.0.0. The error occurred while building the openssl-2.2.1 gem native extension. wilson29thid shared that they faced the same issue and resolved it using a Stack Overflow solution, later also needing another Stack Overflow fix for a PostgreSQL-related problem. Separately, th21 asked how to switch the global Ruby version on an M1 Mac, mentioning that asdf global ruby 3.1.2 did not update their Ruby version from 2.5.0p0. The thread reflects common M1-specific setup hurdles involving Ruby, OpenSSL, and PostgreSQL, with community-provided workarounds.

Hi all, I want to learn rails development by reading its code and try to contribute to it.

I just followed this link to set up my development environment on Macbook Air M1

In the bundle install step, I got an error about openssl, I don’t know how to solve this, can someone help me?
Here is the error messages

...
Installing actionmailer 6.1.4.1
Installing sprockets-rails 3.4.2
Installing omniauth-github 1.4.0
Installing railties 6.1.4.1
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/openssl-2.2.1/ext/openssl
/Users/dongxu/.rbenv/versions/2.7.3/bin/ruby -I /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/site_ruby/2.7.0 -r
./siteconf20220110-88454-17mhnii.rb extconf.rb
checking for t_open() in -lnsl... no
checking for socket() in -lsocket... no
checking for openssl/ssl.h... yes
checking for LIBRESSL_VERSION_NUMBER in openssl/opensslv.h... no
checking for OpenSSL version >= 1.0.1 and < 3.0.0... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
	--with-opt-dir
	--without-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/Users/dongxu/.rbenv/versions/2.7.3/bin/$(RUBY_BASE_NAME)
	--with-openssl-dir
	--with-openssl-include
	--without-openssl-include=${openssl-dir}/include
	--with-openssl-lib
	--without-openssl-lib=${openssl-dir}/lib
	--with-kerberos-dir
	--without-kerberos-dir
	--with-kerberos-include
	--without-kerberos-include=${kerberos-dir}/include
	--with-kerberos-lib
	--without-kerberos-lib=${kerberos-dir}/lib
	--with-debug
	--without-debug
	--enable-debug
	--disable-debug
	--with-nsl-dir
	--without-nsl-dir
	--with-nsl-include
	--without-nsl-include=${nsl-dir}/include
	--with-nsl-lib
	--without-nsl-lib=${nsl-dir}/lib
	--with-nsllib
	--without-nsllib
	--with-socket-dir
	--without-socket-dir
	--with-socket-include
	--without-socket-include=${socket-dir}/include
	--with-socket-lib
	--without-socket-lib=${socket-dir}/lib
	--with-socketlib
	--without-socketlib
	--with-openssl-config
	--without-openssl-config
	--with-pkg-config
	--without-pkg-config
extconf.rb:113:in `<main>': OpenSSL >= 1.0.1, < 3.0.0 or LibreSSL >= 2.5.0 is required (RuntimeError)

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/extensions/arm64-darwin-21/2.7.0/openssl-2.2.1/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/openssl-2.2.1 for inspection.
Results logged to /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/extensions/arm64-darwin-21/2.7.0/openssl-2.2.1/gem_make.out

  /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/site_ruby/2.7.0/rubygems/ext/builder.rb:95:in `run'
  /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/site_ruby/2.7.0/rubygems/ext/ext_conf_builder.rb:47:in `block in build'
  /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/2.7.0/tempfile.rb:291:in `open'
  /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/site_ruby/2.7.0/rubygems/ext/ext_conf_builder.rb:26:in `build'
  /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/site_ruby/2.7.0/rubygems/ext/builder.rb:161:in `build_extension'
  /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/site_ruby/2.7.0/rubygems/ext/builder.rb:195:in `block in build_extensions'
  /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/site_ruby/2.7.0/rubygems/ext/builder.rb:192:in `each'
  /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/site_ruby/2.7.0/rubygems/ext/builder.rb:192:in `build_extensions'
  /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/site_ruby/2.7.0/rubygems/installer.rb:847:in `build_extensions'
/Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/rubygems_gem_installer.rb:66:in
`build_extensions'
  /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/rubygems_gem_installer.rb:26:in `install'
  /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/source/rubygems.rb:192:in `install'
  /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/installer/gem_installer.rb:54:in `install'
/Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/installer/gem_installer.rb:16:in
`install_from_spec'
/Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/installer/parallel_installer.rb:186:in
`do_install'
/Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/installer/parallel_installer.rb:177:in `block
in worker_pool'
  /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/worker.rb:62:in `apply_func'
  /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/worker.rb:57:in `block in process_queue'
  /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/worker.rb:54:in `loop'
  /Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/worker.rb:54:in `process_queue'
/Users/dongxu/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/bundler-2.2.26/lib/bundler/worker.rb:91:in `block (2 levels) in
create_threads'

An error occurred while installing openssl (2.2.1), and Bundler cannot continue.

In Gemfile:
  cose was resolved to 1.2.0, which depends on
    openssl-signature_algorithm was resolved to 1.1.1, which depends on
      openssl

I ran into this issue on a new m1 macbook. This stack overflow post had the solution. I later needed this post to run bundle install because of a postgres issue. Hope that helps!

How do you switch global ruby version? I tried asdf global ruby 3.1.2 but still stuck on ruby 2.5.0p0 on Mac m1