Install 2.x, 3.x version of Ruby via rbenv on M1 Mac
Tags: ruby, rails, m1
Published: 2022-07-06 09:28:28

I bought new M1 Mac and struggled to install Ruby.

Here’s what I did to solve the issue.

This solution worked for me installing Ruby 2.6.6 and 3.0.2.

1. Installed `anyenv` via brew
    
    ```jsx
    brew install anyenv
    ```
    
2. Installed `rbenv` via anyenv
    
    ```jsx
    anyenv install rbenv
    ```
3. Then I got an error while installing Ruby `rbenv install 2.6.6`
    
    ```jsx
    rbenv install 2.6.6
    Downloading openssl-1.1.1q.tar.gz...
    -> https://dqw8nmjcqpjn7.cloudfront.net/d7939ce614029cdff0b6c20f0e2e5703158a489a72b2507b8bd51bf8c8fd10ca
    Installing openssl-1.1.1q...
    
    BUILD FAILED (macOS 12.3 using ruby-build 20220630-3-g2155888)
    
    Inspect or clean up the working tree at /var/folders/7w/__wt6xg10rv3jdq_z2rjt8300000gn/T/ruby-build.20220706155948.38261.B1lwKj
    Results logged to /var/folders/7w/__wt6xg10rv3jdq_z2rjt8300000gn/T/ruby-build.20220706155948.38261.log
    
    Last 10 log lines:
    clang  -Iinclude -arch arm64 -O3 -Wall -D_REENTRANT -DZLIB -DZLIB_SHARED -DNDEBUG -I/Users/yas/.anyenv/envs/rbenv/versions/2.6.6/include  -MMD -MF test/wpackettest.d.tmp -MT test/wpackettest.o -c -o test/wpackettest.o test/wpackettest.c
    clang  -Iinclude -arch arm64 -O3 -Wall -D_REENTRANT -DZLIB -DZLIB_SHARED -DNDEBUG -I/Users/yas/.anyenv/envs/rbenv/versions/2.6.6/include  -MMD -MF test/x509_check_cert_pkey_test.d.tmp -MT test/x509_check_cert_pkey_test.o -c -o test/x509_check_cert_pkey_test.o test/x509_check_cert_pkey_test.c
    test/v3ext.c:201:24: error: implicitly declaring library function 'memcmp' with type 'int (const void *, const void *, unsigned long)' [-Werror,-Wimplicit-function-declaration]
            if (!TEST_true(memcmp(ip1->data, ip2->data, ip1->length) <= 0))
                           ^
    test/v3ext.c:201:24: note: include the header <string.h> or explicitly provide a declaration for 'memcmp'
    1 error generated.
    make[1]: *** [test/v3ext.o] Error 1
    make[1]: *** Waiting for unfinished jobs....
    make: *** [all] Error 2
    ```
    
4. This error is related to Openssl
    1. His post was so helpful.
    [https://github.com/rbenv/ruby-build/discussions/1853#discussioncomment-2146106](https://github.com/rbenv/ruby-build/discussions/1853#discussioncomment-2146106)
        
        > Fundamentally speaking, it is caused by OpenSSL.
        OpenSSL@3.0 came in Sep 2021, but it was not compatible with ruby-build. So we have to switch the OpenSSL@1.1.
        > 
5. Installed openssl version 1.1 using brew
    
    ```jsx
    brew install openssl@1.1
    ```
    
6. Then set PATH to openssl@1.1
    
    ```jsx
    yas@MBP2 ~> which openssl
    /usr/bin/openssl
    yas@MBP2 ~> fish_add_path /opt/homebrew/opt/openssl@1.1/bin // set PATH. this is for fish shell.
    // if you use other than fish, add '/opt/homebrew/opt/openssl@1.1/bin' to your PATH.
    yas@MBP2 ~> exec $SHELL -l //refresh shell
    yas@MBP2 ~> which openssl
    /opt/homebrew/opt/openssl@1.1/bin/openssl
    ```
    
7. Install Ruby using a prefix
    
    ```jsx
    CFLAGS="-Wno-error=implicit-function-declaration" rbenv install 2.6.6
    ```
    

Hope this helps