Finally Fixed: Installing Ruby on Ubuntu 24.04 WSL2
I’ve been pulling my hair out trying to install Ruby on Ubuntu 24.04 in WSL2 for the past few days. Every attempt with both rbenv and rvm kept failing with cryptic GCC internal compiler errors. I ended up diving into VSCode devcontainers to get work done—which turned out to be a valuable learning experience that I don’t regret at all. But having a working local Ruby setup with rbenv is still essential for those times when you don’t have an existing devcontainer configuration and need to jump into a project quickly, or when you hit Docker networking issues—like when your app needs to connect to services on the host machine or communicate with other containers.
The Problem
If you’re reading this, you’ve probably seen errors like this:
Error running '__rvm_make -j12',
please read /home/user/.rvm/log/xxxxx_ruby-x.x.x/make.log
There has been an error while running make. Halting the installation.
Or the dreaded:
gcc: internal compiler error: in try_forward_edges, at cfgcleanup.cc:580
I tried everything:
- Switching between
rbenvandrvm - Upgrading to GCC 14
- Reinstalling build dependencies
- Clearing caches
- Sacrificing rubber ducks to the coding gods
Nothing worked. Ruby 3.x installations kept failing, and I was stuck using devcontainers for every project.
The Solution That Actually Worked
After a few failed attempts (I didn’t have time to debug this extensively given my CTO responsibilities), I found the fix. It turns out Ubuntu 24.04’s GCC (both 13.3.0 and 14.x) has optimization bugs that cause internal compiler errors when building Ruby.
The solution? Disable compiler optimizations during the build process.
Add these exports before installing Ruby:
export CFLAGS="-O0"
export CPPFLAGS="-O0"
export CXXFLAGS="-O0"
I first tested this with rvm and it worked. Once I confirmed the compilation succeeded with RVM, I switched back to rbenv—my preferred Ruby version manager that I’ve been using for years. I uninstalled the RVM setup and reinstalled all three Ruby versions (2.7.8, 3.4.1, and 4.0.0) using rbenv with the same compiler flags:
rbenv install 2.7.8 # Works!
rbenv install 3.4.1 # Works!
rbenv install 4.0.0 # Works!
Why This Works
The -O0 flag tells GCC to compile without any optimizations. While this means your Ruby build will be slightly slower (during compilation only, not runtime performance of your apps), it completely bypasses the buggy optimization passes in GCC that were causing the crashes.
The trade-off is worth it - you get a working Ruby installation without having to:
- Wait for GCC patches
- Downgrade your entire system
- Switch to a different Linux distribution
- Rely solely on devcontainers (though they’re great for other use cases!)
Long-term Impact
Here’s the good news: this only affects compilation time, not your application’s runtime performance. Once Ruby is compiled and installed, it runs at normal speed. The optimization level only matters during the build process itself.
If you’re concerned about performance, you can always recompile Ruby later when GCC gets fixed in a future update. But honestly, I haven’t noticed any difference in day-to-day development work.
Full Installation Commands
Here’s the complete process I use now with rbenv:
# Set compiler flags
export CFLAGS="-O0"
export CPPFLAGS="-O0"
export CXXFLAGS="-O0"
# Install Ruby (pick your version)
rbenv install 2.7.8
rbenv install 3.4.1
rbenv install 4.0.0
# Set your global or local version
rbenv global 3.4.1
# or
rbenv local 2.7.8
You can add these exports to your ~/.bashrc or ~/.zshrc if you plan to install multiple Ruby versions:
# Add to ~/.bashrc or ~/.zshrc
export CFLAGS="-O0"
export CPPFLAGS="-O0"
export CXXFLAGS="-O0"
Closing Thoughts
Learning about devcontainers was actually a silver lining from this issue—they’re an excellent tool for maintaining consistent development environments across teams. But having a reliable local Ruby setup is still valuable for quick prototyping, gem development, and projects that don’t need the full container overhead.
It’s frustrating that Ubuntu 24.04 ships with a buggy GCC, but at least there’s a workaround. Hopefully, this saves someone else the trial and error I went through.
Now if you’ll excuse me, I have a backlog of projects to work on with my freshly installed Ruby versions.
Update: This fix works with both rvm and rbenv. The key is setting those compiler flags before running any Ruby installation command, regardless of which version manager you use.
Share on
X Facebook LinkedIn BlueskyComments are configured with provider: utterances, but are disabled in non-production environments.
