ruby环境的安装

在Mac上安装ruby环境
  对于新入门的开发者,如何安装 Ruby和Ruby Gems 的运行环境可能会是个问题,本页主要介绍如何用一条靠谱的路子快速安装 Ruby 开发环境。
此安装方法同样适用于产品环境!
系统需求
首先确定操作系统环境,不建议在 Windows 上面搞,所以你需要用:
Mac OS X
任意 Linux 发行版本(Ubuntu,CentOS, Redhat, ArchLinux ...)
强烈新手使用 Ubuntu 省掉不必要的麻烦!

以下代码区域,带有 $ 打头的表示需要在控制台(终端)下面执行(不包括 $ 符号)

步骤0 - 安装系统需要的包

   # For Mac

   # 先安装 [Xcode](http://developer.apple.com/xcode/) 开发工具,它将帮你安装好 Unix 环境需要的开发包

步骤1 - 安装 RVM

RVM 是干什么的这里就不解释了,后面你将会慢慢搞明白。

    $ curl -L https://get.rvm.io | bash -s stable
期间可能会问你sudo管理员密码,以及自动通过homebrew安装依赖包,等待一段时间后就可以成功安装好 RVM。

然后,载入 RVM 环境(新开 Termal 就不用这么做了,会自动重新载入的)

    $ source ~/.rvm/scripts/rvm
检查一下是否安装正确

    $ rvm -v
    rvm 1.22.17 (stable) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]

步骤2 - 用 RVM 安装 Ruby 环境 

列出已知ruby版本:

           $ rvm list known

查询已安装的ruby:

          $ rvm list

卸载已安装的版本:

          $ rvm remove 2.2.0

安装ruby

  $rvm install 2.2.0 --disable-binary

同样继续等待漫长的下载,编译过程,完成以后,Ruby, Ruby Gems 就安装好了



步骤3 - 设置 Ruby 版本

RVM 装好以后,需要执行下面的命令将指定版本的 Ruby 设置为系统默认版本

    $ rvm 2.0.0 --default
同样,也可以用其他版本号,前提是你有用 rvm install 安装过那个版本

这个时候你可以测试是否正确

    $ ruby -v
    ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin13.0.0]

    $ gem -v
    2.1.6

这有可能是因为Ruby的默认源使用的是cocoapods.org,国内访问这个网址有时候会有问题,网上的一种解决方案是将远替换成淘宝的,替换方式如下:
     $gem source -r https://rubygems.org/
    $ gem source -a https://ruby.taobao.org
 要想验证是否替换成功了,可以执行:

    $ gem sources -l  

正常的输出结果:

      CURRENT SOURCES            

      http://ruby.taobao.org/            

到这里就已经把Ruby环境成功的安装到了Mac OS X上,接下来就可以进行相应的开发使用了。

例子:安装ruby2.2.2版本

这里我们安装2.2.2:
$rvm install 2.2.2
终端运行结果:(如果直接成功请绕过homebrew的卸载安装)
Searching for binary rubies, this might take some time.
Found remote file https://rvm_io.global.ssl.fastly.net/binaries/osx/10.11/x86_64/ruby-2.2.2.tar.bz2
Checking requirements for osx.
About to install Homebrew, press `Enter` for default installation in `/usr/local`,
type new path if you wish custom Homebrew installation (the path needs to be writable for user)
:
回车:
It appears Homebrew is already installed. If your intent is to reinstall you
should do the following before running this installer again:
    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
The current contents of /usr/local are .git
Requirements installation failed with status: 1.
这里执行:
$ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
卸载home-brew
Warning: This script will remove:
/Library/Caches/Homebrew/
/usr/local/.git/
Are you sure you want to uninstall Homebrew? [y/N] y
==> Removing Homebrew installation...
==> Removing empty directories...
==> Homebrew uninstalled!
You may want to restore /usr/local's original permissions
  sudo chmod 0755 /usr/local
  sudo chgrp wheel /usr/local
再执行:
$ rvm install 2.2.2
提示:
Searching for binary rubies, this might take some time.
Found remote file https://rvm_io.global.ssl.fastly.net/binaries/osx/10.11/x86_64/ruby-2.2.2.tar.bz2
Checking requirements for osx.
About to install Homebrew, press `Enter` for default installation in `/usr/local`,
type new path if you wish custom Homebrew installation (the path needs to be writable for user)
:
按回车:
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/mkdir /Library/Caches/Homebrew

Password:
这里需要输入电脑密码:
==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew
==> /usr/bin/sudo /usr/sbin/chown haha /Library/Caches/Homebrew
==> Downloading and installing Homebrew…
remote: Counting objects: 501, done.
remote: Compressing objects: 100% (445/445), done.
remote: Total 501 (delta 29), reused 360 (delta 27), pack-reused 0
Receiving objects: 100% (501/501), 787.83 KiB | 169.00 KiB/s, done.
Resolving deltas: 100% (29/29), done.
From https://github.com/Homebrew/brew
 * [new branch]      master     -> origin/master
HEAD is now at 32f7e73 download_strategy: ensure fixed commit hash length
==> Tapping homebrew/core
Cloning into '/usr/local/Library/Taps/homebrew/homebrew-core'…
remote: Counting objects: 3714, done.
remote: Compressing objects: 100% (3598/3598), done.
remote: Total 3714 (delta 14), reused 2112 (delta 6), pack-reused 0
Receiving objects: 100% (3714/3714), 2.88 MiB | 240.00 KiB/s, done.
Resolving deltas: 100% (14/14), done.
Checking connectivity… done.
Checking out files: 100% (3717/3717), done.
Tapped 3591 formulae (3,740 files, 9.0M)
==> Installation successful!
==> Next steps
Run `brew help` to get started
Further documentation: https://git.io/brew-docs
==> Homebrew has enabled anonymous aggregate user behaviour analytics
Read the analytics documentation (and how to opt-out) here:
  https://git.io/brew-analytics
Installing requirements for osx.
Updating system…..
Installing required packages: autoconf, automake, libtool, pkg-config, libyaml, readline, libksba, openssl……..
Certificates in '/usr/local/etc/openssl/cert.pem' are already up to date.
Requirements installation successful.
ruby-2.2.2 - #configure
ruby-2.2.2 - #download
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 6854k  100 6854k    0     0  61342      0  0:01:54  0:01:54 —:—:—  132k
ruby-2.2.2 - #validate archive
ruby-2.2.2 - #extract
ruby-2.2.2 - #validate binary
ruby-2.2.2 - #setup
ruby-2.2.2 - #gemset created /Users/haha/.rvm/gems/ruby-2.2.2@global
ruby-2.2.2 - #importing gemset /Users/haha/.rvm/gemsets/global.gems…………………………
ruby-2.2.2 - #generating global wrappers……..
ruby-2.2.2 - #gemset created /Users/haha/.rvm/gems/ruby-2.2.2
ruby-2.2.2 - #importing gemsetfile /Users/haha/.rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.2.2 - #generating default wrappers……..
Updating certificates in '/etc/openssl/cert.pem'.
mkdir: /etc/openssl: Permission denied
mkdir -p "/etc/openssl" failed, retrying with sudo
haha password required for 'mkdir -p /etc/openssl':
and sudo mkdir worked
这样ruby2.2.2就安装好了
现在就可以安装cocoapods啦:
$sudo gem install cocoapods



如果使用cocoapods过程中出现了错误:
xcrun: error: active developer path ("/Volumes/Xcode/Xcode.app/Contents/Developer") does not exist, use xcode-select to change
在终端中输入:sudo xcode-select  -switch /Applications/Xcode.app/Contents/Developer 就可以解决了! (注意空格不能少)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值