LoginSignup
27
24

More than 5 years have passed since last update.

Cygwin で RubyとGitをコンパイルして使おう

Last updated at Posted at 2013-12-02

Cygwinを使ってRuby、Git最新版(今回は1.8.5)、Vagrantを使えるようにしてみた。

※投稿した方法でGitをインストールした場合、git cloneをhttps転送で実行するとfatal: Unable to find remote helper for 'https'エラーになることが判明。
こちらのブログに、その原因と対処を記載。2014/3/20 追記

Cygwin setup

ポイントのみ記載する。詳細手順についてはgoogle先生に聞くと画像付で教えてくれるので割愛した。

ディレクトリとバージョン

  • E:\cygwin ディレクトリを作ってそこへ setup-x86.exe を保存。exeを実行するとパッケージの保管先を聞いてくるので、 E:\cygwin\packages とした。ダウンロードサイトは日本なら http://ftp.jaist.ac.jp などでいいと思う。
  • OSがWindowsXPなので32 bit版を使った。このときのCygwin setupバージョンは2.8.31。

インストールパッケージ

  • Base/gawk *

  • Base/tar *

  • Devel/git

  • Utils/bzip2

  • Web/wget

※アスタリスク印はデフォルトでチェックが入っているので、実質「git、bzip、wget」のみチェックを入れた。

  • インストール後、E:\cygwin\etc\passwd ファイルを編集。使うユーザーのグループIDを545(Users)に変更。ホームディレクトリを変える場合はここで。例では”/home/user”に変更。
  :
- username:unused:1004:513:username,U-WINXP\username,S-1-...-1004:/home/username:/bin/bash
+ username:unused:1004:545:username,U-WINXP\username,S-1-...-1004:/home/user:/bin/bash
  :
  • E:\cygwin\etc\group ファイルを編集。なしをnobodyに変更。
  :
- なし:S-1-...-513:513:
+ nobody:S-1-...-513:513:
  :

起動バッチとhomeディレクトリを用意

  • E:\cygwin へ Cygwin_mintty.bat を作成。
Cygwin_mintty.bat
@echo off
%~d0
set cygdrive=%~d0
set cygdir=%~dp0
cd %~dp0\bin

set PATH=%SystemRoot%\system32
set MAKE_MODE=unix
set CYGWIN=noacl nontsec nosmbntsec
set SHELL=/bin/bash
set HOME=

bash.exe -c "/usr/bin/mintty -&"
exit
  • 作った Cygwin_mintty.bat を実行。minttyでbashが起動するか確認。

  • Cygwin実行後、Windowsでもわかりやすい場所にhomeディレクトリを用意(例としてD:\home\user)。Cygwinからシンボリックリンク作成。

$ cd /home
$ mv user user_bk                    # 元のhomeディレクトリを退避
$ ln -s /cygdrive/d/home/user/ user  # D:\home\userをcygwinのhomeディレクトリとして見えるようにする
$ cp user_bk/.* user/                # ドットファイルを新しいhomeディレクトリにコピー

apt-cygのインストール

当初、オリジナルのapt-cygを使ったが、cygwinのミラーサイトの構造が変わったようで、パッケージがうまくダウンロードできなかった。
なので、下記のgithubリポジトリのapt-cygを使わせていただく。

