Travis CI trusty环境安装Linuxbrew失败,求解决方案
I ran into the exact same issue a while back—Travis CI's trusty environment reports a modern Ruby version, but Linuxbrew ends up using the ancient system Ruby (1.9.3) during installation, causing that frozen string error and the "must be run under Ruby 2.3+" failure. The root cause is sudo resetting your PATH (when installing to the default /home/linuxbrew/.linuxbrew location) which makes brew call the system Ruby instead of Travis's managed 2.4.1.
Here's the fix that worked consistently for me:
Solution: Install Linuxbrew to your user directory (no sudo required)
By installing Linuxbrew to ~/.linuxbrew instead of the system-wide location, you avoid needing sudo entirely. This keeps your Travis-managed Ruby version in the PATH during installation, which satisfies brew's Ruby requirement.
Update your .travis.yml with these steps in before_install:
language: cpp sudo: required dist: trusty before_install: # Install Linuxbrew to user directory (avoids sudo PATH reset) - sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)" "" --force-home # Add Linuxbrew's bin directory to your PATH - export PATH="$HOME/.linuxbrew/bin:$PATH" # Verify Ruby and brew versions to confirm everything's set up correctly - ruby -v - brew --version # Run brew update (no sudo needed now!) - brew update --force
What each step does:
- The
--force-homeflag tells the Linuxbrew installer to use~/.linuxbrewas the installation directory, so you don't need sudo permissions for setup. - Adding
$HOME/.linuxbrew/binto your PATH ensures you're using the Linuxbrew commands you just installed, not any system equivalents. - The
ruby -vandbrew --versionchecks are optional but helpful for debugging if something goes wrong—they should show Ruby 2.4.1+ and a valid brew version.
Alternative: Keep system-wide installation with sudo -E
If you need to install Linuxbrew to the default /home/linuxbrew/.linuxbrew location, you can preserve your environment variables (including the correct Ruby PATH) when running brew update with sudo -E:
language: cpp sudo: required dist: trusty before_install: - sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)" - export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH" # Use sudo -E to retain environment variables (including Travis's Ruby) - sudo -E brew update --force
This works because -E tells sudo to keep your current user's environment variables, so brew will use Travis's Ruby 2.4.1 instead of the system's 1.9.3.
Why this broke suddenly
Linuxbrew raised its minimum Ruby requirement to 2.3.x in a recent update, which is why your older setup worked last year but fails now. The system Ruby on trusty (1.9.3) is way below this threshold, so we just need to make sure brew uses the newer Ruby Travis provides.
内容的提问来源于stack exchange,提问作者Andrew Trotman

