Hiding Ruby 2.7 Deprecation Warnings in Rails 6
If you have upgraded your Rails app to Ruby 2.7, you are probably seeing a lot of deprecation messages in your console. You should first make sure that none of these messages are coming from your code, and address them if they are! If the deprecations are coming mostly from Rails, it may be time to disable the messages and save yourself from messy terminal output.
The TL;DR is that you need to use RUBYOPT='-W:no-deprecated -W:no-experimental'
to disable the deprecations. This will also disable experimental feature warnings as well.
Here are some options you have to make that happen. But first, lets create a new Rails app to experiment with!
Generate a new Rails app with Ruby 2.7
You can either use the CLI or this template.
If you use the CLI, I recommend something like:
rails new silence_ruby_2_7_deprecations -d postgresql --webpack=stimulus
Method #1: Using dotenv-rails
If you are using the dotenv-rails gem, or another method of using .env
files, simply add the following to your .env
file:
export RUBYOPT='-W:no-deprecated -W:no-experimental'
Then run the following in the root of the project:
source .env
You should no longer be seeing the Ruby 2.7 deprecation warnings coming out of Rails! π
Method #2: Prefixing commands
Another option you have for ignoring the Ruby 2.7 deprecation warnings is to prefix all of your Rails commands with RUBYOPT='-W:no-deprecated -W:no-experimental'
.
Example:
-
rails server
would becomeRUBYOPT='-W:no-deprecated -W:no-experimental' rails server
-
rails console
would becomeRUBYOPT='-W:no-deprecated -W:no-experimental' rails console
- etc.
This is obviously not ideal but it will work!
Method #3: Updating your environment
If you want to disable these deprecation messages everywhere, you can add the following to your ~/.zshrc
or ~/.bashrc
:
export RUBYOPT='-W:no-deprecated -W:no-experimental'
This will disable deprecation and experimental feature warnings for all versions of Ruby, for all projects. I have heard of this creating issues for some so you may want to be careful using this method if you work on multiple apps that aren't on Ruby 2.7.
Hopefully this helps! Happy coding!
Top comments (8)
Hi Andrew, i've cloned your repository and still got warnings. What I'm doing wrong?
You can also choose to have depreciation warnings sent to the log file rather than printed out to the console. For example, in the test environment, add the following to
config/environments/test.rb
The string
-W:no-deprecated -W:no-experimental
didn't work for me, but-W0
did, which I found at stackoverflow.com/a/59594760/1049688On method #1 (using dotenv-rails), the line to add to
.env
should read:as the
dotenv-rails
gem will take care to export it.So this is just to silence them within ruby? Wonβt rails internals eventually knock out these warnings?
They will but this will help until that update lands!
None of these worked for me.
Did you reload your zshrc/bashrc or reload your terminal?