Raspberry Pi

How to Install and Use Ruby on Raspberry Pi

Ruby is one of the most versatile programming languages that supports several built-in functions to interact and execute desired functions on the operating system. It is widely used for creating web applications and support for multiple programming paradigms. Besides that, it has an elegant syntax, which you can easily read and understood.

If you want to learn Ruby on a Raspberry Pi system, follow this article to install this programming language and use it to code on your system.

Install and Use Ruby on Raspberry Pi

By default, Ruby is installed by default on Raspberry Pi system, however, if you have accidentally deleted it, use the following command anytime to install it on Raspberry Pi:

$ sudo apt-get install ruby

In order to check the version of the installed Ruby language on Raspberry Pi, type the following command:

$ ruby -v

Use Ruby on Raspberry Pi

To start learning Ruby programming language, first it’s better to create a projects directory using the following command:

$ mkdir projects

After that, you must navigate to the projects directory using the following command:

$ cd projects

The file extension for Ruby is .rb, so, any file you create must have a .rb extension. For the time being, I am creating a project file with the name “hello.rb” that outputs the message on the terminal.

To create a file, you can use the default “nano” editor.

$ nano hello.rb

Add the following simple code to output the “hello” message:

message="Message you want to type"

puts(message)

Save the file using “CTRL+X” keys and add Y, then press Enter to exit.

To run the file using Ruby, apply the following code.

$ ruby hello.rb

In this way, you can write and run multiple Ruby codes on the Raspberry Pi system through this way.

Create a Web Application Using Ruby

You can also use the “Sinatra” service to quickly create a web application with Ruby. However, before that, you must install “Sinatra” from the following command:

$ sudo gem install sinatra

Afterwards, create a new file using the nano editor with .rb extension.

$ nano welcome.rb

Then add the following lines inside the file:

require 'sinatra'

 

get '/hi' do

   "Message"

end

Save the file.

Now, use the command given below to run the file:

$ ruby welcome.rb

Now, open the Raspberry Pi browser and enter the following address to view the message on the web.

http://localhost:4567/hi

Through this way, you can create different web applications using Sinatra and Ruby.

Conclusion

Ruby is an open-source programming language used for creating web applications. Though it’s already installed on the Raspberry Pi system, you can follow the above guidelines to learn how to use this language to print a simple message or utilize Sinatra with Ruby to quickly create a web application on Raspberry Pi system.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.