如何在最新Mac M1机器上配置Ruby环境

提示:为什么标题限定Mac M1上,因为再加入前团队说是没法正确配置,而我也是第一次做

主要内容

  • 1. 系统信息-System Information
  • 2. 配置后的环境版本-Environments
  • 3. 具体步骤-Detailed Steps
  • 4. 遇到的问题 - Issues
  • 5. 如何在VS Code中dubug
      • `!!!还不能正常在VS Code中debug, 有可以运行的可以留言`
    • Install Ruby Dependencies
    • Configuration of different type frameworks


最近由于一些原因换了工作,需要接触Ruby语言及框架,以及Cypress用于前端End-to-End和接口测试。记录以备后面的人用或者遇到相同情况的人参考


1. 系统信息-System Information

uname -a
Darwin EFVCDXLC5 21.5.0 Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000 arm64

Hardware Overview:

  Model Name: MacBook Pro
  Model Identifier: MacBookPro18,2
  Chip: Apple M1 Max
  Total Number of Cores: 10 (8 performance and 2 efficiency)
  Memory: 32 GB
  System Firmware Version: 7459.121.3
  OS Loader Version: 7459.121.3
  Hardware UUID: 8220F119-E329-5FAB-84E2-95D9D3DA1111
  Provisioning UDID: 00006001-001A51483CEB999E
  Activation Lock Status: Disabled

2. 配置后的环境版本-Environments

kielin@EFVCDXLC5 project1 % gem -v 
3.1.6
kielin@EFVCDXLC5 project1 % bundle -v
Bundler version 1.17.3
kielin@EFVCDXLC5 project1 % ruby -v
ruby 2.7.6p219 (2022-04-12 revision c9c2245c0a) [arm64-darwin21]
kielin@EFVCDXLC5 project1 %  openssl version
OpenSSL 1.1.1q  5 Jul 2022

3. 具体步骤-Detailed Steps

  • Install rbenv
# Install rbenv  
brew install rbenv

# Initialise rbenv  
rbenv init

#then verify rbenv
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-doctor|bash
  • Install openssl 1.1
    it looks like system built-in openssl not works for installing ruby, maybe its version too higher, so we will install the older version 1.1 with following command.
# Install openssl 1.1
brew install [email protected]

# Verify it
openssl version

# check installed path
 which openssl 
/opt/homebrew/opt/[email protected]/bin/openssl

add environment $PATH in file ~/.zshrc or profile if you are using bash

export PATH=/opt/homebrew/bin:$PATH
export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"
export PATH="/opt/homebrew/sbin:$PATH"
eval "$(rbenv init - zsh)"

export PATH=~/Desktop/Works/project1/lib/config/driver:$PATH

  • Install ruby
    !!! 需要注意的是这步骤
    you may fail to install the ruby only with command ’ rbenv install 2.7.6’, to install ruby please specify argument --with-openssl-dir=
 RUBY_CONFIGURE_OPTS="--with-openssl-dir=/opt/homebrew/opt/[email protected]"  rbenv install 2.7.6
  • Install bundler(Optional)
gem install bundler:1.17.3
  • Install package from gemfile
# Install packages
 bundle install
  • configure project
    change text in file .ruby-version
 # original is 2.3.7
 2.7.6
  • Add the following to your path variable:

/project1/lib/config/driver

  • Download super-TEST-CREDENTIALS-V2.yml and copy it into project1/lib/config

  • Download Chrome webdriver which match your local chrome version from http://chromedriver.storage.googleapis.com/index.html and copy it into project1/lib/config/driver

  • Run ‘rspec’ and click allow in the popup window

  • Go to System Preference - Security&Privacy - General, click ‘Allow Anyway’ for “chromedriver”

4. 遇到的问题 - Issues

  1. The main issue while installing ruby failed due to openssl version doesn’t match, so we need to install openssl
/Users/kielin/.rvm/src/ruby-2.7.6/lib/rubygems/core_ext/kernel_require.rb:83:in `require': cannot load such file -- openssl (LoadError)
	from /Users/kielin/.rvm/src/ruby-2.7.6/lib/rubygems/core_ext/kernel_require.rb:83:in `require'
	from /Users/kielin/.rvm/src/ruby-2.7.6/lib/rubygems/specification.rb:2430:in `to_ruby'
	from ./tool/rbinstall.rb:846:in `block (2 levels) in install_default_gem'
	from ./tool/rbinstall.rb:279:in `open_for_install'
	from ./tool/rbinstall.rb:845:in `block in install_default_gem'
	from ./tool/rbinstall.rb:835:in `each'
	from ./tool/rbinstall.rb:835:in `install_default_gem'
	from ./tool/rbinstall.rb:799:in `block in 
' from ./tool/rbinstall.rb:950:in `block in <main>' from ./tool/rbinstall.rb:947:in `each' from ./tool/rbinstall.rb:947:in `<main>' make: *** [do-install-nodoc] Error 1 +__rvm_make:0> return 2
  1. package in gemfile does not compatible with ruby, just downgrade the ruby version, or upgrade the package version
net-http-persistent-3.0.0 requires ruby version ~> 2.1, which is incompatible with the current version, ruby 3.0.4p208

5. 如何在VS Code中dubug

!!!还不能正常在VS Code中debug, 有可以运行的可以留言

我试了以下配置都不能正确的直接F5调试Ruby项目:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        // {
        //     "name": "RSpec - all",
        //     "type": "Ruby",
        //     "request": "launch",
        //     "cwd": "${workspaceRoot}",
        //     "program": "${workspaceRoot}/bin/rspec",
        //     "args": [
        //         "-I",
        //         "${workspaceRoot}"
        //     ]
        // },
        {
            "name": "RSpec",
            "type": "Ruby",
            "request": "launch",
            "cwd": "${workspaceRoot}",
            "program": "${workspaceRoot}/bin/bundle",
            "args": [
              "exec",
              "rspec",
              "${file}"
            ]
        },
    ]
}

https://github.com/rubyide/vscode-ruby/blob/main/docs/debugger.md

Install Ruby Dependencies

In this extension, we implement ruby debug ide protocol to allow VS Code to communicate with ruby debug, it requires ruby-debug-ide to be installed on your machine. This is also how RubyMine/NetBeans does by default.

  • If you are using JRuby or Ruby v1.8.x (jruby, ruby_18, mingw_18), run gem install ruby-debug-ide.
  • If you are using Ruby v1.9.x (ruby_19, mingw_19), run gem install ruby-debug-ide. Make sure ruby-debug-base19x is installed together with ruby-debug-ide.
  • If you are using Ruby v2.x
    • gem install ruby-debug-ide
    • gem install debase (or gem install byebug)

Configuration of different type frameworks

https://github.com/rubyide/vscode-ruby/wiki/5.-Example-configurations#rails

你可能感兴趣的:(Ruby,ruby,macos,watir,前端,接口自动化)