升級ruby版本那"不堪回首的經歷"

黃博文發表於2013-09-23

前段時間在玩Chef-一個IT基礎設施自動化工具。由於Chef是由Ruby寫的一個gem,那麼就需要安裝Ruby。當然Ruby我早就安裝了,並且使用rvm來管理Ruby及Gem。本來一切看似正常,但是不會預料後面會遇到那麼多的坑。

Chef本地的repo包中有一個vagrant虛擬機器的模板檔案,通過它可以setup一個虛擬機器,然後使用Chef來操作這個節點。那麼就使用vagrant命令來setup這個虛擬機器把。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ vagrant up --no-color
Vagrant failed to initialize at a very early stage:

It appears that you've ran a newer version of Vagrant on this
computer. Unfortunately, newer versions of Vagrant change internal
directory layouts that cause older versions to break. This version
of Vagrant cannot properly run.

If you'd like to start from a clean state, please remove the
Vagrant state directory: /Users/twer/.vagrant.d

Warning that this will remove all your boxes and potentially corrupt
existing Vagrant environments that were running based on the future
version.

console中說Vagrant版本有問題,那麼就按說明刪除.vagrant.d資料夾把。

1
2
3
4
5
6
$ rm -rf ~/.vagrant.d/
$ vagrant up --no-color
/Users/twer/sourcecode/chef-repo/Vagrantfile:8:in `<top (required)>': undefined method `configure' for Vagrant:Module (NoMethodError)
     from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/vagrant-1.0.7/lib/vagrant/config/loader.rb:115:in `load'
     from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/vagrant-1.0.7/lib/vagrant/config/loader.rb:115:in `block in procs_for_source'
     from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/vagrant-1.0.7/lib/vagrant/config.rb:41:in `block in capture_configures'

窩裡個去,又出新問題了。那好吧,看看當前ruby的版本。

1
2
3
4
5
6
7
8
9
10
$ rvm list

rvm rubies

   ruby-1.9.2-p320 [ x86_64 ]
=* ruby-1.9.3-p194 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

試著將Ruby的版本切換到1.9.2-p320再試試。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ rvm use ruby-1.9.2-p320
Using /usr/local/rvm/gems/ruby-1.9.2-p320
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Setting hostname...
[default] Mounting shared folders...
[default] -- /vagrant

搞定。看來問題是當前使用的ruby的版本有問題。

老是使用舊版本也不是個事,把Ruby升級到最新版本看還有這個問題不。

1
2
3
4
5
6
7
8
9
10
11
$ rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-p374]
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p320]
[ruby-]1.9.3[-p448]
[ruby-]2.0.0-p195
[ruby-]2.0.0[-p247]
[ruby-]2.0.0-head
ruby-head

看來ruby的1.9.3已經有p448了,那麼就升級到p448。

1
2
3
4
5
6
7
8
$ rvm install 1.9.3
Checking requirements for osx.
Installing requirements for osx.
Updating system.
Installing required packages: libksba, openssl..........
Error running 'requirements_osx_brew_libs_install gcc libksba openssl',
please read /usr/local/rvm/log/1379843991_ruby-1.9.3-p448/package_install_gcc_libksba_openssl.log
Requirements installation failed with status: 1.

升級失敗,那麼看看log。

1
2
3
4
5
6
7
8
9
10
11
12
==> make bootstrap
configure: error: cannot compute suffix of object files: cannot compile
See `config.log' for more details.
make[2]: *** [configure-stage1-target-libgcc] Error 1
make[1]: *** [stage1-bubble] Error 2
make: *** [bootstrap] Error 2

READ THIS: https://github.com/mxcl/homebrew/wiki/troubleshooting

There were package installation errors, make sure to read the log.

Try `brew tap --repair` and make sure `brew doctor` looks reasonable.

通過看log是gcc編譯失敗,並且通過log可以看出rvm是通過homebrew來安裝必備檔案的。

通過一番查資料後,找到了解決辦法。那就是將Xcode升級到最新版本,然後在Preference裡選擇Downloads標籤,然後安裝Command Line Tools。

升級ruby版本那"不堪回首的經歷"

安裝完畢後再用Homebrew安裝gcc49。

1
2
3
4
5
6
7
8
9
10
11
12
$ brew install gcc49
==> Downloading ftp://gcc.gnu.org/pub/gcc/snapshots/4.9-20130915/gcc-4.9-20130915.tar.bz2
Already downloaded: /Library/Caches/Homebrew/gcc49-4.9-20130915.tar.bz2
==> ../configure --build=x86_64-apple-darwin12.5.0 --prefix=/usr/local/Cellar/gcc49/4.9-20130915/gcc
==> make bootstrap
==> make install
==> Caveats
This is a snapshot of GCC trunk, which is in active development and
supposed to have bugs and should not be used in production
environment.
==> Summary
?  /usr/local/Cellar/gcc49/4.9-20130915: 977 files, 93M, built in 21.6 minutes

