# 如何解决 Gem 安装 Rails 无法执行的问题

- Link: https://www.ixiqin.com/2023/02/15/how-to-solve-the-problem-of-gem-install-rails-cannot-perform/
- Published: 2023-02-15T06:00:00+08:00
- Author: 白宦成

文章目录

 [Toggle](https://www.ixiqin.com/2023/02/15/how-to-solve-the-problem-of-gem-install-rails-cannot-perform/#)

 * [如何解决 Gem 安装 Rails 无法执行的问题？](https://www.ixiqin.com/2023/02/15/how-to-solve-the-problem-of-gem-install-rails-cannot-perform/#%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3_gem_%e5%ae%89%e8%a3%85_rails_%e6%97%a0%e6%b3%95%e6%89%a7%e8%a1%8c%e7%9a%84%e9%97%ae%e9%a2%98%ef%bc%9f)
    - [解决方案](https://www.ixiqin.com/2023/02/15/how-to-solve-the-problem-of-gem-install-rails-cannot-perform/#%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88)
       * [1. 获取具体的 Path](https://www.ixiqin.com/2023/02/15/how-to-solve-the-problem-of-gem-install-rails-cannot-perform/#1_%e8%8e%b7%e5%8f%96%e5%85%b7%e4%bd%93%e7%9a%84_path)
       * [2. 找到 Bin 目录](https://www.ixiqin.com/2023/02/15/how-to-solve-the-problem-of-gem-install-rails-cannot-perform/#2_%e6%89%be%e5%88%b0_bin_%e7%9b%ae%e5%bd%95)
       * [3. 修改 PATH 环境变量](https://www.ixiqin.com/2023/02/15/how-to-solve-the-problem-of-gem-install-rails-cannot-perform/#3_%e4%bf%ae%e6%94%b9_path_%e7%8e%af%e5%a2%83%e5%8f%98%e9%87%8f)
       * [4. 验证配置](https://www.ixiqin.com/2023/02/15/how-to-solve-the-problem-of-gem-install-rails-cannot-perform/#4_%e9%aa%8c%e8%af%81%e9%85%8d%e7%bd%ae)

# 如何解决 Gem 安装 Rails 无法执行的问题？

由于我并不在大型生产环境使用 Ruby on Rails，都是在一些自己的 Side Project 上使用
Ruby On Rails，所以一直以来，我都是使用 Homebrew 来安装最新版本的 Ruby & Gem。

最近升级了 M1 以后，重新在配置 Ruby ，突然发现之前的配置失效了。在配置 Rails 时，
发现报了个错： `Rails is not currently installed on this system. To get the latest
version, simply type:`。

![截图](https://postimg.aliavv.com/m1/8w5c6g.png)

截图

但我可以肯定的是，我已经执行过了 `gem install rails`，所以出问题的不可能是我没安装，
唯一的可能便是**我安装的 Rails 没有安装到 PATH 当中**。

![](https://postimg.aliavv.com/m1/870yd1.png)

所以想要解决这个问题，只需要将我安装 gem 的路径添加到 path 中即可。

## 解决方案

### 1. 获取具体的 Path

执行 `gem info rails` 来获取到我的 rails 安装的路径，这里可以看到，被安装在了 `/
opt/homebrew/lib/ruby/gems/3.2.0` 路径。

![](https://postimg.aliavv.com/m1/04qwth.png)

### 2. 找到 Bin 目录

使用 `cd /opt/homebrew/lib/ruby/gems/3.2.0` 进入到 gem 目录。你可以看到这里有个 
bin 目录，bin 目录就是我们具体要用的可执行文件。

![](https://postimg.aliavv.com/m1/re9kwr.png)

你可以执行 `./rails -v` 来确认版本正确。

![](https://postimg.aliavv.com/m1/rbj0dk.png)

### 3. 修改 PATH 环境变量

接下来就是修改你的 PATH 环境变量了。在你用的 Shell 的配置文件当中，加入相应的 path
配置，并重启终端，即可完成配置。

![](https://postimg.aliavv.com/m1/xrile5.png)

### 4. 验证配置

重启终端后，随便找个目录，执行 `rails -v`，查看其结果，来验证我们的配置已经生效。
如果你可以看到类似下面的结果，则说明你的配置已经生效了～

![](https://postimg.aliavv.com/m1/8c0vdo.png)
