how to use multiple ruby version in mac as per project

Using Multiple Ruby Versions in macOS for Different Projects

To use multiple Ruby versions in macOS for different projects, you can utilize a version manager like RVM (Ruby Version Manager) or rbenv. These tools allow you to install and manage multiple Ruby versions on your system and easily switch between them based on your project requirements.

Here are the steps to use multiple Ruby versions using RVM:

  1. Install RVM: Start by installing RVM on your macOS system. Open a terminal and run the following command to install RVM:

\curl -sSL https://get.rvm.io | bash -s stable

This command will download and install RVM along with its dependencies.

  1. Install Ruby Versions: Once RVM is installed, you can install multiple Ruby versions. Run the following command to list the available Ruby versions:

rvm list known

This command will display a list of available Ruby versions. Choose the desired version(s) and install them using the following command:

rvm install <ruby_version>

Replace <ruby_version> with the specific version you want to install (e.g., rvm install 2.7.4).

  1. Set Default Ruby Version: After installing multiple Ruby versions, you can set a default version to be used when a Ruby version is not specified for a project. Run the following command to set a default Ruby version:

rvm use <ruby_version> --default

Replace <ruby_version> with the desired default version (e.g., rvm use 2.7.4 --default).

  1. Create Project-Specific Gemsets: Gemsets allow you to isolate gem dependencies for different projects. To create a gemset for a specific project, navigate to the project directory in the terminal and run the following command:

rvm gemset create <gemset_name>

Replace <gemset_name> with a name for your gemset (e.g., rvm gemset create my_project).

  1. Associate Ruby Version and Gemset with Project: Once the gemset is created, you can associate a specific Ruby version and gemset with your project. Run the following command in the project directory:

rvm use <ruby_version>@<gemset_name>

Replace <ruby_version> with the desired Ruby version and <gemset_name> with the gemset name you created (e.g., rvm use 2.7.4@my_project).

This command will set the specified Ruby version and gemset as the default for the current project.

  1. Switching Ruby Versions: To switch between Ruby versions for different projects, navigate to the project directory and run the following command:

rvm use <ruby_version>@<gemset_name>

Replace <ruby_version> with the desired Ruby version and <gemset_name> with the associated gemset name.

This command will switch the Ruby version and gemset for the current project.

By following these steps, you can effectively use multiple Ruby versions in macOS for different projects using RVM. Remember to switch to the appropriate Ruby version and gemset whenever you work on a specific project.

Note: Another popular version manager for Ruby is rbenv. The steps for using multiple Ruby versions with rbenv are similar but involve different commands. You can choose the version manager that best suits your needs.