成功安裝GCC。

繼續回到安裝Ruby的任務上來。由於在安裝Ruby前會先安裝必備檔案,那麼幹脆直接先安裝必備檔案的了。

1
2
3
4
5
6
$ rvm requirements
Checking requirements for osx.
Installing requirements for osx.
mkdir: /usr/local/rvm/log/1379829979_ruby-1.9.3-p448: Permission denied
tee: /usr/local/rvm/log/1379829979_ruby-1.9.3-p448/update_system.log: No such file or directory
tee: /usr/local/rvm/log/1379829979_ruby-1.9.3-p448/update_system.log: No such file or directory

我勒個去,貌似是許可權問題,那麼用sudo解決。

1
2
3
4
5
6
7
8
9
$ sudo rvm requirements

Checking requirements for osx.
Installing requirements for osx.
Updating system.
Installing required packages: libksba, openssl..........
Error running 'requirements_osx_brew_libs_install libksba openssl',
please read /usr/local/rvm/log/1379843991_ruby-1.9.3-p448/package_install_libksba_openssl.log
Requirements installation failed with status: 1.

又失敗,看看錯誤日誌吧。

1
2
3
4
5
6
7
8
9
10
current path: /Users/twer
command(3): requirements_osx_brew_libs_install libksba openssl
Error: No such keg: /usr/local/Cellar/libksba
Error: Cowardly refusing to `sudo brew install`
You can use brew with sudo, but only if the brew executable is owned by root.
However, this is both not recommended and completely unsupported so do so at
your own risk.
There were package installation errors, make sure to read the log.

Try `brew tap --repair` and make sure `brew doctor` looks reasonable.

看了日誌貌似明白了,使用brew安裝libksba由於加了sudo所以出問題了。那麼直接在命令列使用sudo試試。

1
2
3
4
5
$ sudo brew install libksba
Error: Cowardly refusing to `sudo brew install`
You can use brew with sudo, but only if the brew executable is owned by root.
However, this is both not recommended and completely unsupported so do so at
your own risk.

窩裡個去,不使用sudo吧rvm requirements執行不成功。加上sudo吧rvm requirements呼叫的brew install又不行。好吧,就按上面說的將brew轉換到root模式。

1
2
3
4
5
6
ls -al /usr/local/bin/brew
-rwxr-xr-x  1 twer  admin  703 Mar 14  2013 /usr/local/bin/brew
$ sudo chown root:admin /usr/local/bin/brew
Password:
$ ls -al /usr/local/bin/brew
-rwxr-xr-x  1 root  admin  703 Mar 14  2013 /usr/local/bin/brew

OK。再執行吧。

1
2
3
4
5
$ sudo rvm requirements
Password:
Checking requirements for osx.
Certificates in '/usr/local/etc/openssl/cert.pem' already are up to date.
Requirements installation successful.

搞定。把brew再切換到原來的使用者和組。

1
$ sudo chown twer:admin /usr/local/bin/brew

繞了一大圈了終於可以安裝Ruby了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ rvm install 1.9.3
Searching for binary rubies, this might take some time.
No binary rubies available for: osx/10.8/x86_64/ruby-1.9.3-p448.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for osx.
Certificates in '/usr/local/etc/openssl/cert.pem' already are up to date.
Requirements installation successful.
Installing Ruby from source to: /usr/local/rvm/rubies/ruby-1.9.3-p448, this may take a while depending on your cpu(s)...
ruby-1.9.3-p448 - #downloading ruby-1.9.3-p448, this may take a while depending on your connection...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:02 --:--:--     0Warning: Failed to create the file ruby-1.9.3-p448.tar.bz2: Permission denied
  0 9816k    0   745    0     0    320      0  8:43:34  0:00:02  8:43:32  2623
curl: (23) Failed writing body (0 != 745)
There was an error(23).
Checking fallback: http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p448.tar.bz2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0Warning: Failed to create the file ruby-1.9.3-p448.tar.bz2: Permission denied
  0 9816k    0  3329    0     0   4578      0  0:36:35 --:--:--  0:36:35  8853
curl: (23) Failed writing body (0 != 3329)
There was an error(23).
Failed download
There has been an error fetching the ruby interpreter. Halting the installation.

