原文转载自「刘悦的技术博客」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