Ubuntu搭建Ruby on Rails环境
安装Ruby
由于Ubuntu的apt包管理器的ruby版本过旧,故考虑从源码编译安装。这里以安装ruby2.3.0为例:
sudo apt-get install build-essential zlib1g-dev libssl-dev libreadline6-dev libyaml-dev cd /tmp wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.0.tar.gz tar -xvzf ruby-2.3.0.tar.gz cd ruby-2.3.0/ ./configure make sudo make install
安装完成后,我们可以查看其版本:
$ ruby -v ruby 2.3.0p0 (2015-12-25 revision 53290) [i686-linux]
安装sqlite3
sudo apt-get install sqlite3 libsqlite3-dev
安装Rails
由于GFW的缘故,ruby默认的gem源rubygems.org无法访问,故需要先换源。
查看当前的gem源:
$ gem sources -l *** CURRENT SOURCES *** https://rubygems.org/
换源:
$ gem sources -r https://rubygems.org/ https://rubygems.org/ removed from sources $ gem sources -a https://ruby.taobao.org/ https://ruby.taobao.org/ added to sources $ gem sources -l *** CURRENT SOURCES *** https://ruby.taobao.org/
安装Rails:
sudo gem install rails
安装完成后查看其版本:
$ rails --version Rails 4.2.5.2
修改Gemfile
编辑Gemfile:
sudo vi /usr/local/lib/ruby/gems/2.3.0/gems/railties-4.2.5.2/lib/rails/generators/rails/app/templates/Gemfile
将第一行
source 'https://rubygems.org'
改为:
source 'https://ruby.taobao.org/'
安装JavaScript 运行时
这里选择安装NodeJS:
sudo apt-get install nodejs
建立Rails工程
rails new demo
进入文件夹,启动服务器:
cd demo/
rails server