又有錯。走到這步上了,繼續解決吧。經過分析貌似是下載檔案有問題,可能又是許可權問題。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
sudo rvm install 1.9.3
Password:
Searching for binary rubies, this might take some time.
No binary rubies available for: osx/10.8/x86_64/ruby-1.9.3-p448.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for osx.
Certificates in '/usr/local/etc/openssl/cert.pem' already are up to date.
Requirements installation successful.
Installing Ruby from source to: /usr/local/rvm/rubies/ruby-1.9.3-p448, this may take a while depending on your cpu(s)...
ruby-1.9.3-p448 - #downloading ruby-1.9.3-p448, this may take a while depending on your connection...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 9816k  100 9816k    0     0   422k      0  0:00:23  0:00:23 --:--:--  154k
ruby-1.9.3-p448 - #extracting ruby-1.9.3-p448 to /usr/local/rvm/src/ruby-1.9.3-p448
ruby-1.9.3-p448 - #extracted to /usr/local/rvm/src/ruby-1.9.3-p448
ruby-1.9.3-p448 - #configuring...............................................................................................................................................................................................................................................................................................................................................................................................................................................................................
ruby-1.9.3-p448 - #post-configuration
ruby-1.9.3-p448 - #compiling...............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
ruby-1.9.3-p448 - #installing.........................................................................................................
curl: (35) Unknown SSL protocol error in connection to rubygems.org:443
There was an error while trying to resolve rubygems version for 'latest'.
Halting the installation.

前面那個錯沒了,但是新來了一個錯。貌似是ssh到rubygems.org網站有問題,突然想起在rvm requirements時安裝了openssl,應該是ssl設定的問題。從網上查了下,貌似要重置macport。

1
2
3
4
5
6
$ sudo rm -rf /opt/local

$ sudo rm -rf $rvm_path/usr
$ sudo rm -rf $rvm_path/bin/port
sudo: cannot get working directory
$ sudo rvm autolibs homebrew

最後,再重新安裝Ruby。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
$ sudo rvm reinstall 1.9.3
Removing /usr/local/rvm/src/ruby-1.9.3-p448...
Removing /usr/local/rvm/rubies/ruby-1.9.3-p448...
Searching for binary rubies, this might take some time.
No binary rubies available for: osx/10.8/x86_64/ruby-1.9.3-p448.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for osx_brew.
Certificates in '/usr/local/etc/openssl/cert.pem' already are up to date.
Requirements installation successful.
Installing Ruby from source to: /usr/local/rvm/rubies/ruby-1.9.3-p448, this may take a while depending on your cpu(s)...
ruby-1.9.3-p448 - #downloading ruby-1.9.3-p448, this may take a while depending on your connection...
ruby-1.9.3-p448 - #extracting ruby-1.9.3-p448 to /usr/local/rvm/src/ruby-1.9.3-p448
ruby-1.9.3-p448 - #extracted to /usr/local/rvm/src/ruby-1.9.3-p448
ruby-1.9.3-p448 - #configuring...............................................................................................................................................................................................................................................................................................................................................................................................................................................................................
ruby-1.9.3-p448 - #post-configuration
ruby-1.9.3-p448 - #compiling...............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
ruby-1.9.3-p448 - #installing.........................................................................................................
Retrieving rubygems-2.1.4
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  356k  100  356k    0     0  11672      0  0:00:31  0:00:31 --:--:-- 20611
Extracting rubygems-2.1.4 ...
Removing old Rubygems files...
$LANG was empty, setting up LANG=en_US, if it fails again try setting LANG to something sane and try again.
Installing rubygems-2.1.4 for ruby-1.9.3-p448............................................................................................................................................
Installation of rubygems completed successfully.
Saving wrappers to '/usr/local/rvm/wrappers/ruby-1.9.3-p448'........

ruby-1.9.3-p448 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
ruby-1.9.3-p448 - #importing default gemsets, this may take time.......................
Install of ruby-1.9.3-p448 - #complete
Making gemset ruby-1.9.3-p448 pristine....
Making gemset ruby-1.9.3-p448@global pristine....

終於安裝上了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$ rvm list

rvm rubies

   ruby-1.9.2-p320 [ x86_64 ]
 * ruby-1.9.3-p194 [ x86_64 ]
=> ruby-1.9.3-p448 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

$ rvm use ruby-1.9.3-p448
$ rvm list

rvm rubies

   ruby-1.9.2-p320 [ x86_64 ]
 * ruby-1.9.3-p194 [ x86_64 ]
=> ruby-1.9.3-p448 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] VirtualBox VM is already running.

終於將Ruby升級到1.9.3-p448了。回顧這整個過程,都是淚啊。本來是想裝個虛擬機器玩玩,哪想到惹出這麼多事情來。不過好歹問題是解決了。

相關文章