Running Ollama Server Publicly On Ubuntu: A Step-by-Step Guide

Aug 17, 2024    |   

If you're looking to make your Ollama server publicly accessible, you've come to the right place. This detailed guide will walk you through the steps I took to resolve issues encountered while following a specific article (https://github.com/ollama/ollama/blob/main/docs/faq.md). While the article provided a general direction, we'll delve into the exact commands and configurations that worked for me.

Install Ollama on Linux

On Linux, re-run the install script:

curl -fsSL https://ollama.com/install.sh | sh
Setting environment variables on Linux

If Ollama is run as a systemd service, environment variables should be set using systemctl:

Edit the systemd service by calling systemctl edit ollama.service. This will open an editor.

For each environment variable, add a line Environment under section [Service]:

[Service]
Environment="OLLAMA_HOST=0.0.0.0"

Save and exit.

Reload systemd and restart Ollama:

systemctl daemon-reload
systemctl restart ollama


If the aforementioned solution doesn't resolve your issue, as was the case for me, consider trying the alternative approach outlined below.

Step 1: Create the Configuration Directory

The first step is to ensure that there's a dedicated directory for overriding the Ollama service settings. This is where we'll place our custom configuration file.

Open your terminal.
Run the following command to create the directory:

sudo mkdir -p /etc/systemd/system/ollama.service.d/

Step 2: Edit the Override Configuration File

Now that we have the directory, we need to create and edit the override.conf file within it. This file allows us to specify additional configurations for the Ollama service.

Use your preferred text editor to create and open the override.conf file. In this example, I'll use nano:

sudo nano /etc/systemd/system/ollama.service.d/override.conf

Step 3: Add Environment Variable to the Configuration File

In the override.conf file, we need to specify the environment variable that tells Ollama to listen on all network interfaces, not just the local one.

Inside the override.conf file, add the following lines:

[Service]
Environment="OLLAMA_HOST=0.0.0.0"

This configuration sets the OLLAMA_HOST environment variable to 0.0.0.0, which is the IP address that represents all available network interfaces.

Save the file and exit the editor. If you're using nano, press Ctrl+O to save, then Ctrl+X to exit.

Step 4: Restart the Ollama Service

With the configuration in place, the final step is to restart the Ollama service for the changes to take effect.

Run the following commands to reload the systemd manager configuration and restart the Ollama service:

sudo systemctl daemon-reload
sudo systemctl restart ollama


Verifying the Changes

After completing these steps, it's important to verify that the Ollama server is now listening on the public IP address and is accessible from outside your local network.

Check the status of the Ollama service to ensure it's running correctly:

sudo systemctl status ollama

Use a tool like netstat or ss to confirm that the service is listening on 0.0.0.0 for the desired port (default is 11434):

sudo ss -tulnp | grep :11434

If you can see the Ollama service listed with 0.0.0.0:11434, it means the server is now publicly accessible.

Conclusion

By following these steps, you've successfully configured your Ollama server to be publicly accessible. This allows clients from different networks to connect to your server, expanding its utility and reach. Remember to always consider the security implications of making services publicly available and take necessary precautions, such as using firewalls and secure access methods.

Happy coding, and may your Ollama server run smoothly!