Installing ruby with ruby-install causes error out on Mac M1 - ruby

When installing ruby 2.6.6 or 2.7.2 using ruby-install on mac M1, the following error occurs. Ruby 3.0.0 works fine however anything older will error out with readline and not allow ruby to install.
readline.c:1905:37: error: use of undeclared identifier 'username_completion_function'; did you mean 'rl_username_completion_function'?
rl_username_completion_function);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rl_username_completion_function
readline.c:79:42: note: expanded from macro 'rl_username_completion_function'
# define rl_username_completion_function username_completion_function
^
/opt/homebrew/opt/readline/include/readline/readline.h:485:14: note: 'rl_username_completion_function' declared here
extern char *rl_username_completion_function PARAMS((const char *, int));

I finally got the older versions of ruby installed, including 2.6.6, on the m1 chip macbook pro with the following steps:
First, I had to reinstall rbenv, ruby-build and readline with:
brew reinstall rbenv ruby-build readline
Second, using CONFIGURE_OPTS was breaking my OpenSSL build. Use RUBY_CONFIGURE_OPTS instead. I was using hombrew and had to use the following flags:
RUBY_CONFIGURE_OPTS="--with-openssl-dir=`brew --prefix openssl` --with-readline-dir=`brew --prefix readline`"
Third, set the following to allow warnings in the make commands to not halt the build:
RUBY_CFLAGS="-Wno-error=implicit-function-declaration"
Fourth, ensure you set the arch flag when installing via rbenv:
arch -x86_84
Fifth, ensure your homebrew paths are correctly set:
export PATH="/opt/homebrew/bin:$PATH"
export PATH="/opt/homebrew/opt:$PATH"
The final command that successfully installed ruby 2.6.6 was:
export PATH="/opt/homebrew/bin:$PATH"
export PATH="/opt/homebrew/opt:$PATH"
RUBY_CFLAGS="-Wno-error=implicit-function-declaration" RUBY_CONFIGURE_OPTS="--with-openssl-dir=`brew --prefix openssl` --with-readline-dir=`brew --prefix readline`" sudo arch -x86_64 rbenv install --verbose 2.6.6
I used sudo to give mkdir permissions to the scripts.

This worked for me:
❯ arch -x86_64 rvm install 3.1.2 --with-openssl-dir=/usr/local/opt/openssl#3
So I had to specify the architecture (arch -x86_64) and a version of openssl under /usr/local/opt. In my case I already had a couple of versions of openssl installed there, I just picked the latest I had. In other cases you may have to download and compile it yourself

I have been able to install both as x86_64 code and 3.0.1 as arm64 code. I use rvm but this should work with other things.
I use iTerm2 and have made 2 copies. One I used Get Info to change one application to use Rosetta. Somewhere I even found a blue icon for the x86 app.
I have 2 versions of Homebrew. One in /opt/homebrew/bin/brew and the other in /usr/local/bin/brew.
I have 2 sets of exports in my .zshrc profile. I use the architecture to select the correct ones for the shell.
alias abrew="/opt/homebrew/bin/brew"
alias i="arch -x86_64"
alias ibrew="arch -x86_64 /usr/local/bin/brew"
alias irvm="arch -x86_64 rvm"
# Add RVM to PATH for scripting. Make sure this is the last PATH variable change.
export PATH="$PATH:$HOME/.rvm/bin"
_ARCH=$(arch)
PROMPT="$_ARCH $PROMPT"
# Requires iterm2
if [[ "$_ARCH" == "i386" ]]; then
echo -ne "\033]1337;SetColors=bg=000FC5\007"
#usr/local is X_86
export PATH="/usr/local/bin:$PATH"
export PATH="/usr/local/opt:$PATH"
fi
if [[ "$_ARCH" == "arm64" ]]; then
#usr/local is X_86
export PATH="/opt/homebrew/bin:$PATH"
export PATH="/opt/homebrew/opt:$PATH"
fi
With this I can compile 2.6.6 (and I presume 2.7.2) in the x86 shell and separately 3.0.1 in the arm64 shell.
My rvm list looks like:
ruby-2.4.6 [ x86_64 ]
ruby-2.4.9 [ x86_64 ]
* ruby-2.6.5 [ x86_64 ]
ruby-2.6.6 [ x86_64 ]
ruby-2.7.0 [ x86_64 ]
ruby-2.7.2 [ x86_64 ]
=> ruby-3.0.1 [ arm64 ]
PS I still have trouble sometimes getting rails to link correctly to mysql. ruby / rails / mysql seem to all have to be the same architecture. Still chasing that one down.

