Setup Python virtual environment on Synology NAS

Third-Party Package

Last updated: 17 July 2023

Purpose

This article will show you how to create a Python virtual environment using SSH. When used in a virtual environment, common installation tools such as pip will automatically install Python packages into the virtual environment, rather than installing them globally on the system.

This allows you to create an isolated environment with its own set of packages, independent of the system’s global Python installation or any other virtual environments you may have.

Prerequisites

  • Python3 or above is installed on your Synology NAS.
  • SSH Setup on Synology with terminal client of choice (I use Putty)

Resolution

Sign in with SSH

On your PC, use SSH to sign in to your NAS with root privileges.

Step 1. Select the Python version

Check which Python package versions are installed on your NAS with compgen -c python

Enter python3 or python3.9 to choose the Python version. We strongly recommend using these versions, since they are venv-supported and will not affect system operations.

Notes on following Python versions:

Step 2. Create your Python virtual environment

After you select the Python version, run the following commands to create your Python virtual environment. See the following image for example:


  • Choose the volume where you want to create the virtual environment.
    For example: cd /volume2/web/development
  • Create the Python virtual environment at your folder. If you want to use the Python3.9 package, replace python3 with python3.9.
  • You can replace python_dev with a name you want to use for the folder:

python3.9 -m venv python_dev
cd python_dev
source bin/activate

Step 3. Add Packages

python3.9 -m pip install --upgrade pip
python3.9 -m pip install ipykernel
python3.9 -m pip install jupyterlab
deactivate

Or add the names of all packages in requirements.txt and install all.   This provides a useful reference for what has been installed.

python3.9 -m pip install --upgrade pip
python3.9 -m pip install -r requirements.txt
deactivate

References

  1. Python venv: How To Create, Activate, Deactivate, And Delete
  2. virtual environments
  3. jupyter and venv
  4. jupyterlab

To install pytorch.   See here: https://pytorch.org/

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu