Installing ruby with ruby-install causes build error on Mac M1
Asked Answered
M

7

27

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));
Musetta answered 15/3, 2021 at 20:41 Comment(2)
2.7 is a work in progress. I'm not sure 2.6 is getting any attention at all until the 2.7 problems are resolved and back-ported.Crine
I've been getting this error too, and I haven't found any solutions so far :(Leboeuf
S
26

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.

Symons answered 25/4, 2021 at 2:21 Comment(6)
If brew reinstall rbenv ruby-build readline errors out in the cleanup step, reinstalling rbenv, ruby-build, and readline separately works!Copywriter
I had this error, and managed to Ruby to install simply using arch -x86_64 rvm install 2.4.4 -- CFLAGS=-DUSE_FFI_CLOSURE_ALLOC without the extra steps you mentioned (Note: this was for version 2.4.4)Severe
This finally worked for me after 1 day of sorrowReconstructionist
Wow... I was almost hopeless after trying so many things, but this one worked. Thanks!Frissell
got an error arch: Unknown architecture: x86_84 after arch -x86_84 :( oh.. m1...Soffit
nothing else helped, but this instruction worked from the first time 👍Chlorenchyma
S
16

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

Sternutatory answered 10/8, 2022 at 14:29 Comment(1)
Worked for me but I had to use this path instead: /opt/homebrew/opt/openssl@3Pilocarpine
P
7

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"
Puttergill answered 6/12, 2022 at 6:31 Comment(1)
My issue was making sure you have the right version of openssl linked in the path config, it was attempting to use [email protected] and not openssl@3, and adding that arch -arm64 install {{RUBY_VERSION}} did the trick!Wilford
S
5

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.

  1. 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.

enter image description here

  1. I have 2 versions of Homebrew. One in /opt/homebrew/bin/brew and the other in /usr/local/bin/brew.

  2. 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.

Stralka answered 14/4, 2021 at 14:59 Comment(0)
S
4

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

Siphonostele answered 8/10, 2022 at 10:5 Comment(2)
This was the only thing that worked for me after much sorrow.Pops
What does RUBY_CFLAGS="-w" do?Insure
A
2

This is what worked in M1. I have rosetta 2.

  1. Make sure you have latest rbenv, openssl, readline, libyaml & ruby-build

Finally run this command:

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 -arm64 rbenv install --verbose 3.0.6
Advisement answered 27/7, 2023 at 0:47 Comment(0)
G
0

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

Rosetta Terminal

Glory answered 24/1, 2023 at 19:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.