Install Ruby / Rails on Mac M1 Big Sur 2021-01

I bought an M1 Mac mini, so I tried installing Ruby and Rails. In conclusion, as of January 2021, you can install it with Homebrew and rbenv without any problems. However, there is something strange about how Rails (development environment) works. Also, you may get an error when compiling 2.6.6.

Environment: Apple M1, macOS Big Sur, Ruby 2.6.6/2.7.2/3.0.0, Rails 6.1.0

Command line developer tools installation

Run the following command: The dialog "You need a command line developer tool to execute the" clang "command. Do you want to install the tool now?" Is displayed. Press "Install".

% clang

The same is true if you run the following command: You don't need to install Xcode.

% xcode-select --install

Installation of Homebrew and rbenv

Install Homebrew by copying the command from the Homebrew site (https://brew.sh/).

% /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Unlike Intel Mac, the installation directory will be / opt/homebrew, so add the Homebrew bin directory to the environment variable PATH. Edit .zshrc and reopen the terminal.

.zshrc


export PATH="/opt/homebrew/bin:$PATH"

Install ruby-build and rbenv.

% brew install ruby-build rbenv

Install OpenSSL 1.1.

% brew install openssl

Write the OpenSSL and rbenv settings in .zshrc. You can install the other environment variables you need without writing them. Reopen the terminal.

.zshrc


export PATH="/opt/homebrew/bin:$PATH"
export PATH="$(brew --prefix [email protected])/bin:$PATH"

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

eval "$(rbenv init -)"

Install Ruby with rbenv.

% rbenv install 2.7.2

Set the Ruby version to 2.7.2 with rbenv.

% rbenv local 2.7.2

Check the Ruby version. Look at the display "arm64" and get rid of it.

If the system ruby ​​command (/ usr/bin/ruby) remains, reopen the terminal.

% ruby -v
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [arm64-darwin20]

Ruby 3.0.0 is also included.

% rbenv install 3.0.0

2.6.6 Compile failure

2021-01-12 Addendum: Ruby compilation failed when installing 2.6.6 with rbenv. I'm getting the following error in the log: Failed in ext/fiddle/closure.c.

closure.c:264:14: error: implicit declaration of function 'ffi_prep_closure' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    result = ffi_prep_closure(pcl, cif, callback, (void *)self);

To prevent this, first install libffi with Homebrew.

% brew install libffi

Set the environment variable PKG_CONFIG_PATH to use libffi installed by Homebrew.

.zshrc


export PKG_CONFIG_PATH="/opt/homebrew/opt/libffi/lib/pkgconfig"

See also: https://github.com/rvm/rvm/issues/4968

Rails installation

I will raise the version of Bundler.

% gem install bundler -N

In the case of Ruby 3.0.0, bundle install will fail to install nokogiri, so specify the option first and install it (as of January 02, 2020).

% gem install nokogiri -- --use-system-libraries

Install the latest version of Rails. The -N option omits the ri documentation to speed up the installation.

% gem install rails -N

Check the version. If it's still in the system rails command (/ usr/bin/rails), reopen the terminal.

% rails --version
Rails 6.1.0

Install yarn.

% brew install node yarn

Let's create a new application.

% rails new generic

Launch the application.

% cd generic
% bin/rails s

I can access the page at http: // localhost: 3000 /, but I keep getting the following error (as of January 02, 2020).

E, [2021-01-02T20:15:05.838500 #91997] ERROR -- : Exception rescued in listen-worker_thread:
Errno::EBADARCH: Bad CPU type in executable - /Users/kazuto/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rb-fsevent-0.10.4/bin/fsevent_watch
/Users/kazuto/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rb-fsevent-0.10.4/lib/rb-fsevent/fsevent.rb:140:in `popen'

To prevent this, change config.file_watcher in the development environment settings.

config/environments/development.rb


# config.file_watcher = ActiveSupport::EventedFileUpdateChecker
  config.file_watcher = ActiveSupport::FileUpdateChecker

Bonus: PostgreSQL/MySQL installation

Install version 11 of PostgreSQL.

% brew install postgresql@11

Add the version 11 path in .zshrc. With this alone, the gem pg can be compiled.

.zshrc


export PATH="/opt/homebrew/bin:$PATH"
export PATH="$(brew --prefix [email protected])/bin:$PATH"
export PATH="$(brew --prefix postgresql@11)/bin:$PATH"
#The following is omitted

Start PostgreSQL.

% brew services start postgresql@11

Install MySQL 5.7.

% brew install [email protected]

Add the path in .zshrc.

.zshrc


export PATH="/opt/homebrew/bin:$PATH"
export PATH="$(brew --prefix [email protected])/bin:$PATH"
export PATH="$(brew --prefix postgresql@11)/bin:$PATH"
export PATH="$(brew --prefix [email protected])/bin:$PATH"
#The following is omitted

Start MySQL.

% brew services start [email protected]

When I install mysql2 of gem with bundler, I get the error ld: library not found for -lssl (as of 2021/01/02). Install mysql2 with options first.

% gem install mysql2 -- --with-opt-dir="$(brew --prefix [email protected])"

MySQL 5.7 should require a password, but if you install it with Homebrew, you can use it without a root password. To make the password mandatory, set it with the following command.

% mysql_secure_installation

Recommended Posts

Install Ruby / Rails on Mac M1 Big Sur 2021-01
Ruby on Rails development environment construction on M1 Mac
[Ruby] I can't install puma on Mac [Rails, etc.]
[Ruby on Rails] yarn install --check-files
Ruby on Rails installation method [Mac edition]
I can't install rails on my mac
Install gradle on mac
Install Java on Mac
Install Ruby on Ubuntu 20.04
Mac Rails Install Error
Ruby on Rails Elementary
Ruby on Rails basics
[Ruby on Rails] Let's build an environment on mac
Install openjdk11 on mac
Ruby On Rails Association
[Ruby on Rails] How to install Bootstrap in Rails
Install OpenJDK 8 on mac
[Environment construction Mac] Ruby on Rails (+ Webpacker handles errors)
I struggled with pip install on an M1 Mac
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Install Sidekiq pro on Rails
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Install Rails on macOS Catalina
Commentary on partial! --Ruby on Rails
Install Ruby 2.7 on RHEL 8 (AppStream)
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Install Ruby 2.7 on CentOS 7 (SCL)
Ruby on rails learning record -2020.10.06
Install tomcat + eclipse on mac
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
Install Ruby on Sakura's VPS
Install Ruby on MSYS2 with pacman
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
Ruby on Rails variable, constant summary
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
Install Ruby 2.5 on CentOS 7 using SCL
How to use Ruby on Rails
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
(Ruby on Rails6) "Erase" posted content
[Ruby on Rails] CSV output function
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] What is Bcrypt?
Use Docker on your M1 Mac
[Ruby on Rails] Confirmation page creation
Ruby On Rails devise routing conflict
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function