主页

bundle exec rails console 报错:cannot load such file -- readline (loaderror)

2024-12-05 08:48PM

在项目里面运行bundle exec rails console 报错说:

$ bundle exec rails console
Traceback (most recent call last):
        10: from bin/rails:4:in `<main>'
         9: from bin/rails:4:in `require'
         8: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-4.2.10/lib/rails/commands.rb:17:in `<top (required)>'
         7: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
         6: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:58:in `console'
         5: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:123:in `require_command!'
         4: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-4.2.10/lib/rails/commands/commands_tasks.rb:123:in `require'
         3: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-4.2.10/lib/rails/commands/console.rb:3:in `<top (required)>'
         2: from /root/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/railties-4.2.10/lib/rails/commands/console.rb:3:in `require'
         1: from /root/.rbenv/versions/2.5.0/lib/ruby/2.5.0/irb/completion.rb:10:in `<top (required)>'
/root/.rbenv/versions/2.5.0/lib/ruby/2.5.0/irb/completion.rb:10:in `require': cannot load such file -- readline (LoadError)

解决方法:

安装readline 开发库,可以使用下面的这个命令

$ sudo apt-get install libreadline-dev

 如果使用了这个命令之后,还是报错,需要进行下面的操作

1.重新安装ruby(更倾向使用asdf,但是我这里已经使用了rbenv,所以就不修改了,直接继续使用rbenv)

1.1删除ruby 2.5.0

$ rbenv uninstall 2.5.0
rbenv: remove /root/.rbenv/versions/2.5.0? [yN] y

1.2 安装ruby 2.5.0

# rbenv install 2.5.0
==> Downloading openssl-1.1.1w.tar.gz...
-> curl -q -fL -o openssl-1.1.1w.tar.gz https://dqw8nmjcqpjn7.cloudfront.net/cf3098950cb4d853ad95c0841f1f9c6d3dc102dccfcacd521d93925208b76ac8
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 9661k  100 9661k    0     0  2906k      0  0:00:03  0:00:03 --:--:-- 2905k
==> Installing openssl-1.1.1w...
-> ./config "--prefix=$HOME/.rbenv/versions/2.5.0/openssl" "--openssldir=$HOME/.rbenv/versions/2.5.0/openssl/ssl" --libdir=lib zlib-dynamic no-ssl3 shared
-> make -j 12
-> make install_sw install_ssldirs
==> Installed openssl-1.1.1w to /root/.rbenv/versions/2.5.0
==> Downloading ruby-2.5.0.tar.bz2...
-> curl -q -fL -o ruby-2.5.0.tar.bz2 https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.0.tar.bz2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 13.3M  100 13.3M    0     0  2641k      0  0:00:05  0:00:05 --:--:-- 3073k
==> Installing ruby-2.5.0...

WARNING: ruby-2.5.0 is past its end of life and is now unsupported.
It no longer receives bug fixes or critical security updates.

-> ./configure "--prefix=$HOME/.rbenv/versions/2.5.0" "--with-openssl-dir=$HOME/.rbenv/versions/2.5.0/openssl" --enable-shared --with-ext=openssl,psych,+
-> make -j 12
-> make install
==> Installed ruby-2.5.0 to /root/.rbenv/versions/2.5.0

注意:这个步骤需要使用代理。

3. 将ruby 2.5.0设置为全局默认版本:

$ rbenv global 2.5.0

4. 查看版本

# ruby -v
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]

5. 运行bundle install,如果提示说:

# bundle install
rbenv: bundle: command not found

The `bundle' command exists in these Ruby versions:
  2.6.1
  2.6.9
  2.7.6
  3.1.0 

需要安装bundler,我这里安装的版本是 1.17.3

n# gem install bundler -v 1.17.3
Fetching: bundler-1.17.3.gem (100%)
/root/.rbenv/rbenv.d/exec/gem-rehash/rubygems_plugin.rb:6: warning: Insecure world writable dir /mnt/c in PATH, mode 040777
Successfully installed bundler-1.17.3
Parsing documentation for bundler-1.17.3
Installing ri documentation for bundler-1.17.3
Done installing documentation for bundler after 8 seconds
1 gem installed 

6. 再运行bundle install

7. 运行bundle exec rails console

# bundle exec rails console
irb(main):001:0>

这样就可以啦。 

返回>>

登录

请登录后再发表评论。

评论列表:

目前还没有人发表评论