---
# System prepended metadata

title: '[Rails4]安裝rails 4.2.8'
tags: [Rails4]

---

# [Rails4]安裝rails 4.2.8　

###### tags: `Rails4`

https://progressbar.tw/notes/RubyOnRails/24

### 安裝curl
```c!
sudo apt-get install curl libcurl4-openssl-dev
```
### 安裝nodejs
https://stackoverflow.com/questions/7092107/rails-could-not-find-a-javascript-runtime
```c!
sudo apt-get install nodejs
```

### 安裝rvm
https://rvm.io/
```c!
$ sudo apt install gnupg2
$ sudo apt install curl

$ \curl -sSL https://get.rvm.io | bash -s stable --rails
```
安裝完後重開terminal,
輸入以下指令:
```c!
$ rvm -v
```
若有看到版本號表示rvm安裝成功





>ubuntu需先按照以下連結對terminal做設定:
https://rvm.io/integration/gnome-terminal


### 切換ruby版本

切換ruby版本至2.2.6:
```c!
$ rvm --default use 2.2.6
```

若提示未安裝,則需先安裝該版本的ruby:

```c!
$ rvm install "ruby-2.2.6"
```


<br>


### 安裝rails 4.2.8

>若已有安裝新版rails,
>請先用以下指令移除:

```c!
$ gem uninstall rails
```


https://github.com/rubygems/rubygems/issues/2534

在上述網址有提到以下內容:

```
RubyGems 3.0.0 is only installable on Ruby >= 2.3

For 2.2 & 2.2.10, try

  - gem install "rubygems-update:<3.0.0" --no-document
  - update_rubygems

At present, RubyGems does not correctly handle Ruby version constraints.
```


所以就照做:
```terminal
$ gem install "rubygems-update:<3.0.0" --no-document
```

```terminal
$ update_rubygems
```

(2020/01/01更新)
https://stackoverflow.com/questions/18290080/change-rails-version-used-by-rvm/18290486
根據此連結教學,可以安裝不同版本的rails並切換

https://sites.google.com/site/zhoubx/computer/ubuntu-16-04-2-apache-2-4-18-passenger-ruby-2-4-1-rails-5-1-1-postgresql
上述連結提到可加上--default參數

rails與ruby版本對照表:
https://www.fastruby.io/blog/ruby/rails/versions/compatibility-table.html

現在假設要在ruby2.2.6的環境下安裝rails4.2.8:


### 新增@rails4變數方便以後切換
```c!
$ rvm use 2.2.6
$ rvm use @rails4 --create 
```

### 安裝舊版nokogiri(1.9.1)

```c!
gem install nokogiri -v 1.9.1
```

### 安裝舊版sprockets(3.7.2)
```c!
gem install sprockets -v 3.7.2
```

### 開始安裝rails 4.2.8
```c!
gem install rails -v 4.2.8 --no-ri --no-rdoc
```
>若單純使用gem install rails -v 4.2.8,
>則可能會卡在ri的安裝


2020/3/6更新:使用ubuntu 18.04
https://stackoverflow.com/questions/57475842/what-to-use-instead-of-no-ri-for-gem-install
```c!
gem install rails -v 4.2.8 -N
```


### 切換至@rails4
```c!
$ rvm use 2.2.6@rails4 --default
$ ruby -v #-->ruby 2.2.6p396 (2016-11-15 revision 56800) [x86_64-linux]
$ rails -v #-->Rails 4.2.8
```

### 安裝postgresql & pgadmin4
https://blog.gtwang.org/linux/how-to-install-and-use-postgresql-ubuntu-18-04/


#### problem shooting
##### pg gem無法安裝
https://stackoverflow.com/questions/3116015/how-to-install-postgresqls-pg-gem-on-ubuntu
```c!
$ sudo apt-get install libpq-dev
```
##### 其他
[[Postgresql]解決Fatal: role “username” does not exist 錯誤](https://hackmd.io/p24HhyiqR--MvsFXguX2Bw)
[[Rails4][err][psql]fe_sendauth: no password supplied](https://hackmd.io/Gw6E-KeeSOWTNI1SJxgasQ)

### 新增專案

https://stackoverflow.com/questions/48911869/add-gem-pg-to-your-gemfile-and-ensure-its-version-is-at-the-minimum-required

```c!
rails new app-name -d postgresql
```
>-d參數用於指定要使用的資料庫