You can use Rbenv
RUBY_CFLAGS="-w" rbenv install 2.6.6

For M1 users, the architecture is arm64. With Rosetta enabled it defaults to x86_64 which causes compatibility issues. Add arch inline with rvm install command:
arch -arm64 rvm install "ruby-2.7.5"

Duplicate terminal and name it something I put Terminal Ros
Terminal Ros > Right Click > Get Info > Check Open using Rosetta
Open Terminal Ros > Run install "ruby-2.7.2"
And then run rvm use 2.7.2 --default

Related

Ruby don't want to change version or update ( MACBOOK PRO M1 )

I have a problem with ruby I installed it from homebrew ( Ruby version 3.2.0 )
When I use the command which -a ruby it returns me /usr/bin ==> System path and not the Homebrew path
So I changed the path in my .bash_profile and .bashrc
Nothing happened, so I installed rvm to change my version and have a better management but there is a big problem with rvm I can't disable errors and it don't install Ruby version.
I did those commands :
export warnflags=-Wno-error=implicit-function-declaration=
rvm install "ruby-3.2.0"
And still have the
Error running '__rvm_make -j8'
I tried with rbenv it doesn't allow to install new versions too
$ brew link --overwrite ruby
Warning: Refusing to link macOS provided/shadowed software: ruby
If you need to have ruby first in your PATH, run:
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> /Users/gwenauger/.bash_profile
For compilers to find ruby you may need to set:
export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"
For pkg-config to find ruby you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"
Ok so thanks to #Stefan I understood that
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> /Users/gwenauger/.bash_profile
Was a command and not a raw text to cut in your ~/bashrc
But I tried rbenv and rvm they're powerful tools but I can saw that they don't work well with M1 ARM chip the simple way is to use Homebrew even if it's not the best way to achieve it

Install older Ruby versions on a M1 MacBook?

