Mastering Ruby on Mac: A Step-by-Step Installation Guide
Ruby, a dynamic, open-source programming language, has gained significant traction in recent years, powering some of the web's most visited sites like GitHub and Airbnb. If you're a Mac user eager to delve into the world of Ruby development, you've come to the right place. This comprehensive guide will walk you through the process of installing Ruby on your Mac, ensuring you're up and running in no time.
Before we dive into the installation process, ensure your MacOS is up-to-date. Ruby's official website recommends using macOS 10.15 or later. Additionally, having Homebrew, a popular package manager, installed on your Mac will make the process smoother. Don't worry if you don't have it yet; we'll guide you through installing Homebrew as well.
Installing Homebrew
Homebrew is a crucial tool for MacOS that simplifies package installation and management. Before we install Ruby, let's ensure Homebrew is set up on your system.
Open your Terminal and paste the following command to install Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Press 'Enter', and you'll be prompted to enter your password. After successful installation, you'll see "Installation successful!"
Verifying Homebrew Installation
To ensure Homebrew is installed correctly, update the package list by running:
brew update
Then, check the version of Homebrew by running:
brew --version
Installing Ruby via Homebrew
With Homebrew now set up, installing Ruby is just a breeze. Let's install the latest stable version of Ruby using Homebrew.
In your Terminal, simply type:
brew install ruby
Press 'Enter', and wait for Homebrew to fetch and install Ruby. Once complete, you'll see a message similar to "==".
Verifying Ruby Installation
To confirm Ruby was installed correctly, type:
ruby -v
You should see the installed Ruby version as output.
To ensure the new Ruby environment is set up correctly, create a simple Ruby script and run it. In Terminal, create a new file named "script.rb" using the command 'nano script.rb'. Paste the following code into the file:
puts "Hello, world!"
Press 'Ctrl + X', then 'Y', then 'Enter' to save and exit. Now run the script with the command 'ruby script.rb'. You should see "Hello, world!" printed in your terminal.
Bonus: Installing RubyGems
RubyGems is a package manager for Ruby, helping you manage and distribute Ruby software. It comes pre-installed with Ruby but isn't added to your system PATH by default. To add RubyGems to your PATH:
echo "export PATH="/usr/local/opt/ruby/bin:$PATH\"" >> ~/.zshrc
Then, close your Terminal and reopen it, or run 'source ~/.zshrc' to update your shell.
There you have it! You've successfully installed Ruby on your Mac. Now you're ready to dive into the world of Ruby development. Happy coding!