# Starting Ray Serve from Python

**URL:** <https://discuss.ray.io/t/starting-ray-serve-from-python/4646>\
**Category:** Ray Serve\
**Created:** [January 10, 2022, 4:46pm UTC](https://discuss.ray.io/t/starting-ray-serve-from-python/4646 "2022-01-10T16:46:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henry\_Thornton](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@Henry\_Thornton](https://discuss.ray.io/u/Henry_Thornton)\
**Post date:** [January 10, 2022, 4:46pm UTC](https://discuss.ray.io/t/starting-ray-serve-from-python/4646/1 "2022-01-10T16:46:45Z")

</div>

```auto
ray start --head# Start local Ray cluster.
serve start # Start Serve on the local Ray cluster.

```

Is it possible to start Ray and Ray Serve from a Python program by calling the above two commands using say, the subprocess command?

---

<div class="post-metadata">

**Author:** ![shrekris](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@shrekris](https://discuss.ray.io/u/shrekris)\
**Post date:** [January 10, 2022, 6:14pm UTC](https://discuss.ray.io/t/starting-ray-serve-from-python/4646/2 "2022-01-10T18:14:55Z")

</div>

I was able to use the [subprocess.run()](https://docs.python.org/3/library/subprocess.html#subprocess.run) command within a Python script to run `ray start --head` and then `serve start` locally. It launched a local Ray cluster and Serve instance, and I was able to add deployments to it.

Script to start Ray and Serve, and then launch deployments:

```auto
import subprocess

subprocess.run(["ray", "start", "--head"])
subprocess.run(["serve", "start"])

import ray
from ray import serve

ray.init(address="auto", namespace="serve")
serve.start()

@serve.deployment
def f():
    pass

# Deploy f to Serve instance started by subprocess
f.deploy() # Stays deployed even after script exits

```

Script to check the deployment:

```auto
import ray
from ray import serve

ray.init(address="auto", namespace="serve")
print(serve.list_deployments())

```

---

<div class="post-metadata">

**Author:** ![Henry\_Thornton](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@Henry\_Thornton](https://discuss.ray.io/u/Henry_Thornton)\
**Post date:** [January 10, 2022, 7:51pm UTC](https://discuss.ray.io/t/starting-ray-serve-from-python/4646/3 "2022-01-10T19:51:01Z")

</div>

Thank-you for the confirmation and the example code. Is the  
`serve.start()`  
line needed?

---

<div class="post-metadata">

**Author:** ![shrekris](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@shrekris](https://discuss.ray.io/u/shrekris)\
**Post date:** [January 10, 2022, 7:58pm UTC](https://discuss.ray.io/t/starting-ray-serve-from-python/4646/4 "2022-01-10T19:58:41Z")

</div>

It doesn’t look like it is. I ran the first script without `serve.start()`, and it still worked.