Installing Ruby 3.0.x works fine on M1 MacBooks using rbenv or asdf. But older versions like 2.7.x and 2.6.x are having various issues. How do I fix them, without installing both x86 and ARM versions of homebrew at the same time?
In order to make installing of Ruby versions 2.6.x or 2.7.x successful on M1 MacBook using either rbenv or asdf (asdf is used in this example) follow these steps:
Upgrade to the latest version of rbenv or asdf-ruby plugin using your prefered installation method. In my case it's asdf-ruby installed over homebrew:
brew upgrade asdf
asdf plugin update ruby
Reinstall the current versions of openssl, readline and ruby-build in order to have the latest versions and configs:
brew uninstall --ignore-dependencies readline
brew uninstall --ignore-dependencies openssl
brew uninstall --ignore-dependencies ruby-build
rm -rf /opt/homebrew/etc/openssl#1.1
brew install -s readline
brew install -s openssl
brew install -s ruby-build
In your shell config .bashrc or .zshrc add the following ENV variables:
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl#1.1)"
export LDFLAGS="-L/opt/homebrew/opt/readline/lib:$LDFLAGS"
export CPPFLAGS="-I/opt/homebrew/opt/readline/include:$CPPFLAGS"
export PKG_CONFIG_PATH="/opt/homebrew/opt/readline/lib/pkgconfig:$PKG_CONFIG_PATH"
export optflags="-Wno-error=implicit-function-declaration"
export LDFLAGS="-L/opt/homebrew/opt/libffi/lib:$LDFLAGS"
export CPPFLAGS="-I/opt/homebrew/opt/libffi/include:$CPPFLAGS"
export PKG_CONFIG_PATH="/opt/homebrew/opt/libffi/lib/pkgconfig:$PKG_CONFIG_PATH"
This will ensure that the proper libraries and headers are used during the installations and it will ignore the implicit-function-declaration that is preventing some versions to continue installation. Note that for some other shells like fish the exporting of these variables will be a bit different.
Now start a new terminal session and you can try installing the older ruby versions:
asdf install ruby 2.7.2
asdf install ruby 2.6.5
Note that really old versions below 2.5 might still have issues. Most of the credits go to this Github issue.
UPDATE
For Ruby 2.2 please change the following variable:
export RUBY_CONFIGURE_OPTS=openssl#1.0
And do a
asdf reshim ruby
Thanks #xjlin0 for this update
This simple command helped me
RUBY_CFLAGS="-w" rbenv install 2.5.5
I have faced the same issue for Ruby 2.2.2 and many gems were dependent on that. So I have created a docker container for ubuntu 18.04 and then installed ruby on it. It's working.
For OSX v12.6.1 with frum on M1 Macbook Pro 2021 I used the following exports in ~/.bash_profile after installing ruby-build with brew to successfully build ruby 2.6.5
export optflags="-Wno-error=implicit-function-declaration"
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl#3)"
export LDFLAGS="-L/opt/homebrew/opt/readline/lib -L/opt/homebrew/opt/openssl#3/lib"
export CPPFLAGS="-I/opt/homebrew/opt/readline/include -I/opt/homebrew/opt/openssl#3/include"
export PKG_CONFIG_PATH="/opt/homebrew/opt/readline/lib/pkgconfig /opt/homebrew/opt/openssl#3/pkgconfig"
thanks to #orthodox
with rosetta I has brew under arm64. so I uninstall it, and reinstall with x86 and worked
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
echo 'eval "$(/usr/local/bin/brew shellenv)"' >> /Users/mberrueta/.zprofile
eval "$(/usr/local/bin/brew shellenv)"
arch -x86_64 brew uninstall --ignore-dependencies asdf
arch -x86_64 brew install asdf
arch -x86_64 brew upgrade asdf
asdf plugin add ruby https://github.com/asdf-vm/asdf-ruby.git
asdf plugin update ruby
arch -x86_64 brew uninstall --ignore-dependencies --force openssl
arch -x86_64 brew uninstall --ignore-dependencies --force ruby-build
rm -rf /opt/homebrew/etc/openssl#1.1
arch -x86_64 brew install -s readline
arch -x86_64 brew install -s openssl
arch -x86_64 brew install -s ruby-build
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl#1.1)"
export LDFLAGS="-L/opt/homebrew/opt/readline/lib:$LDFLAGS"
export CPPFLAGS="-I/opt/homebrew/opt/readline/include:$CPPFLAGS"
export PKG_CONFIG_PATH="/opt/homebrew/opt/readline/lib/pkgconfig:$PKG_CONFIG_PATH"
export optflags="-Wno-error=implicit-function-declaration"
export LDFLAGS="-L/opt/homebrew/opt/libffi/lib:$LDFLAGS"
export CPPFLAGS="-I/opt/homebrew/opt/libffi/include:$CPPFLAGS"
export PKG_CONFIG_PATH="/opt/homebrew/opt/libffi/lib/pkgconfig:$PKG_CONFIG_PATH"
asdf install ruby 2.6.9

gem install - fatal error: 'ruby/config.h' file not found in Mojave

