How Do I Run a Web Server in Ruby?

//

Heather Bennett

Running a web server in Ruby is a fundamental skill for any aspiring web developer. In this tutorial, we will walk you through the steps required to set up and run a web server using Ruby. Let’s get started!

Step 1: Install Ruby

Before running a web server in Ruby, you need to have Ruby installed on your machine. Visit the official Ruby website (https://www.ruby-lang.org/) and download the latest stable version of Ruby for your operating system.

Once the download is complete, follow the installation instructions specific to your operating system. After successful installation, open your terminal or command prompt and type `ruby -v` to verify that Ruby has been installed correctly.

Step 2: Choose a Web Framework

Ruby offers several popular web frameworks to choose from, such as Ruby on Rails, Sinatra, and Hanami. Each framework has its own advantages and caters to different needs. For this tutorial, we will use Sinatra as it is lightweight and straightforward.

To install Sinatra, open your terminal or command prompt and type:

gem install sinatra

This command will fetch and install the latest version of Sinatra from the RubyGems repository.

Step 3: Create a Simple Web Server

Now that we have Sinatra installed let’s create a simple web server using it.

Create a new file with a `.rb` extension (e.g., `app.rb`) and open it in your preferred text editor.

Inside the file, add the following code:

require 'sinatra'

get '/' do
  'Hello World!'
end

Save the file once you’ve added this code. This simple application responds with “Hello World!” when accessed at the root URL (“/”).

Step 4: Run the Web Server

To start our web server, open your terminal or command prompt, navigate to the directory where your `app.rb` file is located, and type:

ruby app.rb

You should see output similar to:

[2021-01-01 13:37:00] INFO  WEBrick 1.7.0
[2021-01-01 13:37:00] INFO  ruby 2.2 (2020-10-01) [x86_64-darwin19]
== Sinatra (v2.1.0) has taken the stage on 4567 for development with backup from WEBrick
[2021-01-01 13:37:00] INFO  WEBrick::HTTPServer#start: pid=1234 port=4567

Congratulations! Your web server is now running on localhost, port 4567.

Step 5: Test the Web Server

Open your web browser and navigate to http://localhost:4567/. You should see the text “Hello World!” displayed in your browser.

Step 6: Building a Web Application

Now that you have a basic understanding of running a web server in Ruby, you can start building more complex web applications by adding routes, views, and logic to your Sinatra application.

For example, you can create a route that handles requests to “/about” and displays information about your application:

get '/about' do
  'This is my awesome Ruby web application!'
end

Save the changes to your `app.rb` file and restart the server using `ruby app.rb`. Now when you visit http://localhost:4567/about, you will see the new message displayed.

Congratulations!

You have successfully learned how to run a web server in Ruby using the Sinatra framework. This is just the tip of the iceberg when it comes to web development with Ruby, but it provides a solid foundation for building more complex applications.

Remember to experiment with different web frameworks and explore the vast ecosystem of Ruby gems to enhance your web server and create powerful web applications. Happy coding!

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy