Repository navigation
Consolidate dual venvs and separate install from update - #4530
Conversation
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request streamlines the Unsloth Studio installation and update process by consolidating virtual environments, separating concerns between installation and updates, and introducing flags for local development and custom package testing. It also enhances the CLI experience and fixes a frontend path resolution issue, making the setup more robust and user-friendly. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
The pull request refactors the Unsloth Studio installation and update scripts across Windows (PowerShell) and Linux/macOS (Bash) to standardize the virtual environment location under ~/.unsloth/studio/unsloth_studio and improve the installation workflow. Key changes include introducing new command-line flags (--local, --package) for install.sh to support local repository installations and custom package names, updating Python dependency installation logic in studio/install_python_stack.py to handle these new modes and conditionally skip base package reinstallation, and refining studio/setup.sh to remove redundant Python discovery and add dependency update checks. The unsloth_cli is also updated to reflect the new venv path and introduce an update command. Review comments highlight an issue in install.sh where the --package flag's argument parsing is not robust, potentially leading to silent failures if no value is provided, and a redundant variable redefinition in install.ps1 that could be removed for clarity.
I am having trouble creating individual review comments. Click here to see my feedback.
install.sh (13-23)
The argument parsing for --package is not fully robust. If --package is provided as the last argument without a value, the script will not fail and will proceed with the default PACKAGE_NAME. This could lead to unexpected behavior. You should add a check after the loop to ensure that if _next_is_package is true, the script exits with an error.
for arg in "$@"; do
if [ "$_next_is_package" = true ]; then
PACKAGE_NAME="$arg"
_next_is_package=false
continue
fi
case "$arg" in
--local) STUDIO_LOCAL_INSTALL=true ;;
--package) _next_is_package=true ;;
esac
done
if [ "$_next_is_package" = true ]; then
echo "❌ ERROR: --package requires an argument." >&2
exit 1
fi
install.ps1 (201)
The variable $UnslothExe is being redefined here. It was already defined on line 175. This redefinition is redundant and can be removed for better code clarity and maintainability.
…e with PyPI version check)
|
I tested the local package install method on AWS B20O instance (linux) fresh install and it works |
danielhanchen
left a comment
There was a problem hiding this comment.
Thank you for the PR! The goal of this PR is to consolidate the dual Studio venvs into a single ~/.unsloth/studio/unsloth_studio, separate install from update concerns, and add developer testing flags (--local/--package). As a summary, this PR rewrites the install/update flow so that install.sh creates the venv and setup.sh only updates an existing one, adds unsloth studio update CLI command, a global symlink, and PyPI version-check optimization.
| Reviewers | Severity | Finding |
|---|---|---|
| 7/8 | High | setup.sh deletes legacy ~/.unsloth/studio/.venv before checking if new unsloth_studio exists -- destroys working installs during upgrade |
| 7/8 | High | setup.sh hard-requires ~/.unsloth/studio/unsloth_studio -- breaks documented editable/nightly flows that use activated developer venvs |
| 6/8 | Medium | env $_SETUP_ENV bash "$SETUP_SH" is unquoted word-splitting -- fragile with spaces in PACKAGE_NAME |
| 5/8 | Medium | PyPI version check skips ALL Python dep repair -- won't fix partially broken env or missing .venv_t5/ |
| 5/8 | Medium | install.sh unconditionally destroys existing venv on every run (regression from old skip-if-valid behavior) |
| 4/8 | Medium | update() doesn't explicitly set/clear STUDIO_PACKAGE_NAME for default -- stale env vars can be inherited |
| 4/8 | Medium | --local path uses SCRIPT_DIR.parent as repo root -- wrong from site-packages; also unsloth-zoo missing in --local/--package installs |
| 4/8 | Low | [ -t 0 ] auto-launch checks stdin (pipe for curl | sh) -- auto-launch never triggers in primary install flow |
| 3/8 | Low | Banner "Setup Complete!" is 1 char shorter than "Update Complete!" causing box misalignment |
| 3/8 | Low | setup() CLI command is now identical to update() with defaults -- confusing |
Concrete suggestions for each finding below.
| [ -d "$REPO_ROOT/.venv_overlay" ] && rm -rf "$REPO_ROOT/.venv_overlay" | ||
| [ -d "$REPO_ROOT/.venv_t5" ] && rm -rf "$REPO_ROOT/.venv_t5" | ||
| # Clean up old .venv path (replaced by unsloth_studio) | ||
| [ -d "$STUDIO_HOME/.venv" ] && rm -rf "$STUDIO_HOME/.venv" |
There was a problem hiding this comment.
[7/8 reviewers] High: This line deletes the existing legacy ~/.unsloth/studio/.venv before checking whether the new unsloth_studio venv exists. On an upgrade from current main, existing users have ~/.unsloth/studio/.venv as their only working environment. This script removes it unconditionally and then errors out at line 225 with "Run install.sh first", leaving the user with no working Studio venv at all.
The fix is to migrate the legacy venv instead of deleting it:
| [ -d "$STUDIO_HOME/.venv" ] && rm -rf "$STUDIO_HOME/.venv" | |
| # Migrate old .venv path to unsloth_studio (if new path doesn't exist yet) | |
| if [ ! -x "$VENV_DIR/bin/python" ] && [ -x "$STUDIO_HOME/.venv/bin/python" ]; then | |
| echo "==> Migrating legacy Studio environment to $VENV_DIR" | |
| mv "$STUDIO_HOME/.venv" "$VENV_DIR" | |
| fi | |
| # Clean up old .venv path only after migration succeeded | |
| [ -d "$STUDIO_HOME/.venv" ] && rm -rf "$STUDIO_HOME/.venv" |
There was a problem hiding this comment.
If someone has .venv, they're on the old version with the old dual-venv flow and likely outdated/stale dependencies. Migrating it would just carry forward old problems into the new setup. The intended upgrade path is to run install.sh which creates a fresh unsloth_studio from scratch — that's the whole point of separating install from update. The error message already tells them to run install.sh. We could improve the message to mention the environment location changed, but migrating the old venv defeats the purpose of this refactor. please advise
|
|
||
| if [ -n "$INSTALLED_VER" ] && [ -n "$LATEST_VER" ] && [ "$INSTALLED_VER" = "$LATEST_VER" ]; then | ||
| echo "✅ $_PKG_NAME $INSTALLED_VER is up to date (matches PyPI latest)" | ||
| _SKIP_PYTHON_DEPS=true |
There was a problem hiding this comment.
[5/8 reviewers] Medium: Version string equality check skips ALL Python dependency work (including install_python_stack, .venv_t5/ pre-install, and all extras). If the env is partially broken (e.g., unsloth is current but unsloth-zoo, transformers, or .venv_t5/ are missing/corrupt), unsloth studio update will not repair it.
Consider adding a health check alongside the version check:
| _SKIP_PYTHON_DEPS=true | |
| if [ -n "$INSTALLED_VER" ] && [ -n "$LATEST_VER" ] && [ "$INSTALLED_VER" = "$LATEST_VER" ] \ | |
| && [ -d "$VENV_T5_DIR" ] \ | |
| && "$VENV_DIR/bin/python" -c "import fastapi; import importlib.metadata as m; m.version('unsloth_zoo')" >/dev/null 2>&1; then | |
| echo "\u2705 $_PKG_NAME $INSTALLED_VER is up to date (matches PyPI latest)" | |
| _SKIP_PYTHON_DEPS=true |
There was a problem hiding this comment.
The time required to health-check every dependency (and we have a lot of them) would be close to or exceed the time to just reinstall the whole environment. If someone has a broken env, the intended path is to re-run install.sh which nukes and rebuilds from scratch. That's the whole point of separating install (fresh start) from update (quick version bump).
- Broken env? →
./install.sh - Want latest version? →
unsloth studio update
Adding a health check to the update path would defeat the purpose of making it fast.
|
Tested on Linux (WSL): All behaved as expected. Confirmed |
…IO_BASE for all paths
…lls in setup.sh" This reverts commit 301291d.
…ery message on launch failure
…n to preserve torch
|
Without this, Also line 72 still references the deprecated command: |
…udio.py install.ps1 was creating the venv CWD-relative ($VenvName = "unsloth_studio"), setup.ps1 was using an absolute path to ".unsloth\studio\.venv", and studio.py looks for ".unsloth\studio\unsloth_studio". All three paths were different, so the Windows installer would never produce a working Studio setup. install.ps1: - Use absolute $StudioHome + $VenvDir matching the Linux install.sh layout - Add 3-way migration: old .venv at STUDIO_HOME, CWD-relative ~/unsloth_studio from the previous install.ps1, or fresh creation with torch validation - For migrated envs, upgrade unsloth while preserving existing torch/CUDA wheels - Set SKIP_STUDIO_BASE=1 before calling setup.ps1 (matches install.sh behavior) - Fix launch instructions to use the absolute venv path setup.ps1: - Change $VenvDir from ".unsloth\studio\.venv" to ".unsloth\studio\unsloth_studio" - Add SKIP_STUDIO_BASE guard: error out if venv is missing when called from install.ps1 (which should have already created it) - Differentiate "Setup" vs "Update" in banners based on SKIP_STUDIO_BASE
setup.sh always errors out if the venv does not exist (line 224-228), telling the user to run install.sh first. setup.ps1 was conditionally creating a bare venv with python -m venv when SKIP_STUDIO_BASE was not set, which would produce an empty venv with no torch or unsloth. Now setup.ps1 matches setup.sh: always error, always point to install.ps1.
…dows On CPU-only machines, `uv pip install unsloth --torch-backend=auto` falls back to unsloth==2024.8 because the CPU solver cannot satisfy newer unsloth's dependencies. install.ps1 already solved this with a two-step approach; this applies the same fix to install.sh and install_python_stack.py. install.sh: add get_torch_index_url() that detects GPU via nvidia-smi and maps CUDA versions to PyTorch index URLs (matching install.ps1's Get-TorchIndexUrl). Fresh installs now install torch first via explicit --index-url, then install unsloth with --upgrade-package to preserve the pre-installed torch. All 5 --torch-backend=auto removed from primary paths. install.ps1: add fallback else-branch when TorchIndexUrl is empty, using --torch-backend=auto as last resort (matching install.sh). install_python_stack.py: remove unconditional --torch-backend=auto from _build_uv_cmd. Torch is pre-installed by install.sh/setup.ps1 by the time this runs. Callers that need it can set UV_TORCH_BACKEND. Both install.sh and install.ps1 now share the same three-branch logic: migrated env (upgrade-package only), normal (torch-first + index-url), and fallback (--torch-backend=auto if URL detection fails).
For migrated environments (moved from legacy venv location), --reinstall-package is better than --upgrade-package because it forces a clean reinstall even if the same version is already installed. This ensures proper .dist-info and .pyc state in the new venv location. --upgrade-package remains correct for the fresh install path where torch is already installed and we just want to add unsloth without re-resolving torch.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request significantly refactors the Unsloth Studio installation and update process for both Windows (PowerShell) and Linux/macOS (Bash). The virtual environment path is standardized to ~/.unsloth/studio/unsloth_studio, with robust migration logic implemented for existing installations. The install scripts are now responsible for creating the virtual environment and initial package installations, including intelligent GPU detection for PyTorch. The setup scripts (invoked via unsloth studio update) now focus on updating existing packages and system-level setup. The unsloth ui command has been removed, consolidating functionality under unsloth studio, and a new unsloth studio update command is introduced for flexible dependency updates. New cross-platform tests ensure the consistency and correctness of the installation logic. I have no feedback to provide.
- Replace grep -oP (GNU Perl regex) with POSIX sed in get_torch_index_url() so the script works on BSD grep (macOS is already guarded by the Darwin early-return, but Alpine/BusyBox would silently get the wrong CUDA tag) - Add LC_ALL=C before nvidia-smi invocation to prevent locale-dependent output parsing issues - Add warning on stderr when nvidia-smi output is unparseable, matching install.ps1's [WARN] message - Add explicit unsloth-zoo positional arg to install.ps1 migrated path, matching install.sh (--reinstall-package alone won't install it if it was never present in the migrated env) - Fix stale comment in install_python_stack.py line 392 that still claimed --torch-backend=auto is added by _build_uv_cmd - Add sed to test tools directory (function now uses sed instead of grep)
The migrated path runs uv pip install with --reinstall-package for unsloth/unsloth-zoo. While uv should keep existing torch as satisfied, the resolver could still re-resolve torch as a transitive dependency. Without --index-url pointing at the correct CUDA wheel index, the resolver would fall back to plain PyPI and potentially pull CPU-only torch. Adding --index-url $TORCH_INDEX_URL ensures CUDA wheels are available if the resolver needs them. Applied to both install.sh and install.ps1.
The original install.ps1 on main already handles the migrated path without --index-url and it works correctly. --reinstall-package only forces reinstall of the named packages while uv keeps existing torch as satisfied. No need for the extra flag.
studio.py sets STUDIO_LOCAL_REPO when --local is passed, but install_python_stack.py never read it. The update path always installed from PyPI regardless of the --local flag. Add a local_repo branch that first updates deps from base.txt (with --upgrade-package to preserve torch), then overlays the local checkout as an editable install with --no-deps.
* refactor: consolidate dual venvs into single ~/.unsloth/studio/unsloth_studio * refactor: separate install.sh (first-time) from setup.sh (smart update with PyPI version check) * fix: install.sh calls setup.sh directly, keep both setup and update CLI commands * fix: use importlib.resources.files() directly without _path attribute * fix: bootstrap uv before pip upgrade to handle uv venvs without pip * fix: frontend 404 when launched via CLI, add global symlink to ~/.local/bin * feat: add --local flag to install.sh and unsloth studio update for branch testing * fix: resolve repo root from script location for --local installs * feat: add --package flag to install.sh for testing with custom package names * feat: add --package flag to unsloth studio update * fix: always nuke venv in install.sh for clean installs * revert: remove Windows changes, will handle in separate PR * fix: error when --package is passed without an argument * revert: restore Windows scripts to current main * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix: always explicitly set STUDIO_LOCAL_INSTALL and STUDIO_PACKAGE_NAME env vars * fix: pass explicit STUDIO_LOCAL_REPO env var for --local installs * fix: align banner box for Setup vs Update labels * deprecate: hide 'unsloth studio setup' command, point users to update/install.sh * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix: check stdout not stdin for auto-launch detection (curl pipe fix) * fix: update install URL to unsloth.ai/install.sh * fix: update install.sh usage comments to unsloth.ai/install.sh * fix: use --upgrade-package for base deps to preserve existing torch/CUDA installs * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix: --local install now also installs unsloth-zoo via base.txt before editable overlay * fix: don't skip base packages for --local installs (editable needs unsloth-zoo) * refactor: move --local full dep install to install.sh, keep SKIP_STUDIO_BASE for all paths * feat: add migration support for old .venv and CWD-based installs in setup.sh * Revert "feat: add migration support for old .venv and CWD-based installs in setup.sh" This reverts commit 301291d. * feat: migrate old .venv layout in install.sh instead of always nuking * feat: validate old .venv with torch CUDA test before migration, recovery message on launch failure * fix: try CUDA then fall back to CPU for migration validation * fix: upgrade unsloth/unsloth-zoo with --reinstall-package on migration to preserve torch * remove: delete unused unsloth ui command (use unsloth studio instead) * Fix Windows venv path mismatch between install.ps1, setup.ps1, and studio.py install.ps1 was creating the venv CWD-relative ($VenvName = "unsloth_studio"), setup.ps1 was using an absolute path to ".unsloth\studio\.venv", and studio.py looks for ".unsloth\studio\unsloth_studio". All three paths were different, so the Windows installer would never produce a working Studio setup. install.ps1: - Use absolute $StudioHome + $VenvDir matching the Linux install.sh layout - Add 3-way migration: old .venv at STUDIO_HOME, CWD-relative ~/unsloth_studio from the previous install.ps1, or fresh creation with torch validation - For migrated envs, upgrade unsloth while preserving existing torch/CUDA wheels - Set SKIP_STUDIO_BASE=1 before calling setup.ps1 (matches install.sh behavior) - Fix launch instructions to use the absolute venv path setup.ps1: - Change $VenvDir from ".unsloth\studio\.venv" to ".unsloth\studio\unsloth_studio" - Add SKIP_STUDIO_BASE guard: error out if venv is missing when called from install.ps1 (which should have already created it) - Differentiate "Setup" vs "Update" in banners based on SKIP_STUDIO_BASE * setup.ps1: unconditionally error if venv missing, matching setup.sh setup.sh always errors out if the venv does not exist (line 224-228), telling the user to run install.sh first. setup.ps1 was conditionally creating a bare venv with python -m venv when SKIP_STUDIO_BASE was not set, which would produce an empty venv with no torch or unsloth. Now setup.ps1 matches setup.sh: always error, always point to install.ps1. * Fix --torch-backend=auto CPU solver dead-end on Linux, macOS, and Windows On CPU-only machines, `uv pip install unsloth --torch-backend=auto` falls back to unsloth==2024.8 because the CPU solver cannot satisfy newer unsloth's dependencies. install.ps1 already solved this with a two-step approach; this applies the same fix to install.sh and install_python_stack.py. install.sh: add get_torch_index_url() that detects GPU via nvidia-smi and maps CUDA versions to PyTorch index URLs (matching install.ps1's Get-TorchIndexUrl). Fresh installs now install torch first via explicit --index-url, then install unsloth with --upgrade-package to preserve the pre-installed torch. All 5 --torch-backend=auto removed from primary paths. install.ps1: add fallback else-branch when TorchIndexUrl is empty, using --torch-backend=auto as last resort (matching install.sh). install_python_stack.py: remove unconditional --torch-backend=auto from _build_uv_cmd. Torch is pre-installed by install.sh/setup.ps1 by the time this runs. Callers that need it can set UV_TORCH_BACKEND. Both install.sh and install.ps1 now share the same three-branch logic: migrated env (upgrade-package only), normal (torch-first + index-url), and fallback (--torch-backend=auto if URL detection fails). * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Use --reinstall-package for migrated envs on both Linux and Windows For migrated environments (moved from legacy venv location), --reinstall-package is better than --upgrade-package because it forces a clean reinstall even if the same version is already installed. This ensures proper .dist-info and .pyc state in the new venv location. --upgrade-package remains correct for the fresh install path where torch is already installed and we just want to add unsloth without re-resolving torch. * Address review findings: portability, parity, and stale comments - Replace grep -oP (GNU Perl regex) with POSIX sed in get_torch_index_url() so the script works on BSD grep (macOS is already guarded by the Darwin early-return, but Alpine/BusyBox would silently get the wrong CUDA tag) - Add LC_ALL=C before nvidia-smi invocation to prevent locale-dependent output parsing issues - Add warning on stderr when nvidia-smi output is unparseable, matching install.ps1's [WARN] message - Add explicit unsloth-zoo positional arg to install.ps1 migrated path, matching install.sh (--reinstall-package alone won't install it if it was never present in the migrated env) - Fix stale comment in install_python_stack.py line 392 that still claimed --torch-backend=auto is added by _build_uv_cmd - Add sed to test tools directory (function now uses sed instead of grep) * Add --index-url to migrated env path to prevent CPU torch resolution The migrated path runs uv pip install with --reinstall-package for unsloth/unsloth-zoo. While uv should keep existing torch as satisfied, the resolver could still re-resolve torch as a transitive dependency. Without --index-url pointing at the correct CUDA wheel index, the resolver would fall back to plain PyPI and potentially pull CPU-only torch. Adding --index-url $TORCH_INDEX_URL ensures CUDA wheels are available if the resolver needs them. Applied to both install.sh and install.ps1. * Revert --index-url on migrated env path The original install.ps1 on main already handles the migrated path without --index-url and it works correctly. --reinstall-package only forces reinstall of the named packages while uv keeps existing torch as satisfied. No need for the extra flag. * Fix unsloth studio update --local not installing local checkout studio.py sets STUDIO_LOCAL_REPO when --local is passed, but install_python_stack.py never read it. The update path always installed from PyPI regardless of the --local flag. Add a local_repo branch that first updates deps from base.txt (with --upgrade-package to preserve torch), then overlays the local checkout as an editable install with --no-deps. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel Han <[email protected]>
Summary
~/.unsloth/studio/unsloth_studio— previouslyinstall.shcreated one venv andsetup.shnuked it and created a separate.venv, resulting in duplicate environment storage spaceinstall.shhandles first-time setup (system deps, uv, venv creation, unsloth install),setup.shhandles updates (frontend build, python deps, llama.cpp)setup.shchecks installed version against PyPI and skips Python dep reinstallation when already up to date--localflag toinstall.shandunsloth studio updatefor editable installs from a local repo checkout (no wheel build needed for testing)--packageflag toinstall.shandunsloth studio updatefor testing with custom package names (e.g. test PyPI uploads)unsloth studio updatecommand alongside existingsetupcommandunslothsymlink to~/.local/binand adds~/.local/binto the user's shell profile (~/.bashrc,~/.zshrc, or~/.profile) if not already onPATH, so the CLI works without activating the venvinstall.ps1,setup.ps1) will be handled in a separate PRArchitecture
install.sh (used for install)
setup.sh (used for install and update operations)
Flows
curl ... | sh— creates venv, installs unsloth once, setup skips base, builds frontend/llama.cpp, launchesunsloth studio update— reuses unsloth_studio, reinstalls base.txt (picks up new unsloth version), rebuilds frontend if neededunsloth studio updatewith no unsloth_studio — errors with "Run install.sh first"unsloth studio— launches correctly from existing venvFiles changed
install.sh--local/--packageflags, calls setup.sh directly, symlinks to ~/.local/binstudio/setup.shstudio/install_python_stack.pySKIP_STUDIO_BASE/STUDIO_LOCAL_INSTALL/STUDIO_PACKAGE_NAMEenv vars, uv bootstrap before pipstudio/backend/colab.py.venv→unsloth_studiounsloth_cli/commands/studio.pyupdatecommand with--local/--package, frontend path resolution fixTesting
The
--localand--packageflags were added to allow testing this branch before a PyPI release../install.sh --localunsloth studio update --local./install.sh --package roland-slothunsloth studio update --package roland-sloth./install.shunsloth studio updateResults
./install.sh --localunsloth studio update --local./install.sh --package roland-slothunsloth studio update --package roland-sloth./install.sh(production, requires merge + PyPI release)unsloth studio update(production)Rebase conflict resolution
Rebased on main after #4489 and #4527 were merged. Resolved 3 conflicts:
install.sh— kept main's newversion_ge/_uv_version_okhelpers from gate on min uv version and shortcut python candidate search if known #4489, used our$VENV_DIRpath and auto-launch logicstudio/setup.sh— kept main'srun_quiet/run_quiet_no_exithelpers from gate on min uv version and shortcut python candidate search if known #4489, used ourSKIP_STUDIO_BASEbanner logic, removed Python discovery (our change)unsloth_cli/commands/studio.py— kept main'srun_kwargsvariable name from Fix Studio silently exiting on Windows without error output #4527 (trivial rename)