关于ruby-on-rails:别梦依稀咒逝川Ruby二十八年前M1-Mac-os配置Ruby300-on-Rails6112021最新

3次阅读

共计 4519 个字符,预计需要花费 12 分钟才能阅读完成。

原文转载自「刘悦的技术博客」https://v3u.cn/a_id_188

在每个开发者心里,都会有一门“最好”的语言,在这个世界的某个深处,在一些矫矫不群的人们心中,这门语言的名字叫做 Ruby,它往年二十八岁了,历史和 Java 一样的悠久,然而它没有大厂背书、它的性能被开发者诟病、时至今日仍然无奈高效利用多核资源,甚至于它每年都要被“死亡”一次,相比于有太阳计算机系统、甲骨文、IBM 这些大公司反对的 Java,它是那么的赤贫如洗,然而,它又领有全世界最虔诚的“信徒”,领有最沉闷的开发者社区,这所有,又让它是那么的包罗万象。是的,这就是 Rubyist 的理念:有的时候,你想证实给一万个人看,到起初,你发现只失去了一个明确的人,那就够了。

本次咱们尝试在最新的 M1 芯片 Mac os(Big Sur 11.2.2) 中搭建最新版 Ruby3.0.0 以及 Web 开发框架 Rails6.1.1,全新的芯片、全新的征途、全新的开始:

首先咱们来看看 M1 芯片的命令行,如果你是从老版本 Mac 迁徙过去的,比方笔者 (Mojave),最好将老的 Base 命令行更换成 zsh,zsh 是一款性能比 bash 更弱小的终端(shell)零碎,既能够作为一个交互式终端,也能够作为一个脚本解释器,这里更换必要性是指如果应用 Bash 编译 Ruby3.0,可能会产生一些未知谬误。执行命令切换 zsh:

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

装置胜利后,确保在应用程序 -》实用工具 -》终端 -》简介中,不要勾选 Rosetta,因为接下来咱们须要以 arm 架构的 homebrew 进行装置,所以所有的编译和运行动作都不须要 Rosetta 的参加:

随后重启终端,开始装置 amr 架构的 Homebrew:

/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"

而后编辑配置文件 ~/.zshrc,退出如下内容:

path=('/opt/homebrew/bin' $path)  
export PATH

存盘之后执行命令:

source ~/.zshrc

查看新 brew 的地位:

➜  ~ which brew  
/opt/homebrew/bin/brew

如果返回的是 /opt/homebrew/bin/brew 就阐明装置胜利,接着更新一下版本:

➜  ~ brew cleanup && brew update  
Already up-to-date.

如果没有代理,能够抉择设置一下国内源:

# brew  
git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git  
  
# core  
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git  
  
# cask  
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git  
  
  
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zprofile  
source ~/.zprofile

接下来咱们来装置 Ruby3.0,业界比拟支流的装置形式大抵两种:rvm 或者 rbenv,这里咱们应用 rbenv,它其实就是一个相似 python 中 conda 一样的多版本治理软件包,能够不便一些老我的项目以低版本 ruby 运行,比方 ruby2.6。

Ruby 依赖 OpenSSL 和 AutoConf 这俩个包,提前预装好,如果是迁徙过去的 Openssl 可能版本比拟低,最好重新安装最新的 1.1j 版本:

brew reinstall openssl@1.1  
brew reinstall autoconf

随后装置 rbenv,执行命令:

brew install ruby-build rbenv

之后将 rbenv 命令增加到 zsh 命令行的环境变量中:

echo 'if which rbenv > /dev/null; then eval"$(rbenv init -)"; fi' >> ~/.zshrc  
source ~/.zshrc

重启命令行,键入 rbenv:

➜  ~ rbenv  
rbenv 1.1.2  
Usage: rbenv <command> [<args>]  
  
Some useful rbenv commands are:  
   commands    List all available rbenv commands  
   local       Set or show the local application-specific Ruby version  
   global      Set or show the global Ruby version  
   shell       Set or show the shell-specific Ruby version  
   install     Install a Ruby version using ruby-build  
   uninstall   Uninstall a specific Ruby version  
   rehash      Rehash rbenv shims (run this after installing executables)  
   version     Show the current Ruby version and its origin  
   versions    List installed Ruby versions  
   which       Display the full path to an executable  
   whence      List all Ruby versions that contain the given executable

