Ruby Programming/Installing Ruby
The first step to get started in Ruby development is setting up your local environment. Due to differences between various operating systems we will cover multiple of them. If you are already able to use a terminal emulator and know how to install Ruby yourself, you can skip this chapter (after you installed Ruby). Otherwise we will guide you through the process of installing Ruby on your computer.
Knowing how to use a terminal emulator is very useful if you are programming. Usually it will provide the most straightforward access to commands and applications instead of hiding it behind graphical interfaces. On the other hand they are often daunting to beginners since they are often perceived to require a deep understanding of a computer when in fact often only knowing the very basics is already enough to get started.
Unix-like operating systemsEdit
One of the most commonly used shells in the Unix-like operating systems (i.e. macOS, GNU/Linux, BSD) is the Bash shell, in fact it is very often the default shell. To start a session you will often use a terminal emulator, which allow you to use a terminal session at the same time as other graphical applications. It doesn't really matter, which terminal emulator you use, generally you want one that has color and Unicode support. In macOS you can use Terminal.app which you can find under Applications > Utilities. A popular alternative is iTerm. On most Linux distributions you will usually be provided with at least one terminal emulator by default, otherwise you might want to try Terminator, Konsole, rxvt-unicode or something different.
When you open a new window or tab in your terminal emulator of choice you will be shown your prompt. What it looks like exactly depends a lot on configuration, which can vary greatly from OS to OS (you can configure everything to your likings, however this exceeds the scope of this short introduction). Generally it will indicate your current working directory, username and hostname. When working in the shell your session always has current working directory. Commands that accept relative filenames will use that directory as the base directory to look for files. By default you are in your user's home folder, which is often abbreviated with a tilde (
To execute a command you just type it into the shell and press enter.
At first we want to look at the command
ls. If you type it in just like that it will print the files and directories in your current working directory. You can also provide a relative path to a directory you want to list, e.g.
ls DIR. If you want more detailed information about the files you can use
ls -l DIR, if you instead want to also include invisible entries (i.e. names starting with a dot) use
ls -a. Of course it is possible to combine them both by running
ls -l -a DIR or the short form
ls -la DIR. Note that this kind of concatenating multiple arguments into one is only possible with single character parameters. Parameters can also come in long form, for example the equivalent of
ls -a is
ls --all DIR. Which forms are available depends on the individual command.
Now you might be thinking how to remember all parameters for every command you will ever use. Thankfully you only want to remember the most important ones, which are the ones you use most frequently, otherwise there is a nice way to look them up. You can either use the
man command. For example run
man ls to find more information about the
ls command. Oftentimes you can find a more concise summary by trying to run the command in question followed by the parameter
--help, however this is not something you can expect to work well with every command, whereas manual pages should be always available.
Back to the topic of current working directories. If you want to change your directory you can use
cd followed by the directory you want to change to. There are two special virtual directories '.' and '..'. The single dot refers to the current directory while the double dot refers to a dir's parent directory. So executing
cd .. changes into the parent directory of the current working directory.
A very brief summary of other useful commands:
cat FILE: display the contents of a file.
mkdir DIR: create a directory.
Windows doesn't use the Bash shell and in the past it was hard to get Bash to work on Windows. Users of Windows 10 Anniversary Update build 14393 or later however now have the ability to install a Ubuntu subsystem which will essentially provide them with a working Bash shell and access to many commands found in the Ubuntu Linux distribution. Users wishing to use Bash on Windows 10 are advised to follow the official installation instructions from Microsoft. After the installation you can basically follow the instructions for Ubuntu/Debian.
If you don't want to do that you have two options:
- Go with another non-Microsoft project providing a Bash environment for Windows. For example you can install Cygwin, a collection of free software tools available for Windows. During the install, make sure that you select the "ruby" package, located in the "Devel, Interpreters" category.
- Use the Windows Powershell. You might sometimes have to write something different than in this book, but for basic examples this might still be enough and safe you the time you would otherwise need for installation. However if you are serious about learning a shell, learning Bash has the advantage that it is not exclusive to Windows, unlike Powershell, which prevents you from being locked in into a platform should you one day decide to switch to Unix-like operating systems or use a non-Windows server.
A common and easy way to install Ruby is to perform a system-wide installation. Depending on the operating system installation procedures will be different (if required at all).
Ruby comes preinstalled on macOS. To check which version is installed on your system, execute
ruby -v inside a shell session.
If you want to install a more recent version of Ruby, you can:
- Update to a newer version of macOS, which may have a more recent version of Ruby.
- Install Ruby using RVM. (This is the most popular way because you can manage Ruby versions and install many other Ruby packages)
- Install Ruby using Fink.
- Install Ruby using MacPorts.
- Install Ruby using Homebrew.
On many Linux distributions Ruby is installed by default. To check if Ruby is installed on your system, run
ruby -v in a shell session.
Where this is not the case, or you want to update the installed version, you should use your distribution's package manager. Here we will provide information for some popular Linux distributions here, however it is recommended to users of all distributions to familiarize themselves with their distribution's package manager, since this will allow for the most efficient software management. Whether this is a command-line or graphical application depends on the offerings of the distribution and personal preference of the user.
Debian / UbuntuEdit
The package manager Synaptic provides graphical package management. It is installed by default under Ubuntu and has to be installed manually on Debian (by running
sudo apt-get install synaptic from the command line).
Instead of using Synaptic you can also use apt directly from the command-line (you can find further information in the Debian Wiki's article on Package Management). Execute
sudo apt-get install ruby from the command line to install Ruby.
From the command-line you can install Ruby with DNF by executing
sudo dnf install ruby.
Use pacman to install Ruby by executing
pacman -S ruby as root.
On PCLinuxOS, install Ruby using either the graphical tool Synaptic or the command-line tool apt.
Red Hat LinuxEdit
Ruby does not come preinstalled with any version of Microsoft Windows. However, there are several ways to install Ruby on Windows.
- Download and install one of the compiled Ruby binaries from the Ruby web site.
- Download and run the one click RubyInstaller.
- Install Cygwin, a collection of free software tools available for Windows. During the install, make sure that you select the "ruby" package, located in the "Devel, Interpreters" category.
Windows is slowEdit
Currently Ruby on windows is a bit slow. Ruby isn't optimized for windows, because most core developers use Linux. Though 1.9.2 passes almost all core tests on windows.
Most of today's slowdown is because when ruby does a
it searches over its entire load path, looking for a file named xxx, or named xxx.rb, or xxx.so or what not. In windows, doing file stat's like that are expensive, so requires take a longer time in windows than linux. 1.9 further complicates the slowdown problem by introducing gem_prelude, which avoids loading full rubygems (a nice speedup actually), but makes the load path larger, so doing require's on windows now takes forever. To avoid this in 1.9.2, you can do a
which reverts to typical load behavior.
If you want to speed it up (including rails) you can use
Which have some work arounds to make loading faster by caching file locations.
Also the "rubyinstaller" (mingw) builds are faster than the old "one click" installers If yours comes from rubyinstaller.org, chances are you are good there.
NB that Jruby tends to run faster but start slower, on windows, than its MRI cousins. Rubinius is currently not yet windows compatible.
Building from SourceEdit
Building with debug symbolsEdit
If you want to install it with debug symbols built in (and are using gcc--so either Linux, cygwin, or mingw).
./configure --enable-shared optflags="-O0" debugflags="-g3 -ggdb"
Note that with 1.9 you can pass it
--disable-install-doc to have it build faster.
To set the GC to not run as frequently (which tends to provide a faster experience for larger programs, like rdoc and rails), precede your build with
$ export CCFLAGS=-DGC_MALLOC_LIMIT=80000000
though you might be able to alternately put those in as opt or debug flags, as well.
The installation can be tested easily by executing:
$ ruby -v
This should produce an output similar to:
ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]
If this shows up, then you have successfully installed Ruby. However if you get an error similar to:
-bash: ruby: command not found
then you did not successfully install Ruby.