How can I switch to ruby 1.9.3 installed using Hom

2019-01-07 04:05发布

I have installed ruby 1.9.3 using hombrew

brew install ruby

But default 1.8.7 is still used. How can I switch osx to use 1.9.3 as default ruby?

7条回答
小情绪 Triste *
2楼-- · 2019-01-07 04:15

In OSX you can change the path using:

sudo nano /etc/paths

And then add a path or change the order.

查看更多
该账号已被封号
3楼-- · 2019-01-07 04:22

I suggest you take a look at rvm. You can then set it as default with rvm use 1.9.3 --default

But if you are happy with your homebrew install.

Then just change the precedence of directories in the PATH

Here is my /etc/paths

# homebrews should always take precedence
/usr/local/bin

# the default stack
/usr/bin
/bin
/usr/sbin
/sbin

This is important generally for homebrew, else the system version of git, ruby, pg_admin,... will all be used instead of the brew version.

if you say which -a ruby you'll see all the installed rubies, and the precedence in the PATH

eg.

$ which -a ruby
/Users/matthew/.rvm/rubies/ruby-1.9.3-p0/bin/ruby
/Users/matthew/.rvm/bin/ruby
/usr/bin/ruby


UPDATE: I now don't think you should change /etc/paths

Instead you need to check which of .profile, .bashrc, or .bash_login is being loaded in your shell, and just add /usr/local/bin to your path.

For me, I only have a .profile. You can create that file if none of those files already exist in your home directory.

# homebrews should always take precedence
export PATH=/usr/local/bin:$PATH
查看更多
甜甜的少女心
4楼-- · 2019-01-07 04:24

SHORT ANSWER:

after installing ruby via homebrew just do this:

brew link --overwrite ruby

and restart or reopen your Terminal  


LONG ANSWER

So I did a normal install of ruby using homebrew

brew install ruby

that installed fine BUT it was still using the system's default ruby. which I verified by doing:

which ruby 
#/usr/bin/ruby

So as per Matthew Rudy's suggestion, I checked the order of my /etc/paths, and all was good.

Then I decided to do:

which -a ruby
#/usr/bin/ruby
#usr/local/bin/ruby

so nothing was broken as such. tried to reinstall ruby again using the homebrew method, and then i found it.

Homebrew mentioned:

Warning: ruby-2.3.1 already installed, it's just not linked

so had to do:

brew link --overwrite ruby
查看更多
The star\"
5楼-- · 2019-01-07 04:25

If you'd like to use homebrew to install 1.9.3, you can follow these steps:

$ brew update
$ brew install rbenv
$ brew install ruby-build

Once you have rbenv and ruby-build installed, you can run the following command to get Ruby 1.9.3 installed.

$ rbenv install 1.9.3-p125

Now if you’d like to use 1.9.3 by default, you can run the following command:

$ rbenv global 1.9.3-p125
查看更多
【Aperson】
6楼-- · 2019-01-07 04:25

Just as an alternative approach for anyone else looking for an answer to this - you can set an alias in your .bash_profile e.g

ruby="/usr/local/bin/ruby"

this is how i got around the issue

查看更多
smile是对你的礼貌
7楼-- · 2019-01-07 04:36

SHORT: Do note what you want to change it for.

If you're on OS X and trying to use Ruby for something like Jekyll, then don't use homebrew because that's what Apple is using for Ruby for and it might not be good to use if you're not sure what you're doing. Instead, use rbenv or RVM.

LESS SHORT: I was trying to switch from the default version to an updated version (from 2.0) to use Jekyll because it required Ruby version 2.2.5 and above. I updated it and version 2.5 was installed, but when I checked "ruby -v", it was still 2.0. Once I finally got around to changing the default version, I wasn't able to install the package I needed because I didn't have write permission. For example, if you come across something like this, then you probably are having the same problem

$ gem install jekyll bundler
ERROR:  While executing gem ... (Gem::FilePermissionError)    
    You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
查看更多
登录 后发表回答