Skip to content

Instantly share code, notes, and snippets.

@Fortyseven
Created April 26, 2024 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fortyseven/d107dfe2532c9b7b8faf0866bff586f1 to your computer and use it in GitHub Desktop.
Save Fortyseven/d107dfe2532c9b7b8faf0866bff586f1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
This script will download the latest release of ollama from the github
releases page. You can use it for any project that has just one binary
file in the release. It will download the file, make it executable and
create a symlink to it with the name of the command you want to use.
This is intended to keep a reliable link to the latest version of the
binary, so you can use it in your path, scripts or other automation
without juggling with version numbers.
You can do this by hand. But after the 10th time, welcome to
Automationville.
"""
import os
from bs4 import BeautifulSoup
import requests
import urllib
from tqdm import tqdm
BASE_URL = "https://github.com/ollama/ollama/releases/"
BASE_FILE = "ollama-linux-amd64"
COMMAND = "ollama"
# https://stackoverflow.com/a/53877507
class DownloadProgressBar(tqdm):
def update_to(self, b=1, bsize=1, tsize=None):
if tsize is not None:
self.total = tsize
self.update(b * bsize - self.n)
def getURLtoFile(url, output_path):
with DownloadProgressBar(
unit="B", unit_scale=True, miniters=1, desc=url.split("/")[-1]
) as t:
urllib.request.urlretrieve(url, filename=output_path, reporthook=t.update_to)
def main():
redirected_url = requests.get(f"{BASE_URL}/latest").url
print(f"- Found: {redirected_url}")
# extract version number end of from resolved url
# (e.g. github.com/ollama/ollama/releases/tag/v0.1.30 becomes v0.1.30)
release_version = redirected_url.split("/")[-1]
print(f"- Latest version: {release_version}")
target_file = f"{BASE_FILE}-{release_version}"
if os.path.exists(target_file):
print(f"- Already latest, aborting: {target_file}")
exit(0)
download_url = f"{BASE_URL}/download/{release_version}/{BASE_FILE}"
print(f"- Downloading: {download_url}")
getURLtoFile(
f"{BASE_URL}/download/{release_version}/{BASE_FILE}",
target_file,
)
print(f"- Wrote: {BASE_FILE}-{release_version}")
# update permissions +x
os.chmod(target_file, 0o755)
# update symlink 'ollama' to point to the latest version
if os.path.exists(COMMAND):
os.remove(COMMAND)
os.symlink(target_file, COMMAND)
print(f"- Updated symlink: {COMMAND} -> {target_file}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment