1. 程式人生 > >Rbenv在Mac上安裝Ruby

Rbenv在Mac上安裝Ruby

Mac OS X

Install Rbenv and Ruby 1.9.x - Mac OS X

First you’ll need to install a C compiler to build Ruby from source. Just download and install the latest version of Xcode from the App Store, if you don’t have it installed already. Also make sure you install the Command Line Tools by choosing the menu item Xcode -> Preferences...

 and click on the Downloads tab. Click on the Install button to download the Command Line Tools. Xcode Command Line Tools

Next, you’ll need to install the Homebrew package manager to get all the dependencies needed to compile Ruby from source. While you could manage these dependencies by hand, using a package manager is a better idea, as package managers know how to uninstall what they install.

Run the following command to install Homebrew:

$ ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

Run brew doctor and address any issues it discovers. When all is well, you should see:

$ brew doctor
Your system is raring to brew.

Next, install the additional dependencies to compile Ruby from source:

# For update-system
brew update
brew tap homebrew/dupes
# For ruby
brew install apple-gcc42

And now install rbenv and the ruby-build plugin:

brew update
brew install rbenv
brew install ruby-build

Add rbenv init to your shell to enable shims and autocompletion:

$ echo 'eval "$(rbenv init -)"' >> $HOME/.bash_profile
$ source ~/.bash_profile

Restart shell as a login shell so that the PATH changes take effect:

$ exec $SHELL -l

Install the latest version of ruby 1.9.x (at the time of this writing 1.9.3-p448)

$ rbenv install 1.9.3-p448

Rebuild the shim executable

$ rbenv rehash

You’ll need to run rbenv rehash every time you install a new version of Ruby or install a new gem.

Set the latest version of ruby to be the default version of ruby

$ rbenv global 1.9.3-p448

Verify the ruby install. If everything was installed correctly, the ruby -v command should report that version 1.9.3p448 is installed.

$ ruby -v
ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-darwin12.0.0]

Install Bundler - Mac OS X

You’ll need to use Bundler to manage gems. Installing a gem is also a good way to ensure that you’ve installed most of the Ruby prerequisites.

First, make sure you update to the latest version of Rubygems:

$ gem update --system

Then install the bundler gem. If the gem install command reports Successfully installedyou’re good to go:

$ gem install bundler
Successfully installed bundler-1.3.5
Parsing documentation for bundler-1.3.5
1 gem installed
$ rbenv rehash

Install Ruby 2.0.0 - Mac OS X

As of this writing, Ruby 2.0.0-p247 is the latest version of Ruby 2.0.0. Use rbenv install --listto print out the available versions. To install:

$ rbenv install 2.0.0-p247

To verify the install:

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

If you want to make Ruby 2.0.0 the global default version of ruby:

$ rbenv global 2.0.0-p247

Upgrade Rbenv - Mac OS X

To upgrade rbenv via homebrew:

$ brew update
$ brew upgrade rbenv
$ brew upgrade ruby-build

Remove Rbenv - Mac OS X

Uninstall the packages you installed via homebrew:

brew uninstall rbenv
brew uninstall ruby-build

Remove the following directory:

rm -rf $HOME/.rbenv

And remember to remove whatever you added to $HOME/.bash_profile

Linux

Install Rbenv and Ruby 1.9.x - Linux

Make sure the prerequisite packages are installed.

Ubuntu prerequisites:

$ sudo apt-get update
$ sudo apt-get install -y build-essential git
$ sudo apt-get install -y libxml2-dev libxslt-dev libssl-dev

RHEL/CentOS prerequisites:

$ sudo yum update
$ sudo yum install -y git
$ sudo yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel
$ sudo yum install -y libyaml-devel libffi-devel openssl-devel make bzip2
$ sudo yum install -y autoconf automake libtool bison
$ sudo yum install -y libxml2-devel libxslt-devel 

Download the rbenv source distribution to $HOME/.rbenv:

$ git clone git://github.com/sstephenson/rbenv.git $HOME/.rbenv

Add $HOME/.rbenv/bin to your $PATH

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> $HOME/.bashrc

Add rbenv init to your shell to enable shims and autocompletion:

$ echo 'eval "$(rbenv init -)"' >> $HOME/.bashrc

Restart shell as a login shell so that the PATH changes take effect:

$ exec $SHELL -l

Install the ruby-build plugin, which provides an rbenv install command to simplify the process of compiling and install new Ruby versions:

$ git clone git://github.com/sstephenson/ruby-build.git $HOME/.rbenv/plugins/ruby-build

Install the latest version of ruby 1.9.x (at the time of this writing 1.9.3-p448)

$ rbenv install 1.9.3-p448

Rebuild the shim executable

$ rbenv rehash

You’ll need to run rbenv rehash every time you install a new version of Ruby or install a new gem.

Set the latest version of ruby to be the default version of ruby

$ rbenv global 1.9.3-p448

Verify the ruby install. If everything was installed correctly, the ruby -v command should report that version 1.9.3p448 is installed.

$ ruby -v
ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-linux]