mac安装Ruby

由于Mac自带Ruby,所以加入对Ruby没有版本上的要求,无须安装。

Mac下安装Ruby,以下提供了两种方式。

一、直接安装

安装homebrew

1
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

brew 安装 Ruby 最新版本

1
2
$ brew update
$ brew install ruby

如果已经安装ruby,出错,提示升级:

1
2
Error: ruby 2.4.1_1 is already installed
To upgrade to 2.5.0_2, run `brew upgrade ruby`

升级:

1
$ brew upgrade ruby

查看版本及路径

1
2
3
4
5
//查看ruby版本
ruby --version

//查看ruby环境变量路径which ruby
which ruby

Rails

  1. 安装

    1
    $ sudo gem install rails
  2. 卸载

    1
    2
    $ gem uninstall rails
    $ gem uninstall railties

设置环境变量

参考环境变量设置

  1. 添加下面到~/.bash_profile/etc/profile
1
2
# Ruby
export PATH=/usr/local/bin/ruby:$PATH
  1. 生效
1
2
source ~/.bash_profile
source /etc/profile

二、通过RVM安装Ruby

安装

RVM官网指南如下:

1
2
$ gpg2 --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
$ \curl -sSL https://get.rvm.io | bash -s stable

其中,gpg需要自己安装,下载地址

推荐:不过也可以直接通过下面操作直接安装。

1
\curl -sSL https://get.rvm.io | bash -s stable --auto-dotfiles

如果遇到提醒:

1
WARNING: you have GEM_HOME="/Users/wenghengcong/.gem" this is conflicting with RVM, make sure to:

使用命令行:

1
$ source ~/.rvm/scripts/rvm

卸载

执行脚本如下:removeRVM.sh

1
2
3
4
5
#!/bin/bash
/usr/bin/sudo rm -rf $HOME/.rvm $HOME/.rvmrc /etc/rvmrc /etc/profile.d/rvm.sh /usr/local/rvm /usr/local/bin/rvm
/usr/bin/sudo /usr/sbin/groupdel rvm
/bin/echo "RVM is removed. Please check all .bashrc|.bash_profile|.profile|.zshrc for RVM source lines and delete
or comment out if this was a Per-User installation."

使用

  1. 安装某个版本

    1
    2
    3
    $ rvm install ruby-2.5.1
    // 安装最新的
    $ rvm install ruby --latest
  2. 移除某个版本

    1
    $ rvm remove 2.2.2

    假如遇到错误:

    1
    2
    3
    ruby-2.2.2 - #removing rubies/ruby-2.2.2 - please wait
    Error running '__rvm_rm_rf /Users/wenghengcong/.rvm/rubies/ruby-2.2.2',
    please read /Users/wenghengcong/.rvm/log/1520907262_ruby-2.2.2/remove.rubies.log

    就使用下面:

    1
    $ rvm fix-permissions

    之后,就能正常移除。

  1. 查看ruby版本

    1
    2
    3
    4
    5
    //查看当前所有可用版本
    $ rvm list

    //查看当前使用版本
    $ rvm current
  1. 使用版本

    1
    2
    $ rvm use system --default
    $ rvm use 2.2.2 --default
  2. gemset

    1
    2
    3
    4
    5
    //其中gemset2.4.1是gemset名称
    $ rvm gemset create gemset2.4.1
    $ rvm use --default 2.4.1@gemset2.4.1
    $ rvm gemset list
    $ rvm gemset delete gemset2.4.1

错误

  1. Warning! PATH is not properly set up, /.gem/bin is not at first place.

    参考:https://stackoverflow.com/a/23967102/4124634

    export PATH="$PATH:$HOME/.rvm/bin"

    修改为export PATH="$GEM_HOME/bin:$HOME/.rvm/bin:$PATH"

  2. Error running ‘./configure –prefix=/Users/wenghengcong/.rvm/rubies/ruby-2.5.1 –with-opt-dir=/usr/local/opt/libyaml:/usr/local/opt/readline:/usr/local/opt/libksba:/usr/local/opt/openssl@1.1 –disable-install-doc –enable-shared’,
    please read /Users/wenghengcong/.rvm/log/1539851512_ruby-2.5.1/configure.log
    There has been an error while running configure. Halting the installation.

    注意Xcode是否选择了command line tools.

#参考

如何快速正确的安装 Ruby, Rails 运行环境