How to install and use Ruby on Windows
Why install Ruby
Ruby is a scripting language, that is, pre-compilation is not required to run programs. In this sense, Ruby is an analogue of PHP, Python, PERL and others.
Ruby is a fairly popular language and many interesting programs are written on it; in scope of InfoSec, the well-known WPScan, WhatWeb, Wayback Machine Downloader and others can be cited as examples.
By installing Ruby on Windows, you can run programs written in this language, as well as learn this programming language and write your own scripts.
By the way, Ruby, like PHP, Python and PERL, can be a web server module, and scripts written in this language can be used as backend of a website or service.
How to install Ruby on Windows
To install, go to https://rubyinstaller.org/downloads/
There you will see many installer options that differ not only in versions, but also in the composition of the downloaded files. Executable files are self-contained installers for Windows that include the Ruby language, runtime, important documentation, and more. If you do not know which version to install in order to get started with Ruby, the Ruby+Devkit * (x64) is recommended. It provides the largest number of compatible gem (Ruby packages) and installs MSYS2-Devkit along with Ruby, so gem with C-extensions can be compiled immediately after installation.
Run the downloaded file. In this window we can select the settings:
The installation folder can be left unchanged.
Add Ruby executables to your PATH – means adding a directory with Ruby executables to the system variable. This is recommended to do for in future to avoid specifying the full path to the script interpreter each time the Ruby script is run.
Associate .rb and .rbw files with Ruby installation. Thanks to this, Ruby files can be launched by double-clicking or by typing the name of the script on the command line.
Use UTF-8 as default external encoding.
I selected all three checkmarks and entered C:\Ruby26 as the installation folder:
As you can see on the next page, the Ruby package itself takes up a bit of space, but the MSYS2 development toolkit offered for installation takes up a lot of space. I highly recommend installing MSYS2 because, in addition to the ability to compile packages for Ruby mentioned above, MSYS2 provides a console environment with Linux features (something like Cygwin does, but with additional features).
Leave a checkmark on the last window to configure the MSYS2 developer tools and click Finish.
Further we are offered:
1 - MSYS2 base installation 2 - MSYS2 system update (optional) 3 - MSYS2 and MINGW development toolchain Which components shall be installed? If unsure press ENTER [1,2,3]
Just press ENTER to complete all three actions:
Everything is completed, to exit, press ENTER:
By the way, if you carefully watched what was happening on the screen, you might notice pacman. There really is a pacman package manager in this console environment. We will return to other functions of MSYS2 a bit later.
How to update Ruby on Windows
To upgrade to the latest patch (that is, the minor version, for example from 2.5.1 to 2.5.4), it is enough to launch a new version of the installer. Installed gem (packages from the Ruby repository) are not overwritten and will work with the new version without reinstalling. To upgrade the installation, just use RubyInstaller without Devkit. You can update Devkit separately by running the following command at the Windows command prompt:
ridk install
When a new major version is released, it cannot be updated by installing it in the same directory as the previous one. For example, if the previous version of the installation is RubyInstaller-2.5.x, and the new version is RubyInstaller-2.6.x, then you need to install it either in a new directory or delete the old version and install a new one instead, since gem (programs) with C extensions are not compatible between ruby-2.5 and 2.6.
How to install and use gem on Windows
RubyGems is a package manager for Ruby. Using it, you can install various programs and their dependencies, installation can be done both from the source code on the local system, and from remote application sources.
The Ruby installation shown above also installs gem:
gem --help
You should see gem help.
To display all available gem commands, run:
gem help commands
To install the package, run a command of the form:
gem install PROGRAM_NAME
More examples of installing packages will follow.
To show help about the installation command:
gem help install
For example, to install the 'rake' program from a local directory or a remote server:
gem install rake
Installing the 'rake' package only from a remote server:
gem install rake --remote
Installing 'rake', but only version 0.3.1, even if there are unsatisfied dependencies, do the installation in the user directory:
gem install rake --version 0.3.1 --force --user-install
List gem (packages) whose name begins with 'D':
gem list D
List local and remote gem whose name contains 'log':
gem search log --both
The previous command is used to search for packages by name.
List only remote (non-local) gem whose name contains 'log':
gem search log --remote
Delete 'rake':
gem uninstall rake
View information about RubyGems:
gem environment
Update all gem programs in the system:
gem update
Update local version of RubyGems:
gem update --system
How to install bundler
bundler is a Ruby dependency manager. This manager is useful when installing other programs written in Ruby.
To install bundler on Windows, run:
gem install bundler
To upgrade bundle, run the command:
gem update bundle
If you install the program from the source code and there is a Gemfile file, then go to the folder with this program and run the command in it:
bundle install
This command will install all the dependencies listed in the Gemfile.
To see where gem packages are installed using bundle, use the command:
bundle info name_gem
How to install the Ruby program on Windows. How to run a Ruby program on Windows
The following are a few examples of installing and using Ruby programs on Windows.
Recovering sites from the Internet Archive in Windows
Let's start with the Wayback Machine Downloader program, which completely restores sites from the web archive.
To install Wayback Machine Downloader, just do:
gem install wayback_machine_downloader
Let's check:
wayback_machine_downloader --help
To check the operability of the program, run site restoration from the Internet Archive:
wayback_machine_downloader https://suip.biz
The program successfully completed the work:
Folder structure:
dir .\websites\suip.biz\
How to Install WhatWeb on Windows
WhatWeb identifies websites. The purpose of this program is to answer the question “What is this website?”. WhatWeb recognizes web technologies, including a content management system (CMS), blogging platforms, statistics/analytics packages, JavaScript libraries, a web server and embedded devices. WhatWeb has over 1700 plugins, each of which is for recognizing one or the other. WhatWeb also identifies version numbers, email addresses, account IDs, web platform modules, SQL errors and more.
Let's start by searching WhatWeb in the Ruby package repository:
gem search whatweb --both
Only simple_whatweb found (0.4.1).
Even if it's WhatWeb, the version is very old. Therefore, on Windows, install WhatWeb from the source code.
To do this, download the archive with WhatWeb files: https://github.com/urbanadventurer/WhatWeb/archive/master.zip
Unzip this folder. Open a command prompt and navigate to this folder:
cd %userprofile%\Downloads\WhatWeb-master\
Run the command:
bundle install
The following command will show the help for the program:
ruby .\whatweb --help
Run the site check:
ruby .\whatweb mi-al.ru ruby .\whatweb w-e-b.site
How to install WPScan on Windows
To install and run WPScan on Windows is tricky a bit, so the tutorial was divided in separate article “How to Install and Run WPScan on Windows”.
What is MSYS2. How to use MSYS2
We have already installed MSYS2 in order to gain access to compilation tools, thanks to which we can install any Ruby package. In fact, MSYS2 is based on Cygwin and contains a large number of Linux utilities.
MSYS2 provides a shell for bash, Autotools, make, gcc, pacman, sh, and many other packages. Of particular note is the pacman package manager.
To activate the MSYS2 working environment, open a command prompt or powershell console and run there:
ridk enable
As a result, many Linux utilities will immediately become available for work, as well as the Pacman package manager, with which you can install new packages or update existing ones. When installing packages, Pacman monitors the dependencies and installs them.
To update all packages:
pacman -Syu
To search for a package:
pacman -Ss PACK_NAME
To list all available packages:
pacman -Ss
How to configure Ruby as an Apache module on Windows
Ruby scripts can be run in an Apache environment similar to running PHP scripts. To do this, you need to make a little configuration.
I installed the web server according to this tutorial, if you installed according to another manual, then edit the paths to fit your environment.
Open the httpd.conf file for editing, I have it located on the path C:\Server\bin\Apache24\conf\httpd.conf.
Find the line there
Options Indexes FollowSymLinks
and add ExecCGI to it. You should get the following line (ATTENTION: you may have different the set of options):
Options Indexes FollowSymLinks ExecCGI
Now find the line:
#AddHandler cgi-script .cgi
Uncomment it, i.e. remove the # character at the beginning of the line and add .rb to the end of the line. The new line will look something like this:
AddHandler cgi-script .cgi .rb
Restart Apache
c:\Server\bin\Apache24\bin\httpd.exe -k restart
Ruby CGI Test Page for Apache
In the folder for your sites (on my machine the path is C:\Server\data\htdocs\) create a test.rb file and copy it into it:
#!C:\Ruby26\bin\ruby.exe puts "Content-type: text/html\n\n" puts "" puts "<html>" puts "<body>" puts "Test Ruby Page." puts "</body>" puts "</html>"
Pay attention to the line #!C:\Ruby26\bin\ruby.exe - if you have a different path to the ruby.exe file, then edit the line accordingly.
Open this page in a web browser: http://localhost/test.rb
You should see
Test Ruby Page
As shown in the screenshot below:
Related articles:
- How to set up the PowerShell environment on Windows and Linux (74.7%)
- How to get started with Linux commands on Windows: Cygwin (70.9%)
- How to install Go (compiler and tools) on Linux (70.3%)
- How to install Python and PIP on Windows 10. How to set up Python as a web server module (59.5%)
- How to run PHP script without a web server (56.3%)
- How to install the driver for W-Fi for cards with RTL8812AU/RTL8821AU chipsets (Alfa AWUS036ACH, Alfa AWUS036AC) (RANDOM - 50%)