Ruby

How to Install Ruby on Ubuntu

Ruby is an open-source, dynamic, object-oriented, balanced programming language which is developed by Yukihiro “Matz” Matsumoto in Japan. It is implemented by merging the features of some others popular programming languages, such as PERL, Smalltalk, Ada etc. It has made a balance between functional programming with imperative programming. It is one of the most popular programming language now because it is designed to simplify the programming tasks but not based on machine requirements. It reduces most complex parts of the programming and the programmer can quickly build something from scratch without writing more codes. It is mainly designed for the beginner but anyone can use this language to create any complex application.

So, both novice and experienced programmers can use this language for programming. It is free of charge, so you can easily copy, use, modify and distribute this language without any restriction. Another important feature of ruby is mark-and-sweep garbage collection system. The programmer can write any code in ruby without maintaining the references of extension libraries for this feature. So, when you install Ruby in any supported operating system then ruby will dynamically load extension libraries.

You can install ruby in Linux operating system in various ways. Using apt package manager is the easiest way to install Ruby on Ubuntu operating system. How you can install ruby from command line and run ruby code on Ubuntu are shown in this tutorial.

Installing Ruby

Press Ctrl+Alt+T to open the terminal. Before running the command for installing ruby, you have to update your system. If the system is not updated then ruby will not install properly. Run the following command to update the system.

$ sudo apt-get update

Now, run the following simple command with root permission to install ruby.

$ sudo apt-get install ruby-full

Press “y” when it will ask for permission to complete the installation process.

If the installation completes successfully then check ruby is working properly or not. Run the following command. It will show ruby version which is installed in the system. The output shows that ruby version 2.3.3 is installed in the system.

Creating First Ruby Program

You can use any text editor to write ruby script. Here, nano editor is used. The extension for ruby file is rb. Suppose, you have to create a ruby file named first.rb. Then, run the following command from the terminal to open the editor.

$ nano first.rb

Simple input and output operations are shown in the following script. gets command is used in ruby to take input from the user and puts command is used in ruby to print output in the console. In ruby, + operator is used to combine string values.   In the following code, one input will be taken from the user in the variable name and print the value of name after adding other string.

Write the code in the editor.  Press Ctrl + X  and type y to save the file.

puts "Type your name :"
name = gets.chomp
puts "Welcome to LinuxHint " + name

Running Ruby Program

Run the following command from the terminal to run first.rb. If the script is error free then first it will print the message, “Enter your name”, Type something and press enter. It will then print the value that you have typed by adding the string “Welcome to LinuxHint”.

$ ruby first.rb

If you are new in programming, you can choose Ruby language as your first choice to learn programming. By following above simple way, you can easily install ruby in your Ubuntu operating system and start to learn programming or develop any application using ruby script.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.