(詳細:http://wiki.livedoor.jp/kou1okada/d/apt-cyg

$ mkdir /tmp/apt-cyg
$ cd /tmp/apt-cyg
$ git clone https://github.com/kou1okada/apt-cyg .
$ chmod +x apt-cyg
$ mv apt-cyg /bin/
$ apt-cyg --use-setuprc
$ apt-cyg update

コンパイラ/ライブラリ/ツールのインストール

$ apt-cyg install gcc-core
$ apt-cyg install make patch vim
$ apt-cyg install libncurses-devel openssl-devel readline libsqlite3-devel libxml2-devel libxslt-devel

git最新版(1.8.5)インストール

事前準備とgitのコンパイル

$ apt-cyg install autoconf zlib zlib-devel libiconv python openssh tcl
$ apt-cyg install gettext-devel
$ cd /tmp
$ git clone git://github.com/git/git.git
$ cd git
$ make configure
$ ./configure --prefix=/usr/local
$ make all
$ make install
$ cd
$ git --version
  : # 古いgitのバージョンが表示される
$ export PATH=/usr/local/bin:$PATH
$ git --version
  : # 新しいgitのバージョンが表示される
$ echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bash_profile

git1.8.5のmanページのインストール

$ mkdir /tmp/git-manpages
$ cd /tmp/git-manpages
$ wget https://www.kernel.org/pub/software/scm/git/git-manpages-1.8.5.tar.gz --no-check-certificate
$ mkdir /usr/local/man
$ tar xvfz git-manpages-1.8.5.tar.gz -C /usr/local/man
$ man git
  : # 最下段までスクロールするとバージョンが表示されるので確認。

Rubyのインストール

(方法1)Rubyをコンパイルしてインストール

$ mkdir /tmp/ruby
$ cd /tmp/ruby
$ wget http://cache.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p353.tar.gz
$ tar xvfz ruby-2.0.0-p353.tar.gz
$ cd ruby-2.0.0-p353
$ ./configure
$ make
$ make install
$ ruby -v

(方法2)rbenvでインストール

  • rbenvのインストール

    $ git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
    $ echo 'export PATH="$PATH:$HOME/.rbenv/bin"' >> ~/.bash_profile
    $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
    $ source ~/.bash_profile

  • ruby-buildのインストール

$ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/rucby-build
  • Rubyのインストール
$ rbenv install --list
  : # インストールできるrubyバージョンのリストが出るので、お目当てのバージョンを探す
$ rbenv install 2.0.0-p247
  : # めっちゃ時間かかる(笑)
$ rbenv rehash
$ rbenv global 2.0.0-p247
$ which ruby
  : # rubyのインストール場所を確認
$ ruby -v
$ gem -v

.gemrc の作成

これを作っておくと、gemインストール時にドキュメントが含まれずインストールが早くなる。

echo 'gem: --no-document' > .gemrc

VirtualBoxのインストール

  • Windowsインストーラで4.2.18をインストール

  • インストール先はD:\opt\VirtualBox

  • D:\opt をcygwin起動時に /opt へマウントするように fstab ファイルを編集する。

    :
d:\opt /opt ntfs override,noacl,binary,auto 0 0
  • fstabの反映と、VirtualBoxディレクトリのパスを通す。
$ mount -a
$ export PATH="/opt/VirtualBox:$PATH"
$ echo 'export PATH="/opt/VirtualBox:$PATH"' >> ~/.bash_profile

Vagrantのインストール

  • 1.3.5を使用

  • D:\opt\Vagrant へインストール

  • Vagrant設定ファイルの保管場所を設定し、binディレクトリのパスを通す。

$ export VAGRANT_HOME="$HOME/.vagrant.d"
$ echo 'export VAGRANT_HOME="$HOME/.vagrant.d"' >> ~/.bash_profile
$ export PATH="/opt/Vagrant/bin:$PATH"
$ echo 'export PATH="/opt/Vagrant/bin:$PATH"' >> ~/.bash_profile
  • Vagrantにおまじない
$ dos2unix /opt/Vagrant/bin/vagrant

(任意)gitコミットログをsakuraエディタで書けるようにしておく

  • cygwin独特のパスを解決させるようにスクリプトを組んでおく。

  • d:/opt/sakura/sakura.exeにある前提。

#!/bin/bash
argv=("$@")

for i in `seq 1 $#` ; do
  case ${argv[$i-1]} in
    -*) ;;                                        # オプション引数はそのまま
    *) argv[$i-1]=`cygpath -w "${argv[$i-1]}"` ;; # ファイルパスはcygpathで変換
  esac
done

/cygdrive/d/opt/Sakura/sakura.exe ${argv[@]}

  :
[core]
  editor = 'sakura' \"$1\" \"-CODE=4 -GROUP=9\"
  :

(任意)git-completionの利用とプロンプトにgitブランチ名を表示する

git-completionの利用とプロンプトにgitブランチ名を表示する場合は、gitコンパイル時に /tmp/git/contrib/completion ディレクトリに .git-completion.* と git-prompt.sh ファイルが生成されているので、それをhomeディレクトリに.ファイルとしてコピーして、.bashrcに下記を追加する。

  :
# git-completionの設定
if [ -e "${HOME}/.git-completion.bash" ] ; then
  source "${HOME}"/.git-completion.bash
fi

# プロンプト
PS1='\[\033]0;Cygwin:${PWD//[^[:ascii:]]/?}\007\]'   # set window title
PS1="$PS1"'\n'                                       # new line
PS1="$PS1"'\[\033[32m\]'                             # change color
PS1="$PS1"'\u@\h '                                   # user@host<space>
PS1="$PS1"'\[\033[33m\]'                             # change color
PS1="$PS1"'\w'                                       # current working directory
if [ -e "${HOME}/.git-prompt.sh" ] ; then
  source "${HOME}"/.git-prompt.sh
  PS1="$PS1"'$(__git_ps1 "(%s)")'                    # git branch name
fi
PS1="$PS1"'\[\033[0m\]'                              # change color
PS1="$PS1"'\n'                                       # new line
PS1="$PS1"'\$ '                                      # prompt: # or prompt: $
export PS1
  :

その他

gemなどをインストールして使おうとすると、下記のようなメッセージが出るときがある。
この場合、「cygwin gem インストール rebaseall」でgoogle先生に尋ねると良い。

$ gem update --system
Updating rubygems-update
Fetching: rubygems-update-2.1.11.gem (100%)
Successfully installed rubygems-update-2.1.11
Installing RubyGems 2.1.11
      1 [main] ruby 2180 child_info_fork::abort: unable to remap openssl.so to same address as parent (0x1080000) - try running rebaseall
      0 [main] ruby 2160 child_info_fork::abort: unable to remap fcntl.so to same address as parent (0x2410000) - try running rebaseall
      1 [main] ruby 2828 child_info_fork::abort: unable to remap openssl.so to same address as parent (0x1080000) - try running rebaseall
  :
  • (参考)cygwin上で下記を実行。
$ find /usr -name '*.so' > /tmp/rebase_so.lst
$ find /usr -name '*.dll' >> /tmp/rebase_so.lst
  • (参考)ashを起動して下記を実行。
$ /bin/rebaseall -T /tmp/rebase_so.lst -v

実際に使ってみた感想

Cygwinは日本語も使えるコマンドライン環境で結構好きー。

rbenvを使うと、少々重い感じでした。Rubyをコンパイルした場合はサクサク動いてくれるので、私はコンパイルして使ってるよ。

27
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
24