gem install is failing in MacOs Mojave. Anything that can help me solve this? My ruby version is ruby 2.3.7p456.
➜ sudo gem install json -v '1.8.3'
current directory: /Library/Ruby/Gems/2.3.0/gems/json-1.8.3/ext/json/ext/generator
make "DESTDIR="
compiling generator.c
In file included from generator.c:1:
In file included from ./../fbuffer/fbuffer.h:5:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/include/ruby-2.3.0/ruby.h:33:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/include/ruby-2.3.0/ruby/ruby.h:24:10: fatal error: 'ruby/config.h' file not found
#include "ruby/config.h"
^~~~~~~~~~~~~~~
1 error generated.
make: *** [generator.o] Error 1
make failed, exit code 2
Update 15/03/2021
According to Apple, Ruby won't be included anymore in future versions of the system, so using a Ruby version manager will be the only way to go once this will actually happen.
Original answer
After hours and hours of trial and errors I wanted to go deeper into the reasons of this error and I finally found this that I think is the clean way to go around this issue:
Catalina (NdMe: I think this started earlier, on Mojave) no longer supports including macOS headers in command line
tools, they have to be explicitly used through Xcode SDK. You can do
so by running gem through the xcrun tool like:
xcrun gem install <your_gem>
Quoted from a Github issue comment.
Xcode 12 upgrade messed this up for me.
I installed ruby from brew and added this to my .zshrc
export PATH="/usr/local/opt/ruby/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"
export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"
If you have the Xcode 10 beta running this might fix it
sudo xcode-select -s /Applications/Xcode-beta.app/Contents/Developer
In macOS Monterey (in the Beta at least), Ruby (ruby 2.6.3p62 (2019-04-16 revision 67580)) is still included but it doesn't seem to work well (fatal error: 'ruby/config.h' file not found), so I installed my own version of Ruby. You can install ruby manually, but I prefer to use rbenv, a Ruby version manager.
Install ruby using rbenv
Install rbenv: brew install rbenv ruby-build
ruby-build is a plugin for rbenv to actually install a ruby version, it provides ruby install command.
Setup rbenv: rbenv init, (and follow its recommended instructions, for example if you're using zsh, it says add eval "$(rbenv init -)" to ~/.zshrc)
Install ruby: rbenv install 3.0.1. Latest version listed here
Do what you came to do: sudo gem install cocoapods or bundle install
Or install ruby manually:
Run brew install ruby
Run echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc so that this version is used
Restart terminal
asdf?
An alternative to rbenv is asdf, but I find asdf a bit complicated to use, since it supports more than just ruby, NodeJS. I would argue it does none of them very ergonomically.
Note: I have an M1 mac, and it's still working.
I got into this issue when trying to install fastlane with homebrew. None of the suggestions mentioned earlier helped me. Manually installing the macOS_SDK_headers_for_macOS_10.14.pkg fixed it.
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
I was having this same issue where I could not install native extensions due to a failure when including "ruby/config.h".
I believe the cause of this issue was because I was using the ruby version installed with Homebrew. After upgrading to a new version of Ruby with Homebrew, I saw the following helpful message from Homebrew:
ruby is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.
If you need to have ruby first in your PATH run:
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.bash_profile
For compilers to find ruby you may need to set:
export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"
For pkg-config to find ruby you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"
Adding the following lines to my ~/.bash_profile fixed the issue for me:
export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"
xcrun gem install <your_gem> worked for me, but I had to do it multiple times — every time bundle install failed.
The best solution is probably to set the same vars as xcrun in your .zshrc file. xcrun env on my system adds:
SDKROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CPATH=/usr/local/include
LIBRARY_PATH=/usr/local/lib
I had this problem after installing Big Sur. I found the file ruby/config.h was not present in the Xcode Packages in the Xcode versions I had installed prior to upgrading. I was able to correct it by uninstalling the Xcode versions and reinstalling them.

rbenv fails to install ruby 1.8.7 on MacOS X Sierra

After creating a .ruby-version file with 1.8.7, running rbenv install on the same directory where the file is gives the following error:
ERROR: This package must be compiled with GCC, but ruby-build couldn't
find a suitable gcc executable on your system. Please install GCC
and try again.
DETAILS: Apple no longer includes the official GCC compiler with Xcode
as of version 4.2. Instead, the gcc executable is a symlink to
llvm-gcc, a modified version of GCC which outputs LLVM bytecode.
For most programs the llvm-gcc compiler works fine. However,
versions of Ruby older than 1.9.3-p125 are incompatible with
llvm-gcc. To build older versions of Ruby you must have the official
GCC compiler installed on your system.
TO FIX THE PROBLEM: Install Homebrew's apple-gcc42 package with this
command: brew tap homebrew/dupes ; brew install apple-gcc42
You will need to install the official GCC compiler to build older
versions of Ruby even if you have installed Apple's Command Line Tools
for Xcode package. The Command Line Tools for Xcode package only
includes llvm-gcc.
BUILD FAILED (OS X 10.12.4 using ruby-build 20170405-2-g3b15693)
Then running brew install apple-gcc42 gives:
apple-gcc42: This formula either does not compile or function as expected on macOS
versions newer than Mavericks due to an upstream incompatibility.
Error: An unsatisfied requirement failed this build.
Stuck trying to install ruby 1.8.7 through rbenv on MacOS X Sierra. Any ideas on how to fix this?
Previous answer looks good, but some updates for it:
You must add the following code not after line 762 (because from version to version lines must be different).
You must insert it after:
require_gcc() {
local gcc="$(locate_gcc || true)"
these lines(for me it was 784 line)
So full tutorial:
Found the solution here: http://xibbar.hatenablog.com
http://xibbar.hatenablog.com/entry/2017/04/28/112813
After running it through Google Translate, got to this:
Find ruby-build: which ruby-build
Edit it (in my case): vim /usr/local/bin/ruby-build
Find the lines like these:
require_gcc() {
local gcc="$(locate_gcc || true)"
Add the following code after it:
local osx_version="$(osx_version)"
if [ $osx_version = "1012" ]; then
return 0
fi
Run:
CONFIGURE_OPTS="--with-readline-dir=/usr/local --with-openssl-dir=`brew --prefix openssl`" RUBY_CONFIGURE_OPTS="--with-openssl-dir=`brew --prefix openssl`" rbenv install 1.8.7-p374
Hope it's useful to others.
Found the solution here: http://xibbar.hatenablog.com
After running it through Google Translate, got to this:
Find ruby-build: which ruby-build
Edit it (in my case): vim /usr/local/bin/ruby-build
Add the following code after line 762:
local osx_version="$(osx_version)"
if [ $osx_version = "1012" ]; then
return 0
fi
Run:
CONFIGURE_OPTS="--with-readline-dir=/usr/local --with-openssl-dir=`brew --prefix openssl`" RUBY_CONFIGURE_OPTS="--with-openssl-dir=`brew --prefix openssl`" rbenv install 1.8.7-p374
Hope it's useful to others.

Upgrade to Ruby 2.0.0 - Libtool

I'm trying to upgrade from ruby 1.9.3 to 2.0.0.
I ran
rvm upgrade 1.9.3-p448 2.0.0-p247
The error I get is:
Error running 'requirements_smf_lib_install libtool',
please read /Users/melanie/.rvm/log/1381637445_ruby-2.0.0-p247/package_install_libtool.log
Failed installation package: libtool of: libtool, gdbm.
Requirements installation failed with status: 1.
Unable to install ruby ruby-2.0.0-p247. Please install it manually to continue.
My log says:
[2013-10-13 15:11:58] requirements_smf_lib_install
requirements_smf_lib_install ()
{
typeset -a __smf_install_command;
requirements_smf_lib_install_command "$#";
rvm_debug "__smf_install_command: ${__smf_install_command[*]};";
"${__smf_install_command[#]}" || {
typeset ret=$?;
rvm_warn "There were package installation errors with SM Framework, make sure to read the log.
If you see this on OSX, then you might want to try macports (http://www.macports.org/) or homebrew (http://mxcl.github.io/homebrew/) and tell RVM to use them by running:
rvm autolibs macports #OR
rvm autolibs homebrew
You can get help using IRC: http://webchat.freenode.net/?channels=rvm";
return $ret
};
shift
}
current path: /Users/melanie
command(2): requirements_smf_lib_install libtool
[ ] libtool 2.4.2 ~ dependencies...
[[32m✔[0m] libtool 2.4.2 ~ dependencies
[ ] libtool 2.4.2 ~ fetch... curl: (9) Server denied you to change to the given directory
[[32m✔[0m] libtool 2.4.2 ~ fetch
FAIL(paths): Path '/opt/sm/src/libtool-2.4.2' does not exist.
+# source file # function()
================================================================================
vi +330 /opt/sm/core/sm/shell/log/functions # __sm.log.fail
vi +32 /opt/sm/core/internal/shell/paths/functions # __sm.paths.enter
vi +144 /opt/sm/core/api/shell/paths/functions # paths
vi +54 /opt/sm/core/internal/shell/package/strategies/gnu/functions # __sm.package.gnu.install
vi +15 /opt/sm/core/internal/shell/package/functions # __sm.package.install
vi +143 /opt/sm/core/api/shell/package/functions # package
vi +403 /opt/sm/core/sm/shell/log/functions # __sm.actions.call
vi +178 /opt/sm/core/internal/shell/extensions/execution/functions # __sm.extension.run
vi +151 /opt/sm/core/sm/shell/core/initialize # /opt/sm/core/sm/shell/core/initialize
vi +41 /opt/sm/bin/sm # /opt/sm/bin/sm
================================================================================
There were package installation errors with SM Framework, make sure to read the log.
If you see this on OSX, then you might want to try macports (http://www.macports.org/) or homebrew (http://mxcl.github.io/homebrew/) and tell RVM to use them by running:
rvm autolibs macports #OR
rvm autolibs homebrew
You can get help using IRC: http://webchat.freenode.net/?channels=rvm
How do I resolve the issues so that I can upgrade using RVM. Alternatively, how do i install manually?
Many thanks in advance.
rvm lets you have all the versions and then you switch between them for whichever interpreter you want to run your code against.
Use rvm install 2.0 to install Ruby 2.0.
Use rvm use 2.0 to use it
Use rvm use 2.0 --default to set it as the default

Resources