如果返回版本号和相干操作,问题就不大了,不过最好通过脚本诊断一下,确保后续编译不会出问题:

curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash

诊断没有报谬误即可:

➜  ~ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash  
Checking for `rbenv' in PATH: /opt/homebrew/bin/rbenv  
Checking for rbenv shims in PATH: OK  
Checking `rbenv install' support: multiple  
  You seem to have multiple `rbenv-install' in the following locations.  
  Please pick just one installation and remove the others.  
    
  /Users/liuyue/.rbenv/plugins/ruby-build/bin/rbenv-install  
  /opt/homebrew/bin/rbenv-install  
  
Counting installed Ruby versions: 1 versions  
Checking RubyGems settings: OK  
Auditing installed plugins: OK

接下来,因为家喻户晓的学术问题,rbenv 下载二进制安装包会十分的迟缓,所以咱们能够通过国内镜像来手动下载:https://cache.ruby-china.com/…

这里下载 ruby3.0 正式版:

而后将压缩包手动拷贝到 rbenv 的装置目录:~/.rbenv/cache

这里的~/.rbenv/cache 有可能不存在,能够手动创立:

mkdir ~/.rbenv/cache

拷贝安装包:

cp ~/Downloads/ruby-3.0.0.tar.gz ~/.rbenv/cache/ruby-3.0.0.tar.gz

紧接着咱们终于能够装置 Ruby3.0 本体了:

brew link openssl --force  
  
RUBY_CONFIGURE_OPTS=--with-openssl-dir=/opt/homebrew/Cellar/openssl@1.1/1.1.1j  rbenv install 3.0.0

因为笔者之前装置过 openssl,所以这次强制指定由 arm 架构的 openssl 来编译装置。

装置胜利后键入 rbenv versions:

➜  ~ rbenv versions       
* system (set by /Users/liuyue/.rbenv/version)  
  3.0.0

能够看到,除了 M1 零碎默认的版本,又呈现了一个 3.0.0 版本,咱们能够应用 rbenv global 命令来切换版本:

➜  ~ rbenv global 3.0.0   
➜  ~ rbenv versions  
  system  
* 3.0.0 (set by /Users/liuyue/.rbenv/version)

随后输出 ruby -v:

➜  ~ ruby -v  
ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [arm64-darwin20]

能够看到版本曾经切换到 3.0,并且内核版本是 arm64 位,如果须要零碎默认版本,还能够切回来:

➜  ~ rbenv global system  
➜  ~ ruby -v              
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.arm64e-darwin20]

零碎默认是 ruby 2.6.3,最好不要动它。

接着咱们就能够装置 Rails 了:

gem install rails -v 6.1.1

装置胜利后,刷新一下:

rbenv rehash

而后查看版本号:

➜  ~ rails -v  
Rails 6.1.1  
➜  ~

创立一个新我的项目:

rails new myrails

进入我的项目目录:

cd myrails

启动服务:

rails s

千呼万唤始进去:

结语:作为同龄语言,如果说 Java 是闪现在天上的绮丽,那么 Ruby 就是埋藏于地底的火热,同样平凡但各擅胜场,而事实上同样作为脚本语言的 Ruby 更多的是在和 Python 比照,Python 近几年在数据分析和深度学习畛域的突飞猛进让 Ruby 难以望其项背,而 Ruby 在 Mac 零碎中软件包治理层面却有着统治级的位置,就像你玩儿 Mac 就防止不了 Homebrew,接触 Homebrew 就无奈躲开 Ruby。很多人唱衰 Ruby,认为它过期了,而在 Rubyist 的心中则正相反,它太超前了,正是和 M1 芯片一样,是超过时代的产物,兴许有一天,它会“死亡”,但绝不会是明天,最初,用十九世纪美国小说家赫尔曼·梅尔维尔《白鲸》中的一节和诸君共勉:

“有些人死在涨潮里;有些人死在浅水滩里;有些人却死在洪水里。”——第一百三十五章, 亚哈最初一次追击白鲸时, 对拦截他的大副斯达巴克说。

原文转载自「刘悦的技术博客」https://v3u.cn/a_id_188

正文完
 0