0

When I run bundle install, I get no errors. But when I try to run puma, I get the error:

Puma starting in single mode...
* Puma version: 5.6.5 (ruby 3.0.2-p107) ("Birdie's Version")
*  Min threads: 5
*  Max threads: 5
*  Environment: development
*          PID: 95251
Your Ruby version is 3.0.2, but your Gemfile specified 3.1.2
! Unable to load application: SystemExit: exit

but when I run rbenv local, I get: 3.1.2

and ruby -v gives: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]

and my Gemfile has ruby "3.1.2"

Why does Puma still think it should be 3.0.2? I should note that I'm running puma with sudo... because I want to have the ability to run https on port 443 at 0.0.0.0 - when I run rails server all by itself, everything works fine and Puma shows 3.1.2 as expected.

I've looked through other similar questions and I haven't found anything that works.

  • aha! Running sudo ruby -v gives me 3.0.2, and sudo which ruby shows /usr/bin/ruby - that's why it expects 3.0.2. How do I upgrade the default ruby under sudo? – Jacob Jackson Mar 11 '23 at 23:50
  • 1
    Why did you install Ruby with sudo in the first place. That is usually not recommended. Instead, [install `3.1.2` with rbenv](https://stackoverflow.com/a/36485498/2483313) and use that version. – spickermann Mar 12 '23 at 06:10
  • Always start Rails using `bundle exec rails server` to use the *bundled* version of Rails. – anothermh Mar 13 '23 at 17:37

2 Answers2

1

In the development and deployment setups I've seen, the Ruby server is not run on port 443 directly. As far as I'm aware, doing that is quite unusual. Instead, a higher port is used, which doesn't require root access. In deployment, port 443 would be served by a proxy in front of the Ruby process, like Nginx or Envoy.

I'm struggling to reproduce your issue. The key difference I can see between our setups is that for you, sudo which ruby shows /usr/bin/ruby (system Ruby), whereas I get /home/brendan/.rbenv/shims/ruby (rbenv Ruby). This is probably due to the $HOME/.rbenv/shims directory not being part of the PATH environment variable within your sudo environment. Perhaps the applicable rbenv init command is not running there? Check you've followed these rbenv installation instructions.

You can more readably view the entries within your sudo environment's $PATH with:

sudo env | grep PATH | sed s/^PATH=// | tr : '\n' | sort

As said in the comments, when running an executable from a gem, always prefix it with bundle exec to ensure you're using the version of it and its dependencies that are in your Gemfile.lock. And to avoid headaches when using a Ruby version manager (like rbenv), avoid using the Ruby that's installed by your system package manager.

If you feel you must use port 443 and hence sudo, I'm sure there's a way to get that to work; but I'd recommend side-stepping the issue entirely by using a different port number. If there is something requiring you to use 443, perhaps you should be looking for a way to address that issue instead.

ZimbiX
  • 485
  • 3
  • 7
0

Firstly make sure you have installed ruby 3.1.2 (you can install using rbenv or rvm)

Then make it default

Store 3.1.2 in .ruby-version file under your project directory

Clean the Gemfile.lock

Bundle install

Crazy Cat
  • 186
  • 3
  • 13
  • I'd reckon there must already be a `.ruby-version`. Without one, `rbenv` instead outputs `rbenv: no local version configured for this directory` rather than what was provided in the question – ZimbiX Mar 17 '23 at 17:37