170

On my development machine, I use port 10524. So I start my server this way :

rails s -p 10524

Is there a way to change the default port to 10524 so I wouldn't have to append the port each time I start the server?

Pierre Olivier Martel
  • 3,144
  • 4
  • 26
  • 30

10 Answers10

136

First - do not edit anything in your gem path! It will influence all projects, and you will have a lot problems later...

In your project edit script/rails this way:

#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.

APP_PATH = File.expand_path('../../config/application',  __FILE__)
require File.expand_path('../../config/boot',  __FILE__)

# THIS IS NEW:
require "rails/commands/server"
module Rails
  class Server
    def default_options
      super.merge({
        :Port        => 10524,
        :environment => (ENV['RAILS_ENV'] || "development").dup,
        :daemonize   => false,
        :debugger    => false,
        :pid         => File.expand_path("tmp/pids/server.pid"),
        :config      => File.expand_path("config.ru")
      })
    end
  end
end
# END OF CHANGE
require 'rails/commands'

The principle is simple - you are monkey-patching the server runner - so it will influence just one project.

UPDATE: Yes, I know that the there is simpler solution with bash script containing:

#!/bin/bash
rails server -p 10524

but this solution has a serious drawback - it is boring as hell.

Radek Paviensky
  • 8,316
  • 2
  • 30
  • 14
  • 15
    Or even an alias! `alias rs='rails server -p 10524'` – trisweb Sep 04 '13 at 16:00
  • 2
    Be sure to put the `require 'rails/commands'` AFTER the new stuff you paste in. Otherwise it will still try port 3000. – CJBrew Oct 31 '13 at 17:45
  • doesn't work for me, still starts at :3000. Howver @Spencer solution (on this page) works – Roman Feb 21 '14 at 11:30
  • Worked in one instance for me but not another. When I had to create my own script folder and rails file -> not great success. Probably has to do with whether or not rails is running off of the script or not? – gards Oct 16 '14 at 21:26
  • @trisweb Could you explain how to create `alias rs` – Selvamani Apr 26 '15 at 17:02
  • @Selvamani in the shell window, run `alias rs='rails server -p 10524'`. After that, you'll be able to type `rs` in that shell to perform the full command. You may want to make this alias permanent by adding it to `~/.bashrc`. – Eliot Sykes May 25 '15 at 14:30
131

I like to append the following to config/boot.rb:

require 'rails/commands/server'

module Rails
  class Server
    alias :default_options_alias :default_options
    def default_options
      default_options_alias.merge!(:Port => 3333)
    end    
  end
end
Spencer
  • 1,311
  • 1
  • 8
  • 2
  • 8
    Use `super` instead of alias hack. – Nowaker Mar 16 '13 at 18:03
  • 2
    Unfortunately, if `super` is used instead of alias, it calls the wrong method. It calls the `::Rack::Server` version of default_options. – codingFoo Sep 24 '13 at 20:34
  • 3
    With ruby 2.0 you could `prepend` an anonymous module instead of using `alias`. This allows a clean use of `super`. – exbinary Oct 02 '13 at 21:35
  • 2
    This answer will cause `Rails::Server` to become defined in contexts when it shouldn't be (e.g. running the Rails console). So I recommend putting the code at the end of `application.rb`, guarded with an `if defined?(Rails::Server)`. – XåpplI'-I0llwlg'I - Aug 05 '15 at 10:13
30

One more idea for you. Create a rake task that calls rails server with the -p.

task "start" => :environment do
  system 'rails server -p 3001'
end

then call rake start instead of rails server

Ross
  • 1,934
  • 2
  • 20
  • 19
17

Combining two previous answers, for Rails 4.0.4 (and up, presumably), this suffices at the end of config/boot.rb:

require 'rails/commands/server'

module Rails
  class Server
    def default_options
      super.merge({Port: 10524})
    end
  end
end
Thilo
  • 17,565
  • 5
  • 68
  • 84
  • How can I retrieve this option in the running app? Specifically, I want to set `config.action_mailer.default_url_options`, otherwise it's still pointing to port 3000. – Joshua Muheim Mar 31 '15 at 13:40
  • I added a question related to this here: http://stackoverflow.com/questions/29431315/actionmailers-default-url-options-how-to-retrieve-port-automatically-from-defa – Joshua Muheim Apr 03 '15 at 11:27
10

We're using Puma as a web server, and dotenv to set environment variables in development. This means I can set an environment variable for PORT, and reference it in the Puma config.

# .env
PORT=10524


# config/puma.rb
port ENV['PORT']

However, you'll have to start your app with foreman start instead of rails s, otherwise the puma config doesn't get read properly.

I like this approach because the configuration works the same way in development and production, you just change the value of the port if necessary.

declan
  • 5,605
  • 3
  • 39
  • 43
4

Inspired by Radek and Spencer... On Rails 4(.0.2 - Ruby 2.1.0 ), I was able to append this to config/boot.rb:

# config/boot.rb

# ...existing code

require 'rails/commands/server'

module Rails
  # Override default development
  # Server port
  class Server
    def default_options
      super.merge(Port: 3100)
    end
  end
end

All other configuration in default_options are still set, and command-line switches still override defaults.

TuK
  • 3,556
  • 4
  • 24
  • 24
3

Solution for Rails 2.3 - script/server:

#!/usr/bin/env ruby
require 'rack/handler'
module Rack::Handler
  class << WEBrick
    alias_method :old_run, :run
  end

  class WEBrick
    def self.run(app, options={})
      options[:Port] = 3010 if options[:Port] == 3000
      old_run(app, options)
    end
  end
end

require File.dirname(__FILE__) + '/../config/boot'
require 'commands/server'
Nowaker
  • 12,154
  • 4
  • 56
  • 62
3

If you're using puma (I'm using this on Rails 6+):

To change default port for all environments:

The "{3000}" part sets the default port if undefined in ENV.

~/config/puma.rb

change:
    port ENV.fetch('PORT') { 3000 }
for:
    port ENV.fetch('PORT') { 10524 }

To define it depending on the environment, using Figaro gem for credentials/environment variable:

~/application.yml
local_db_username: your_user_name
​local_db_password: your_password
PORT: 10524

You can adapt this to you own environment variable manager.

1

You could install $ gem install foreman, and use foreman to start your server as defined in your Procfile like:

web: bundle exec rails -p 10524

You can check foreman gem docs here: https://github.com/ddollar/foreman for more info

The benefit of this approach is not only can you set/change the port in the config easily and that it doesn't require much code to be added but also you can add different steps in the Procfile that foreman will run for you so you don't have to go though them each time you want to start you application something like:

bundle: bundle install
web: bundle exec rails -p 10524
...
...

Cheers

Mshka
  • 1,798
  • 1
  • 10
  • 19
1

For ruby > 3 and For rails > 7

in file app/config/puma.rb, update the port number.

port ENV.fetch("PORT") { 3200 }
vidur punj
  • 5,019
  • 4
  • 46
  • 65