Ruby on Rails development environment construction on M1 Mac

Introduction

Mac with M1 chip (M1 Mac) using homebrew, a package management tool for macOS The procedure for building a development environment for Ruby on Rails is described above.

It is based on MacBook Pro environment construction procedure with M1 chip.

Development environment for M1 Mac is a good reference for building a general-purpose environment on M1 Mac. (Since MacPorts is used for package management instead of Homebrew, that part is It is not very helpful when building an environment with Homebrew. )

About building an environment with Homebrew on M1 Mac I wrote Development environment on M1 Mac with Homebrew.

Advance preparation

Homebrew uses the features of Xcode to compile software, so first Install Xcode and Command Line Tools for Xcode.

Xcode installation

Install via the App Store.

Installing Command Line Tools for Xcode (CLT)

Start Xcode and select Xcode => Open Developer Tool => More Installed from the page displayed by clicking Developer Tools Download and install the CLT for your Xcode version.

Homebrew installation on M1 Mac

For M1 Mac, for now, the traditional installer is not available, except when using Rosetta2. You can not use it. Aside from the traditional installer, a package for M1 Mac (M1 Mac version) Is prepared.

Installation of the M1 Mac version can be found on the official homepage Alternative tetrahydrofuran. As you can see, the procedure is to extract a set of Homebrew tarballs to/opt/homebrew.

Under/opt/homebrew instead of under/usr/local /, which is the traditional installation destination The installation to is working well if you put it under/usr/local / It seems that there are cases where it is not done.

