Last modified: Mon Aug 28 2017 02:18:33 GMT+0800 (Malay Peninsula Standard Time)
The following guide was tested only on machine running MacOS Sierra (10.12). If you are using older version of MacOS, this tutorial may not work. At the time this tutorial is being written the latest stable Ruby version for MacOS is Ruby 2.4.0. If any of the command require you to match the Ruby version that you have downloaded, change the version accordingly.
Open a terminal as shown in Figure 3.1.1.
Figure 3.1.1: Terminal Window icon in Applications
The root directory of the terminal as shown in Figure 3.1.2 is your Home directory.
Figure 3.1.2: Terminal Window in MacOS Sierra
By default, modern MacOS come preinstalled with Ruby. To check your Ruby version, enter the command below.
After the command above is inserted, you will see the Ruby version on your terminal window. Figure 3.1.3 shows the terminal window with Ruby version 2.0.0 returned to the terminal window.
Figure 3.1.3: Ruby version
The default Ruby version, Ruby 2.0.0. is old. The latest stable Ruby version for MacOS when this tutorial is written is Ruby 2.4.0. To update your Ruby in your Mac to the latest version, enter the command below. Figure 3.1.4 shows the output after the command is entered into the terminal window.
curl -sSL https://get.rvm.io | bash -s stable --ruby
Figure 3.1.4: Updating Ruby version
If you do not have XCode installed in your machine, a window will pop up and ask you to see if you would like to install XCode. XCode and Git command line are part of the developer tools required in order to update Ruby on your machine. Select Install, as shown in Figure 3.1.5, to install the required files and enter your admin password in the terminal window.
Figure 3.1.5: XCode installation window pop up and Admin password request
A License Agreement window, as shown in Figure 3.1.6 will then pop up. Press "Agree" to continue.
Figure 3.1.6: License Agreement window
If you received
Return Error 1 message as shown in Figure 3.1.7, skip to Chapter 3.2. However, if you received an output as shown in Figure 3.1.8, you have successfully updated your Ruby version to Ruby 2.4.0 (as shown in the console output).
Status 1 error message
Figure 3.1.8: Ruby 2.4.0 installed successfully
Now, set Ruby 2.4.0 as your default Ruby version, enter the command below. Figure 3.1.9 shows the default Ruby version is set to Ruby 2.4.0. If you are not using Ruby 2.4.0, change the number in the command according to the version shown in Figure 3.1.8. If the code does not return a correct Ruby version, please repeat the installation process:
rvm use 2.4 --default
To check your updated Ruby version, use the command
Figure 3.1.7: Ruby 2.4.0 is set as default Ruby version
Proceed to Chapter 3.3 to install Rails.
If you received
Return Error 1 message as shown in Figure 3.2.1, continue reading. If not, proceed to Chapter 3.3 to install Rails.
Status 1 error message
The reason this error shows up is because you do not have Homebrew installed. To fix this, install Homebrew by entering the command below into your terminal. A confirmation screen, as shown in Figure 3.2.2, will display all the files and scripts that will be installed on your machine. Press
return key to install the files.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Figure 3.2.2: Homebrew installation confirmation screen
If your machine is password protected, you will asked to enter your password next. Figure 3.2.3 shows the password request window.
Figure 3.2.3: Password request window
Figure 3.2.4 shows Homebrew is installed successfully to your machine. Now, update your Ruby by repeating the process as discussed in Chapter 3.1.
Figure 3.2.4: Homebrew installed successfully
To download Rails, enter the code below in a Terminal Window. Figure 3.3.1 shows the input and the output text from the Terminal. This operation can take up to 5 minutes depending on your computer’s processing speed and Internet connection.
gem install rails
Figure 3.2.1 Input and the output text from the Terminal
After the installation is complete, run the following code to check if Ruby is installed successfully. Figure 3.2.2 Rails version in the Terminal Window. If the code does not return a Rails version, please repeat the installation process.
Figure 3.2.2: Rails version shown in Terminal Window
To create your website, it is highly recommending to create a folder name Code in the Ruby file directory. The purpose the folder Code is to store all your source code folder. The Ruby folder location may vary depending on your Operating System Architecture.
Enter the folder Code and create a sample application name Blog. Enter the following command to command line window a similar output as shown in Figure 3.4.1 below will be generated.
rails new Blog
Figure 3.4.1 Generating a new Blog
To start the Web Server, navigate to the folder of your code and run the follow command below. Figure 3.5.1 shows a Rails server is running.
Figure 3.5.1: A Rails server is running
Insert the URL in your Web Browser based on what you have seen from the Terminal Window to visit your website. Figure 3.5.2 shows a running Ruby on Rails website. Based on the Figure 3.5.1,
localhost:3000 is the path to my website.
Figure 3.5.2 A working Ruby on Rails website
A second command line window is opened to prevent termination of local Web Server. To verify that Ruby and Rails were installed correctly without any errors, use the scaffold command to generate a simple set of a model, views, and controller for Rails Application.
rails g scaffold User name:string age:integer
A similar output as shown in Figure 3.6.1 will be generated from the command line tool.
Figure 3.6.1: Output of terminal window when a scaffold command is used.
Run the command rake db:migrate to migrate the database generated by the command scaffold to local database server. A similar output as shown Figure 3.6.2 will be generated from the command line tool.
Figure 3.6.2: Output of terminal window when a migrate command is used.
Navigate to the index page of newly generated page by entering the path at your browser. The index page of
Users will be generated as shown in Figure 3.6.3.
Users index page
To make sure that the application is working and does not throw any error when new data are committed to the database, press the link New User create a new sample data set. A form will be rendered a shown in Figure 3.6.4. Populate the form with any sample data that you prefer and press Create User.
Figure 3.6.4: New user form
An alert message, as shown in Figure 3.6.5, User was successfully created alert message will be shown on the screen when the data is inserted into the database successfully.
Figure 3.6.5: User was successfully created alert
To redirect to the index page of
Users, press the Back button located under the Age shown in Figure 3.6.5. By looking at Figure 3.6.6, we can see that the
Users index page was being rendered.
Users index page was rendered from output log
Figure 3.6.7 shows a picture of
Users index page with the data that populated in the form earlier.
Users index page
Congratulation, you have successfully installed Ruby on Rails on your own machine!