How to install ruby on mac/ change ruby source in china

his one is tailor made for the Basix users among you. If you've been itching to try out Ruby and/or Rails, if the Terminal is somewhat new to you, you may find that even the process of installing it can generate countless confusing errors. This article will detail the exact steps you need to follow to get Ruby up and running on your Mac.

What you might be interested to know is that Ruby comes preinstalled on your Mac. Don't believe me? Open the Terminal and type:

Likely, the version number will return 1.8.7. While you might be tempted to stick with that, you probably shouldn't for a couple reasons:

  • Old versions of the OS shipped with a buggy version of Ruby
  • RVM provides the flexibility to use any version of Ruby that you require. Plus, if you're just starting out with Ruby, don't use an old version; you want 1.9.2!

These days, RVM is the way the cool kids install Ruby, and that's what we'll use.

"RVM lets you deploy each project with its own completely self-contained and dedicated environment--from the specific version of ruby, all the way down to the precise set of required gems to run your application. Having a precise set of gems also avoids the issue of version conflicts between projects, which can cause difficult-to-trace errors and hours of hair loss. "

Open the Terminal, and type:

If an error is returned when you run this command, make sure that you have Git installed. Don't worry, it's easy to setup!

Give that a few seconds to install, and next, we need to make RVM available to the shell. We'll do this by updating our ~/.bash_profile file.

Note that we're using Vim to update this file, which can be a bit confusing at first. Feel free to open this file in any code editor your prefer. Maybe you want to use mate .bash_profile. Also, note that, if this file does not exist, you should create it manually. Paste the following to the bottom of the page.

If you're using Vim, you'll need to press i to switch into Insert Mode first. Once the line has been pasted, press Escape, and then :wq! to save and close the file. If you're using a different code editor, then you know what to do!

Just to be safe, let's restart Terminal to make sure that everything took effect. To ensure that RVM is correctly installed, type:

You should see a long list of commands that are available to you. Good job; that part is out of the way.

From my experiences, the biggest gotcha is when you try to install a new version of Ruby with RVM, and you're met with a handful of confusing errors. Most of the time, this is the result of Apple's Xcode not being installed on your system, or, if it is, not being the current version.

You can download Xcode from Apple's App Store. Search for "xcode" and click the install button.

Download XCode

You might want to fix yourself some lunch, as this large file will take some time to download. Once it does, though, run the install process, and, when finished, close Xcode. You shouldn't need to restart your computer, but, if it keeps you in the good graces of the church, go ahead and do so.

Next, restart Terminal, and type:

You'll see a long list of versions...

For our needs, let's install Ruby 1.9.2

That shouldn't take but a moment.

If you still receive an error at this point, leave a comment below with your error message, and the rest of us will help you debug it. But, before doing so, type: rvm notes to determine if you've forgotten to install any dependencies.

Once the installation has completed, we need to tell RVM which version of Ruby we currently want to use:

Next, test it by checking the version number:

On my computer, this returns ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.1.0]. Good job, you're now using Ruby 1.9.2. There's just one last thing to deal with.

If you restart Terminal, and type ruby -v again, you'll likely find that it has defaulted back to the system version of Ruby: 1.8.7. That's no good! Let's be sure to make 1.9.2 the default.

This bit is identical to what we did just a moment ago - the only difference being that we've specified that 1.9.2 should be the default.

And that's it! You're all set to go! If you'd like to experiment with Ruby's syntax, type:

The next step is to install any gems that you require. For example, if you want to work with Ruby on Rails:

Or possibly Sinatra:

Important: Note that I'm not using sudo to install these gems. This is a big no-no when working with RVM. From the official docs:

"When you do sudo you are running commands as root, another user in another shell and hence all of the setup that RVM has done for you is ignored while the command runs under sudo (such things as GEM_HOME, etc...). So to reiterate, as soon as you 'sudo' you are running as the root system user which will clear out your environment as well as any files it creates are not able to be modified by your user and will result in strange things happening. (You will start to think that someone has a voodoo doll of your application...)"

Advertisement

If this tutorial seemed incredibly easy to you, that's because it wasn't intended for you. Not everyone is comfortable with the command line, and not everyone installs Ruby without first being met with a huge crop of errors. Hopefully, this article will help a bit. And, again, if you're still having trouble, leave a comment, and we'll do our best to help you out!

Alternatively to RVM, you also might look into rbenv, which, though quite new, is quickly gaining popularity.

安装命令 -> sudo gem install cocoapods

墙挡了.出这个错

ERROR:  While executing gem ... (Gem::RemoteFetcher::FetchError)
    Errno::ECONNRESET: Connection reset by peer - SSL_connect (https://api.rubygems.org/quick/Marshal.4.8/cocoapods-0.39.0.gemspec.rz)

这是因为ruby的软件源rubygems.org因为使用亚马逊的云服务,还是墙..,需要更新一下ruby的源,过程如下:

Could not find a valid gem 'cocoapods' (>= 0) in any repository

$ gem sources -l (查看当前ruby的源)
$ gem sources --remove https://rubygems.org/
$ gem sources -a https://gems.ruby-china.org/


$ gem sources -l

查看当前ruby的软件源

cuiwenlongdeMac-mini:~ cuiwenlong$ gem sources -l
*** CURRENT SOURCES ***

https://gems.ruby-china.org/

加进ruby源之后,就再次安装,成功了

cuiwenlongdeMac-mini:~ cuiwenlong$ sudo gem install cocoapods
Password:
Fetching: nap-1.0.0.gem (100%)
Successfully installed nap-1.0.0
Fetching: thread_safe-0.3.5.gem (100%)
Successfully installed thread_safe-0.3.5
Fetching: minitest-5.8.3.gem (100%)
Successfully installed minitest-5.8.3
Fetching: tzinfo-1.2 
原文地址:https://www.cnblogs.com/rosepotato/p/5380679.html