Intel Mac version under/usr/local, M1 Mac version under/opt/homebrew It seems that it is supposed to be installed and used together. (Personally, I don't recommend using the Intel Mac version and the M1 Mac version together.)

Currently, Intel Mac version and M1 Mac version are separated, and M1 Mac uses under/opt/homebrew. What we are doing is that it takes time to make the installer and the main unit compatible with both Intel and M1 As a result of temporary measures to provide Homebrew as soon as it becomes an M1 Mac, I can think of it.

Once Homebrew is compatible with both Intel and M1, users will be able to use Intel Mac or M1 Mac. You can install Homebrew without being aware of it, and install it under/usr/local You should be able to use the provided software.

Installation procedure

Create/opt/homebrew.

Since/opt is under the control of the root user, it cannot be tampered with by the logged-in user. Create a directory as root user with sudo & in the created directory Set the ownership of the logged-in user.

After installation in/opt/homebrew, in .zshrc (if shell is zsh) Add the path to Homebrew and reflect it in the shell.

Make sure you have the path to Homebrew, then install Homebrew I will update it.

% cd /opt
% sudo mkdir homebrew
% sudo chown -R $USER .
% curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew
% vi ~/.zshrc
export PATH=/opt/homebrew/bin:$PATH
% source ~/.zshrc
% brew -v
% brew update

Install rbenv

Install rbenv and ruby-build with Homebrew.

The path to rbenv and the rbenv initialization command at shell startup .zhrc (if the shell is zsh) Add to and reflect in the shell.

RBENV_ROOT is set to the directory where rbenv is installed. With this set, rbenv was installed when ruby ​​was installed with rbenv. Ruby will be installed in the directory.

If RBENV_ROOT is not set, when installing ruby ​​with rbenv Ruby will be installed under Users/[login username] /. Rbenv.

Make sure the path is in rbenv.

% brew install ruby-build
% brew install rbenv 
% vi ~/.zshrc
export PATH=/opt/homebrew/bin:$PATH
export PATH="/opt/homebrew/sbin:$PATH"
export RBENV_ROOT=/opt/homebrew/opt/rbenv
export PATH=$RBENV_ROOT/bin:$PATH
eval "$(rbenv init -)"
% source ~/.zshrc
% rbenv

Ruby installation

Since openssl and readline are required to install ruby, use Homebrew Install it.

Set the path to openssl and the environment variables required for ruby ​​compilation for openssl Add it to .zhrc (if the shell is zsh) and reflect it in the shell.

Check the installable version with rbenv, and check the version to install Select and install.

Update shims in rbenv.

Set the version of Ruby to work with rbenv.

Make sure that the version of Ruby you set is working.

%brew install readline
%brew install openssl
% vi ~/.zshrc
export PATH=/opt/homebrew/bin:$PATH
export PATH="/opt/homebrew/sbin:$PATH"
export RBENV_ROOT=/opt/homebrew/opt/rbenv
export PATH=$RBENV_ROOT/bin:$PATH
eval "$(rbenv init -)"

export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"
export LDFLAGS="-L/opt/homebrew/opt/[email protected]/lib"
export CPPFLAGS="-I/opt/homebrew/opt/[email protected]/include"
export PKG_CONFIG_PATH="/opt/homebrew/opt/[email protected]/lib/pkgconfig"
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=/opt/homebrew/opt/[email protected]"
% source ~/.zshrc
% rbenv install —-list 
% rbenv install 2.7.2
Installed ruby-2.7.2 to /opt/homebrew/opt/rbenv/versions/2.7.2
% rbenv rehash
% rbenv global 2.7.2
% ruby -v

RubyGems & Bundler Update

Update RubyGems and Bundler pre-installed in Ruby to the latest version.

% gem update --system
% gem update bundler

Install Node.js & Yarn

For Javascript package management in Ruby on Rails projects Since Node.js and Yarn are used in Webpacker etc, Node.js and Yarn is also installed.

% brew install node
% brew install yarn

Install Deno.js (bonus)

Not related to Ruby on Rails so far, but the same Javascript execution environment as Node.js is.

Continued here

Install libxml2 & libxslt & libiconv

I'm using Capybara for system testing in a Ruby on Rails project In some cases, Capybara depends on Nokogiri gem, which is used in the backend. These native libraries must be installed in advance.

Also, the path to these libraries and the programs that use these libraries Add the environment variable settings required at compile time to .zhrc (if the shell is zsh) and Reflect in the shell.

Bundler nokogiri installs these libraries with Homebrew Set to use during installation.

% brew install libxml2 libxslt libiconv
% vi ~/.zshrc
export PATH=/opt/homebrew/bin:$PATH
export PATH="/opt/homebrew/sbin:$PATH"
export RBENV_ROOT=/opt/homebrew/opt/rbenv
export PATH=$RBENV_ROOT/bin:$PATH
eval "$(rbenv init -)"

export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"
export LDFLAGS="-L/opt/homebrew/opt/[email protected]/lib"
export CPPFLAGS="-I/opt/homebrew/opt/[email protected]/include"
export PKG_CONFIG_PATH="/opt/homebrew/opt/[email protected]/lib/pkgconfig"
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=/opt/homebrew/opt/[email protected]"

export PATH="/opt/homebrew/opt/libxml2/bin:$PATH"
export LDFLAGS="$LDFLAGS -L/opt/homebrew/opt/libxml2/lib"
export CPPFLAGS="$CPPFLAGS -I/opt/homebrew/opt/libxml2/include"
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/opt/homebrew/opt/libxml2/lib/pkgconfig"

export PATH="/opt/homebrew/opt/libxslt/bin:$PATH"
export LDFLAGS="$LDFLAGS -L/opt/homebrew/opt/libxslt/lib"
export CPPFLAGS="$CPPFLAGS -I/opt/homebrew/opt/libxslt/include"
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/opt/homebrew/opt/libxslt/lib/pkgconfig"

export PATH="/opt/homebrew/opt/libiconv/bin:$PATH"
export LDFLAGS="-L/opt/homebrew/opt/libiconv/lib $LDFLAGS"
export CPPFLAGS="-I/opt/homebrew/opt/libiconv/include $CPPFLAGS"
% source ~/.zshrc
% bundle config build.nokogiri --use-system-libraries \
  --with-xml2-include=$(brew --prefix libxml2)/include/libxml2

Install libffi

Use Selenium WebDriver for system testing in Ruby on Rails projects The ffi gem on which this gem depends is using it in the backend The native library must be installed in advance.

Also, the path to libffi and required when compiling programs using this library Add the setting to the environment variable to .zhrc (if the shell is zsh) and reflect it in the shell.

% brew install libffi
% vi ~/.zshrc
export PATH="/opt/homebrew/opt/libffi/bin:$PATH"
export LDFLAGS="-L/opt/homebrew/opt/libffi/lib $LDFLAGS"
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/opt/homebrew/opt/libffi/lib/pkgconfig"
% source ~/.zshrc

Install LibSass (optional)

Use sassc-rails gem to compile Sass in Ruby on Rails project When doing, sassc-ruby used by this Gem is used in the backend LibSass must be installed in advance.

Up to Rais 5.0, sass-rails was widely used, but this Gem is using it. Since sass-ruby has been deprecated by the Sass team, after that It is recommended to use sassc-rails.

The other day, the Sass team deprecated LibSass, which is the reference implementation of Sass. Announcing recommendations for Dart Sass.

Sass : LibSass is deprecated

From now on, LibSass will be in maintenance mode, no new features of Sass will be added, only bug fix Please note that it will be done.

You may want to consider using Dart Sass via WebPacker.

% brew install --build-from-source libsass

Install ImageMagick & libvips (optional)

Gem etc. that uploads and displays images in the Ruby on Rails project Native that those gems use as a backend when using The library must be installed in advance.

ImageMagick used by Refile and CarrierWave has a Homebrew package. It seems to be M1 compatible, so it can be installed. (Installation failed several times After (I got rid of it on the way), the installation was successful, so I'm not sure if M1 support is complete. )

The image_processing gem used by ActiveStorage is the backend You can choose ImageMagick or libvips, development using Active Storage When doing it, I decided to use ImageMagick or libvips as the back end Become.

libvips can also be installed because the Homebrew package seems to be M1 compatible.

% brew install imagemagick
% brew install vips

Install Git

Install the standard Git source code control tool.

% brew install git

Database installation

Install PostgresSQL and MySQL.

I was able to install PostgreSQL smoothly, so the latest version of Homebrew The package seems to be M1 compatible.

MySQL has been installed several times before successful installation, and then Homebrew I was able to install it with package version 8.0.22_1. 5.x is still currently It seems that M1 is not supported, so be careful when using MySQL on M1 Mac.

PostgreSQL makes macOS login user superuser at installation It is set, but it is not good to operate PostgreSQL as a logged-in user So create a user postgres for operation, give it superuser privileges, Allow the postgres user to operate PostgreSQL.

Add the PostgresSQL data storage path to .zhrc (if the shell is zsh) and After applying to the shell, create a PostgresSQL database cluster.

Immediately after installing MySQL, the root user (superuser) has a password Since it is not set, hit mysql_secure_installation to secure it Let's strengthen it.

% brew install postgresql
% brew install mysql
% brew services start postgresql
% psql -U${USER} postgres
postgres=# create user postgres with SUPERUSER;
postgres=# exit
% brew services stop postgresql
% vi ~/.zshrc
export PGDATA="/opt/homebrew/var/postgres"
% source ~/.zshrc
% rm -rf /opt/homebrew/var/postgres
% brew services start postgresql
% initdb -U postgres --encoding=UTF-8 --locale=ja_JP.UTF-8
% brew services stop postgresql
% brew services start mysql
% mysql_secure_installation
% brew services stop mysql

Docker installation (bonus)

Docker for building development / execution environment in development project using Ruby on Rails I may be using it.

Continued here

Finally

What is the minimum installation procedure for the Ruby on Rails development environment on M1 Mac? that's all.

After that, install the necessary things in each environment and customize the development environment let's go.

Recommended Posts

Ruby on Rails development environment construction on M1 Mac
Rails6 development environment construction [Mac]
Ruby on Rails 6.0 environment construction memo
[Environment construction] Ruby on Rails 5.2 system development environment construction [within 1 hour]
[Environment construction Mac] Ruby on Rails (+ Webpacker handles errors)
Java development environment construction memo on Mac
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
[Environment construction] Get the Ruby on Rails 6 development environment within 1 hour
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Muscle Ruby on Rails Day 1 ~ Environment Construction ~
Ruby on Rails --From environment construction to simple application development on WSL2
Ruby on Rails development environment construction with Docker + VSCode (Remote Container)
Install Ruby / Rails on Mac M1 Big Sur 2021-01
[Personal memo] Ruby on Rails environment construction (Windows)
[Ruby on Rails] Let's build an environment on mac
Rails engineer environment construction ruby2.7.1
Rails environment construction Rails5.2.1 ruby2.5.1 Catalina
[Rails / MySQL] Mac environment construction
How to solve the local environment construction of Ruby on Rails (MAC)!
Build a Ruby on Rails development environment on AWS Cloud9
Docker the development environment of Ruby on Rails project
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
Ruby on Rails environment construction using VirtualBox, Vagrant, cyberduck
Ruby environment construction summary ~ mac version ~
Rails on Docker environment construction procedure
Install Java development environment on Mac
From 0 to Ruby on Rails environment construction [macOS] (From Homebrew installation to Rails installation)
Spring Boot environment construction memo on mac
Laravel development environment construction with Docker (Mac)
Ruby on Rails installation method [Mac edition]
Build a Java development environment on Mac
CentOS8.2 (x86_64) + ruby2.5 + Rails5.2 + MariaDB (10.3.17) environment construction
Build a development environment where Ruby on Rails breakpoints work on Windows
[ev3 × Java] leJOS development environment construction (Eclipse on Mac OSX / bluetooth)
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
Steps to build a Ruby on Rails development environment with Vagrant
Rails Docker environment construction
Building a Ruby environment for classes on Mac
Ruby on Rails basics
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
java development environment construction
Ruby On Rails Association
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
Create a Java development environment using jenv on Mac
Build Java development environment with VS Code on Mac
Java development environment construction on Mac-JDK Install (2020 preservation version)
[Mac] VS Code development environment construction (Java, Gradle, Node.js)
Stable development environment construction manual for "Rails6" with "Docker-compose"
[Ruby on Rails] From MySQL construction to database change
Ruby on rails learning record -2020.10.03
Ruby on rails learning record -2020.10.04
[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 ②
Java development environment (Mac, Eclipse)
Commentary on partial! --Ruby on Rails