
December 3, 2024
Ruby is a powerful, elegant programming language known for its simplicity and productivity. While it’s often used for web development with frameworks like Ruby on Rails, it’s also perfectly capable of running as a lightweight web server. In this article, we’ll build a basic HTTP server using just Ruby’s built-in socket library. This will provide you with a solid foundation for understanding how web servers work and how Ruby can be used for low-level network programming.
Do you need more hands for your Ruby on Rails project?

Why Build a Web Server in Ruby?
Building a simple web server from scratch is a great way to deepen your understanding of HTTP and networking. While web frameworks like Ruby on Rails abstract away many complexities, building a custom server allows you to appreciate the underlying mechanics. Plus, it’s a fun and educational project!
Prerequisites
Before we dive into the code, you should have a basic understanding of Ruby and how to run Ruby scripts. You should also be familiar with the fundamentals of HTTP requests and responses.
Step 1: Create the Server
Let’s begin by creating a simple TCP server using Ruby’s built-in socket library. This will allow us to listen for incoming connections and process HTTP requests.

Step 2: How It Works
Let’s break down the code to understand how it works:
- TCPServer Initialization: The TCPServer.new(‘localhost’, 3000) line creates a server that listens on localhost (your machine) and port 3000. This means your server will be accessible at http://localhost:3000.
- Accepting Connections: The server enters an infinite loop (loop do) where it constantly waits for incoming connections. The server.accept method blocks until a client connects. Once a client connects, it returns a client object representing the connection.
- Handling the Request: The server reads the incoming request using client.gets. This will capture the first line of the HTTP request (the request line), which typically contains information like the HTTP method and path (e.g., GET / HTTP/1.1).
- Sending the Response: The server then sends a basic HTTP response. The response starts with the HTTP version (HTTP/1.1), followed by the status code (200 OK), and a content-type header (text/plain). Finally, the response body is sent, which in this case is just the string “Hello, World from Ruby Web Server!”.
- Closing the Connection: After sending the response, the server closes the connection with client.close. This is important because HTTP is a request-response protocol, and once the server has responded, it terminates the connection.
Step 3: Run the Server
To run the server:
- Save the code to a file, for example, simple_server.rb.
- Open a terminal and navigate to the directory where the file is saved.
- Run the command ruby simple_server.rb to start the server.
- Open your browser and navigate to http://0.0.0.0:3000.

You should see the message “Hello, World from Ruby Web Server!” displayed in the browser.
Step 4: What’s Next?
This basic server only handles a single type of HTTP request (GET requests) and always sends the same response. However, you can extend this server to:

- Handle different HTTP methods (e.g., POST, PUT, DELETE).
- Parse the request to handle different paths (e.g., /about, /contact).
- Serve dynamic content by integrating Ruby code or templates.
You could even use this server as the foundation for building a lightweight web application. While frameworks like Ruby on Rails offer more advanced features and better scalability, this simple server can serve as a great introduction to the principles behind web applications.
Conclusion
Building a web server in Ruby is a great way to learn more about networking and HTTP. While this server is minimal and not production-ready, it highlights the simplicity and power of Ruby. You can use it as a stepping stone to more complex server-side applications, or as a tool for learning and experimentation.
By using Ruby’s built-in libraries and understanding how HTTP requests and responses work, you’ll gain a deeper appreciation of how the web operates under the hood.
