安装Ruby

由于Ubuntu的apt包管理器的ruby版本过旧,故考虑从源码编译安装。这里以安装ruby2.3.0为例:

1
2
3
4
5
6
7
8
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

安装完成后,我们可以查看其版本:

1
2
$ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [i686-linux]

安装sqlite3

1
sudo apt-get install sqlite3 libsqlite3-dev

安装Rails

由于GFW的缘故,ruby默认的gem源rubygems.org无法访问,故需要先换源。

查看当前的gem源:

1
2
3
4
$ gem sources -l
*** CURRENT SOURCES ***

https://rubygems.org/

换源:

1
2
3
4
5
6
7
8
$ 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:

1
sudo gem install rails

安装完成后查看其版本:

1
2
$ rails --version
Rails 4.2.5.2

修改Gemfile

编辑Gemfile:

1
sudo vi /usr/local/lib/ruby/gems/2.3.0/gems/railties-4.2.5.2/lib/rails/generators/rails/app/templates/Gemfile

将第一行

1
source 'https://rubygems.org'

改为:

1
source 'https://ruby.taobao.org/'

安装JavaScript 运行时

这里选择安装NodeJS:

1
sudo apt-get install nodejs

建立Rails工程

1
rails new demo

进入文件夹,启动服务器:

1
2
cd demo/
rails server