在Ubuntu 15.04中安装Ruby on Rails
本篇我们会学习如何用rbenv在Ubuntu 15.04中安装Ruby on Rails。我们选择Ubuntu作为操作系统是因为Ubuntu是Linux发行版中自带很多包和完整文档的操作系统,因此我认为这是正确的选择。如果你还没有安装最新的Ubuntu,你可以从下载iso文件开始。
安装 Ruby
我们要做的第一件事是更新Ubuntu包并且为Ruby安装一些依赖。
<span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> update</span>
<span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install git</span><span class="pun">-</span><span class="pln">core curl zlib1g</span><span class="pun">-</span><span class="pln">dev build</span><span class="pun">-</span><span class="pln">essential libssl</span><span class="pun">-</span><span class="pln">dev libreadline</span><span class="pun">-</span><span class="pln">dev libyaml</span><span class="pun">-</span><span class="pln">dev libsqlite3</span><span class="pun">-</span><span class="pln">dev sqlite3 libxml2</span><span class="pun">-</span><span class="pln">dev libxslt1</span><span class="pun">-</span><span class="pln">dev libcurl4</span><span class="pun">-</span><span class="pln">openssl</span><span class="pun">-</span><span class="pln">dev Python</span><span class="pun">-</span><span class="pln">software</span><span class="pun">-</span><span class="pln">properties libffi</span><span class="pun">-</span><span class="pln">dev</span>
有三种方法来安装Ruby:rbenv、rvm和从源码安装。每种都有各自的好处,但是近来开发者们更倾向使用rbenv而不是rvm和源码来安装。我们将安装最新的Ruby版本,2.2.2。
用rbenv来安装只有简单的两步。第一步安装rbenv,接着是ruby-build:
<span class="pln">cd</span>
<span class="pln">git clone git</span><span class="pun">:</span><span class="com">//github.com/sstephenson/rbenv.git .rbenv</span>
<span class="pln">echo </span><span class="str">'eval "$(rbenv init -)"'</span><span class="pun">>></span><span class="pun">~/.</span><span class="pln">bashrc</span>
<span class="kwd">exec</span><span class="pln"> $SHELL</span>
<span class="pln">git clone git</span><span class="pun">:</span><span class="com">//github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build</span>
<span class="pln">echo </span><span class="str">'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"'</span><span class="pun">>></span><span class="pun">~/.</span><span class="pln">bashrc</span>
<span class="kwd">exec</span><span class="pln"> $SHELL</span>
<span class="pln">git clone https</span><span class="pun">:</span><span class="com">//github.com/sstephenson/rbenv-gem-rehash.git ~/.rbenv/plugins/rbenv-gem-rehash</span>
<span class="pln">rbenv install </span><span class="lit">2.2</span><span class="pun">.</span><span class="lit">2</span>
<span class="pln">rbenv </span><span class="kwd">global</span><span class="lit">2.2</span><span class="pun">.</span><span class="lit">2</span>
<span class="pln">ruby </span><span class="pun">-</span><span class="pln">v</span>
我们需要安装Bundler,但是我们要在安装之前告诉rubygems不要为每个包安装本地文档。
<span class="pln">echo </span><span class="str">"gem: --no-ri --no-rdoc"</span><span class="pun">></span><span class="pun">~/.</span><span class="pln">gemrc</span>
<span class="pln">gem install bundler</span>
配置 GIT
配置git之前,你要创建一个github账号,你可以注册一个github 账号。我们需要git作为版本控制系统,因此我们要设置它来匹配github账号。
用户的github账号来替换下面的Name 和 Email address 。
<span class="pln">git config </span><span class="pun">--</span><span class="kwd">global</span><span class="pln"> color</span><span class="pun">.</span><span class="pln">ui </span><span class="kwd">true</span>
<span class="pln">git config </span><span class="pun">--</span><span class="kwd">global</span><span class="pln"> user</span><span class="pun">.</span><span class="pln">name </span><span class="str">"YOUR NAME"</span>
<span class="pln">git config </span><span class="pun">--</span><span class="kwd">global</span><span class="pln"> user</span><span class="pun">.</span><span class="pln">email </span><span class="str">"[email protected]"</span>
<span class="pln">ssh</span><span class="pun">-</span><span class="pln">keygen </span><span class="pun">-</span><span class="pln">t rsa </span><span class="pun">-</span><span class="pln">C </span><span class="str">"[email protected]"</span>
接下来用新生成的ssh key添加到github账号中。这样你需要复制下面命令的输出并粘贴在Github的设置页面里面。
<span class="pln">cat </span><span class="pun">~</span><span class="str">/.ssh/</span><span class="pln">id_rsa</span><span class="pun">.</span><span class="pln">pub</span>
如果你做完了,检查是否已经成功。
<span class="pln">ssh </span><span class="pun">-</span><span class="pln">T git@github</span><span class="pun">.</span><span class="pln">com</span>
你应该得到下面这样的信息。
<span class="typ">Hi</span><span class="pln"> excid3</span><span class="pun">!</span><span class="typ">You</span><span class="str">'ve successfully authenticated, but GitHub does not provide shell access.</span>
安装 Rails
我们需要安装像NodeJS这样的javascript运行时环境,因为近来Rails的依赖越来越多了。这样我们可以合并和压缩你的javascript,从而提供一个更快的生产环境。
我们需要添加PPA来安装nodeJS。
<span class="pln">sudo add</span><span class="pun">-</span><span class="pln">apt</span><span class="pun">-</span><span class="pln">repository ppa</span><span class="pun">:</span><span class="pln">chris</span><span class="pun">-</span><span class="pln">lea</span><span class="pun">/</span><span class="pln">node</span><span class="pun">.</span><span class="pln">js</span>
<span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> update</span>
<span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install nodejs</span>
如果在更新时遇到了问题,你可以试试这个命令:
<span class="com"># Note the new setup script name for Node.js v0.12</span>
<span class="pln">curl </span><span class="pun">-</span><span class="pln">sL https</span><span class="pun">:</span><span class="com">//deb.nodesource.com/setup_0.12 | sudo bash -</span>
<span class="com"># Then install with:</span>
<span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install </span><span class="pun">-</span><span class="pln">y nodejs</span>
下一步,用这个命令安装 rails:
<span class="pln">gem install rails </span><span class="pun">-</span><span class="pln">v </span><span class="lit">4.2</span><span class="pun">.</span><span class="lit">1</span>
因为我们正在使用rbenv,用下面的命令来让rails的执行程序可以使用。
<span class="pln">rbenv rehash</span>
要确保rails已经正确安装,你可以运行rails -v,显示如下:
<span class="pln">rails </span><span class="pun">-</span><span class="pln">v</span>
<span class="com"># Rails 4.2.1</span>
如果你得到的是不同的结果可能是环境没有设置正确。
设置 MySQL
或许你已经熟悉MySQL了,你可以从Ubuntu的仓库中安装MySQL的客户端与服务端。你可以在安装时设置root用户密码。这个信息将来会进入你rails程序的database.yml文件中。用下面的命令来安装mysql。
<span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install mysql</span><span class="pun">-</span><span class="pln">server mysql</span><span class="pun">-</span><span class="pln">client libmysqlclient</span><span class="pun">-</span><span class="pln">dev</span>
安装libmysqlclient-dev用于mysql2 gem的编译;在设置rails程序时,rails通过它来连接mysql。
最后一步
让我们尝试创建你的第一个rails程序:
<span class="com"># 使用 MySQL 数据库</span>
<span class="pln">rails </span><span class="kwd">new</span><span class="pln"> myapp </span><span class="pun">-</span><span class="pln">d mysql</span>
<span class="com"># 进入到应用目录</span>
<span class="pln">cd myapp</span>
<span class="com"># 创建数据库</span>
<span class="pln">rake db</span><span class="pun">:</span><span class="pln">create</span>
<span class="pln">rails server</span>
访问http://localhost:3000来访问你的新网站。现在你的电脑上已经可以构建rails程序了。
如果你在创建数据库时遇到了“Access denied for user 'root'@'localhost' (Using password: NO)”这个错误信息,你需要更新你的config/database.yml文件来匹配数据库的用户名和密码。
<span class="com"># 编辑配置文件夹中的 database.yml</span>
<span class="pln">nano config</span><span class="pun">/</span><span class="pln">database</span><span class="pun">.</span><span class="pln">yml</span>
接着填入MySql root用户的密码。
退出 (Ctrl+X)并保存。
总结
Rails是用Ruby写的, 也是随着rails一起使用的编程语言。在Ubuntu 15.04中Ruby on Rails可以用rbenv、 rvm和源码的方式来安装。本篇我们使用的是rbenv方式并用了MySQL作为数据库。有任何的问题或建议,请在评论栏指出。
Ruby on rails初体验系列文章:
via: http://linoxide.com/ubuntu-how-to/installing-ruby-rails-using-rbenv-ubuntu-15-04/