位元詩人 技術雜談:Install Ruby Gems in Home Directory

Facebook Twitter LinkedIn LINE Skype EverNote GMail Yahoo Email

Although some system package managers on Linux or Unix provides Ruby gems as package, you still want to gem to install updated or specific gems sometimes. One popular way is using RVM, a command-line tool which allow you to manage multiple Ruby environment. Nevertheless, if you do not need multiple Ruby versions in one system, you may also choose to install Ruby gems to local directories like home directory. By this way, gems files will not be mixed with other system files.

To install gems locally, set ~/.gemrc. Before that, type gem environment in terminal to get some information about gem environment. What you need is GEM PATHS.


$ gem environment
# other message omitted...
- GEM PATHS:
    - /Library/Ruby/Gems/2.0.0
    - /Users/user/.gem/ruby/2.0.0
    - /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0
{{< / highlight >}}

Edit *~/.gemrc*.  Set *gemhome* and *gempath* variable with your own values.  Personally, I prefer to store all library files in *~/lib*.  You may set to other path.  Remember to set with **absolute path** because *~/.gemrc* is not a shell script file.

```text
gemhome: /Users/user/lib/gems
gempath:
- /Users/user/lib/gems
- /Library/Ruby/Gems/2.0.0
- /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0
{{< / highlight >}}

Set **GEM_HOME**, **GEM_PATH** and **PATH** variable in *~/.profile* or *~/.bash_profile* to use executives.

```bash
export GEM_HOME=$HOME/lib/gems
export GEM_PATH=$HOME/lib/gems

if [ -d $HOME/lib/gems/bin ]; then
    PATH=$HOME/lib/gems/bin:$PATH
fi

export PATH
{{< / highlight >}}

Finally, you can install gems in home directory and enjoy Ruby programming.
關於作者

位元詩人 (ByteBard) 是資訊領域碩士,專注於從探索到產品的開發過程,並以工具驅動的方式改善專案。喜歡以開源專案作為成果,回饋社群。

主要方向包括:自用工具的打磨 (dogfooding)、編譯器前端在工具開發中的應用,以及將研究轉化為可維護的開源成果。

除了技術之外,也喜歡日本料理和黑咖啡,偶爾自助旅行,將生活中的靈感融入技術隨筆。