# Specify port when using ray.init() to start new local instance

**URL:** <https://discuss.ray.io/t/specify-port-when-using-ray-init-to-start-new-local-instance/21906>\
**Category:** Ray Clusters\
**Created:** [February 24, 2025, 7:36pm UTC](https://discuss.ray.io/t/specify-port-when-using-ray-init-to-start-new-local-instance/21906 "2025-02-24T19:36:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![egorbn](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/egorbn/32/7709_2.png) [@egorbn](https://discuss.ray.io/u/egorbn)\
**Post date:** [February 24, 2025, 7:36pm UTC](https://discuss.ray.io/t/specify-port-when-using-ray-init-to-start-new-local-instance/21906/1 "2025-02-24T19:36:04Z")

</div>

**How severe does this issue affect your experience of using Ray?**

- High: It blocks me to complete my task.

In my service code, I am connecting to an existing ray cluster.

In local tests, I’m using `ray.init()` to start a local instance in a fixture and pass this as the head endpoint to the service code.

The problem is that using `ray.init()` to start a local instance opens a random port, so the endpoint always changes.

I do not see any options to specify the port for a new local instance.

Right now I am doing the following in a test fixture:

```auto
ray.init(local_mode=True, ignore_reinit_error=True, num_cpus=1)

```

But this fails because the service `ray.init()` tries to connect on a ray endpoint that is specified in the environment, does not find the local instance because the local port doesn’t match the environment, and times out.

So I want to do something like this in a test fixture:

```auto
RAY_PORT=10001 # Specified in the environment
ray.init(local_mode=True, ignore_reinit_error=True, num_cpus=1, port=RAY_PORT)

```

The goal is to then be able to connect on `f"ray://127.0.0.1:{RAY_PORT}"` using `ray.init()` in the code.

Thanks!

---

<div class="post-metadata">

**Author:** ![christina](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/christina/32/7542_2.png) [@christina](https://discuss.ray.io/u/christina)\
**Post date:** [February 24, 2025, 10:40pm UTC](https://discuss.ray.io/t/specify-port-when-using-ray-init-to-start-new-local-instance/21906/2 "2025-02-24T22:40:05Z")

</div>

Hello! What version of Ray are you using? `local_mode` seems to be [deprecated](https://docs.ray.io/en/latest/ray-core/api/doc/ray.init.html#ray-init) as of the latest version of Ray. `local_mode` also doesn’t really set up a Ray cluster (like you may have noticed) since it is more of a ephemeral process used for debugging.

Have you tried using the regular `ray.init` (docs here: [ray.init — Ray 2.42.1](https://docs.ray.io/en/latest/ray-core/api/doc/ray.init.html)) and setting variable `dashboard_port` to a specific port?

Here’s a guide on how to customize the dashboard port using `ray.init()` if it helps:

[https://docs.ray.io/en/latest/cluster/configure-manage-dashboard.html](https://docs.ray.io/en/latest/cluster/configure-manage-dashboard.html)

---

<div class="post-metadata">

**Author:** ![egorbn](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/egorbn/32/7709_2.png) [@egorbn](https://discuss.ray.io/u/egorbn)\
**Post date:** [February 24, 2025, 11:14pm UTC](https://discuss.ray.io/t/specify-port-when-using-ray-init-to-start-new-local-instance/21906/3 "2025-02-24T23:14:52Z")

</div>

Thanks, @christina!

I’m using `ray` 2.34.0

The dashboard port isn’t the same as the head node port, which is what needs to be passed to `ray.init(address=xxxx)` in order to connect to an existing cluster. Setting the dashboard port doesn’t impact the issue I am describing.

---

<div class="post-metadata">

**Author:** ![christina](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/christina/32/7542_2.png) [@christina](https://discuss.ray.io/u/christina)\
**Post date:** [February 24, 2025, 11:34pm UTC](https://discuss.ray.io/t/specify-port-when-using-ray-init-to-start-new-local-instance/21906/4 "2025-02-24T23:34:31Z")

</div>

Oh, I see what you mean! Ok, so this is a way to start Ray with a port via the CLI (`ray start --head --port=6379`) [Starting Ray — Ray 2.42.1](https://docs.ray.io/en/latest/ray-core/starting-ray.html#starting-ray-via-the-cli-ray-start)

Having a stable port for every launch might help with the issues you’re facing. But since you’re doing it in a testing environment I think doing it in Python will be better than the CLI. The general idea is the same as the one above. I think the equivalent in Python code will be `ray.init(address="127.0.0.1:6379") `, or something like this in your test suite:

```auto
  ray.init(
      address=127.0.0.1:6379, ...
  )

```

I think either one will work in setting the port, lmk if this helps in your tests.

---

<div class="post-metadata">

**Author:** ![egorbn](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/egorbn/32/7709_2.png) [@egorbn](https://discuss.ray.io/u/egorbn)\
**Post date:** [February 25, 2025, 12:04am UTC](https://discuss.ray.io/t/specify-port-when-using-ray-init-to-start-new-local-instance/21906/5 "2025-02-25T00:04:45Z")

</div>

Thanks, @christina, but this won’t work because if an address is specified, `ray.init()` will try to connect to it and not create an instance.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/ray/original/2X/b/b652d2cbfd68a213169028228aaa2da11bc40966.png)

According to the [docs](https://docs.ray.io/en/latest/ray-core/api/doc/ray.init.html), the only way to have `ray.init()` create a new local instance is not to specify an address.

And yes, I cannot use the cli because the init needs to happen in the test only.

---

<div class="post-metadata">

**Author:** ![egorbn](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/egorbn/32/7709_2.png) [@egorbn](https://discuss.ray.io/u/egorbn)\
**Post date:** [February 25, 2025, 12:50am UTC](https://discuss.ray.io/t/specify-port-when-using-ray-init-to-start-new-local-instance/21906/6 "2025-02-25T00:50:19Z")

</div>

FYI I resolved this for now by using the `address="auto"` argument in `ray.init()`

```auto
# project/utils.py
def is_ray_local():
    """Logic to determine whether you are running locally or in a deployment"""
    return True

# project/service.py
import ray

from project.utils import is_ray_local

RAY_LOCAL_FLAG = is_ray_local()
RAY_CLUSTER_ENDPOINT_ACTUAL = "ray://1.1.1.1:11111"

def get_ray_enpoint()
    if RAY_LOCAL_FLAG:
        return "auto"
    return RAY_CLUSTER_ENDPOINT_ACTUAL

def foo_service():
    ray.init(address=get_ray_enpoint())
    # Do stuff
    return "Success"

# tests/test.py
import ray

from project.service import foo_service

def test_foo_service():
    ray.init()

    status = foo_service()

    assert status == "Success"

```

---

<div class="post-metadata">

**Author:** ![christina](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/christina/32/7542_2.png) [@christina](https://discuss.ray.io/u/christina)\
**Post date:** [February 25, 2025, 1:01am UTC](https://discuss.ray.io/t/specify-port-when-using-ray-init-to-start-new-local-instance/21906/7 "2025-02-25T01:01:12Z")

</div>

Ok! Thanks for the update 🙂 I read the docs for a bit and here’s just a small update with what I found out so far. I think your workaround works well, too. ^^ You can also set the environment variable `RAY_ADDRESS` and then `ray.init()` will [automatically use the environment variable](https://docs.ray.io/en/latest/serve/api/index.html#advanced-apis) that you set previously automatically. I think this should work even if you are running test suites. Apologies, I wasn’t much help, but please feel free to reach out anytime!
