diff --git a/Installation Script/SN27 Installer Instructions.md b/Installation Script/SN27 Installer Instructions.md deleted file mode 100644 index 0dc45bd1..00000000 --- a/Installation Script/SN27 Installer Instructions.md +++ /dev/null @@ -1,78 +0,0 @@ -# Bittensor/Compute Subnet Installer Script -This repository contains an installation script for setting up a Bittensor miner with the necessary dependencies and configurations for SN27 (Subnet 27) of the Bittensor network. This installation process requires Ubuntu 22.04. You are limited to one external IP per UID. There is automatic blacklisting in place if validators detect anomalous behavior. - - -## Features - -- Installs Bittensor and its dependencies -- Installs Docker for containerization -- Installs NVIDIA docker support for optimized GPU functionality -- Installs Subtensor and Starts a Lite Node on Mainnet -- Installs PM2 for process management -- Clones and installs the Compute-Subnet repository and its dependencies -- Starts the Docker service within Compute Subnet -- Installs Hashcat for computational tasks -- Installs NVIDIA drivers and CUDA toolkit for GPU functionality -- Installs UFW and configures ports for miners -- Provides a convenient one-line command for easy installation - -## Usage - -This installation process requires Ubuntu 22.04. - -Use a Virtual Environment - -When working with Bittensor miners across various Subnets, it's highly recommended to use a virtual environment (venv) to isolate the dependencies and avoid potential conflicts. Each subnet may have different requirements, and using a separate virtual environment ensures that the dependencies are managed independently. - -To create and activate a virtual environment, follow these steps: - -1. Open a terminal and navigate to the directory where you want to create the virtual environment. - -2. Run the following command to create a new virtual environment: -``` -python3 -m venv myenv -``` - -Replace `myenv` with your desired name for the virtual environment. - -3. Activate the virtual environment: -- For Linux or macOS: - ``` - source myenv/bin/activate - ``` -- For Windows: - ``` - myenv\Scripts\activate - ``` - -4. Once the virtual environment is activated, you'll see the name of the environment in your terminal prompt, indicating that you're now working within the isolated environment. - -5. Proceed with installing the required dependencies and running the Bittensor miner specific to the Subnet you're working on. - -6. To deactivate the virtual environment when you're done, simply run: -``` -deactivate -``` -Using a virtual environment ensures that the dependencies and requirements are isolated and avoids potential conflicts. - -Please remember to activate the appropriate virtual environment when you switch between Subnets or working on a different miner/project. - -To install Bittensor with SN27 dependencies, simply run the following command in your terminal: -``` -/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/cisterciansis/Bittensor-Compute-Subnet-Installer-Script/main/install_sn27.sh)" -``` -The script will guide you through the installation process and set up the necessary components for running a Bittensor miner on SN27. - -Please note that this script is designed for Linux systems with the apt package manager. If you're using a different Linux distribution or package manager, you may need to modify the script accordingly. - -## Disclaimer - -This script is provided as-is and without warranty. Use it at your own risk. Make sure to review the script and understand its actions before running it on your system. - -## Contributions - -Contributions to improve the script or add support for other platforms are welcome! Please feel free to open issues or submit pull requests. - -## License - -This script is released under the [MIT License](https://opensource.org/licenses/MIT). diff --git a/Installation Script/install_sn27.sh b/Installation Script/install_sn27.sh deleted file mode 100644 index 14454e05..00000000 --- a/Installation Script/install_sn27.sh +++ /dev/null @@ -1,376 +0,0 @@ -#!/bin/bash -set -u - -# enable command completion -set -o history -o histexpand - -python="python3" - -abort() { - printf "%s\n" "$1" - exit 1 -} - -getc() { - local save_state - save_state=$(/bin/stty -g) - /bin/stty raw -echo - IFS= read -r -n 1 -d '' "$@" - /bin/stty "$save_state" -} - -exit_on_error() { - exit_code=$1 - last_command=${@:2} - if [ $exit_code -ne 0 ]; then - >&2 echo "\"${last_command}\" command failed with exit code ${exit_code}." - exit $exit_code - fi -} - -wait_for_user() { - local c - echo - echo "Press RETURN to continue or any other key to abort" - getc c - # we test for \r and \n because some stuff does \r instead - if ! [[ "$c" == $'\r' || "$c" == $'\n' ]]; then - exit 1 - fi -} - -shell_join() { - local arg - printf "%s" "$1" - shift - for arg in "$@"; do - printf " " - printf "%s" "${arg// /\ }" - done -} - -# string formatters -if [[ -t 1 ]]; then - tty_escape() { printf "\033[%sm" "$1"; } -else - tty_escape() { :; } -fi -tty_mkbold() { tty_escape "1;$1"; } -tty_underline="$(tty_escape "4;39")" -tty_blue="$(tty_mkbold 34)" -tty_red="$(tty_mkbold 31)" -tty_bold="$(tty_mkbold 39)" -tty_reset="$(tty_escape 0)" - -ohai() { - printf "${tty_blue}==>${tty_bold} %s${tty_reset}\n" "$(shell_join "$@")" -} - -# Things can fail later if `pwd` doesn't exist. -# Also sudo prints a warning message for no good reason -cd "/usr" || exit 1 - -linux_install_pre() { - sudo apt-get update - sudo apt-get install --no-install-recommends --no-install-suggests -y apt-utils curl git cmake build-essential ca-certificates - - # Add Docker's official GPG key: - sudo install -m 0755 -d /etc/apt/keyrings - sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc - sudo chmod a+r /etc/apt/keyrings/docker.asc - - # Add the repository to Apt sources: - echo \ - "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ - $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ - sudo tee /etc/apt/sources.list.d/docker.list > /dev/null - - sudo apt-get update - - # Install the latest version of Docker - sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin - - exit_on_error $? -} - -linux_install_subtensor() { - ohai "Cloning subtensor into ~/subtensor" - mkdir -p ~/subtensor - sudo apt install -y git - git clone https://github.com/opentensor/subtensor.git - cd subtensor -} - -linux_install_python() { - which $python - if [[ $? != 0 ]] ; then - ohai "Installing python" - sudo apt-get install --no-install-recommends --no-install-suggests -y $python - else - ohai "Updating python" - sudo apt-get install --only-upgrade $python - fi - exit_on_error $? - ohai "Installing python tools" - sudo apt-get install --no-install-recommends --no-install-suggests -y $python-pip $python-dev - exit_on_error $? -} - -linux_update_pip() { - PYTHONPATH=$(which $python) - ohai "You are using python@ $PYTHONPATH$" - ohai "Installing python tools" - $python -m pip install --upgrade pip -} - -linux_install_bittensor() { - ohai "Cloning bittensor@master into ~/.bittensor/bittensor" - mkdir -p ~/.bittensor/bittensor - git clone https://github.com/opentensor/bittensor.git ~/.bittensor/bittensor/ 2> /dev/null || (cd ~/.bittensor/bittensor/ ; git fetch origin master ; git checkout master ; git pull --ff-only ; git reset --hard ; git clean -xdf) - ohai "Installing bittensor" - $python -m pip install -e ~/.bittensor/bittensor/ - exit_on_error $? -} - -linux_increase_ulimit(){ - ohai "Increasing ulimit to 1,000,000" - prlimit --pid=$PPID --nofile=1000000 -} - -linux_install_pm2() { - sudo apt update - sudo apt install -y npm - sudo npm install pm2 -g -} - -linux_install_nvidia_docker() { - ohai "Installing NVIDIA Docker support" - cd - distribution=$(. /etc/os-release;echo $ID$VERSION_ID) - curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add - - curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list - sudo apt update - sudo apt-get install -y nvidia-container-toolkit - sudo apt install -y nvidia-docker2 -} - -linux_install_compute_subnet() { - ohai "Cloning Compute-Subnet into ~/Compute-Subnet" - mkdir -p ~/Compute-Subnet - git clone https://github.com/neuralinternet/Compute-Subnet.git ~/Compute-Subnet/ 2> /dev/null || (cd ~/Compute-Subnet/ ; git pull --ff-only ; git reset --hard ; git clean -xdf) - - ohai "Installing Compute-Subnet dependencies" - cd ~/Compute-Subnet - $python -m pip install -r requirements.txt - $python -m pip install --no-deps -r requirements-compute.txt - $python -m pip install -e . - sudo apt -y install ocl-icd-libopencl1 pocl-opencl-icd - - ohai "Starting Docker service, adding user to docker, and installing 'at' package" - sudo groupadd docker - sudo usermod -aG docker $USER - sudo systemctl start docker - sudo apt install -y at - - cd ~ - exit_on_error $? -} - -linux_install_hashcat() { - wget https://hashcat.net/files/hashcat-6.2.6.tar.gz - tar xzvf hashcat-6.2.6.tar.gz - cd hashcat-6.2.6/ - sudo make - sudo make install - export PATH=$PATH:/usr/local/bin/ - echo "export PATH=$PATH">>~/.bashrc - cd ~ -} - -linux_install_nvidia_cuda() { - wget https://developer.download.nvidia.com/compute/cuda/12.3.1/local_installers/cuda-repo-ubuntu2204-12-3-local_12.3.1-545.23.08-1_amd64.deb - sudo dpkg -i cuda-repo-ubuntu2204-12-3-local_12.3.1-545.23.08-1_amd64.deb - sudo cp /var/cuda-repo-ubuntu2204-12-3-local/cuda-*-keyring.gpg /usr/share/keyrings/ - sudo apt-get update - sudo apt-get -y install cuda-toolkit-12-3 - sudo apt-get -y install -y cuda-drivers - export CUDA_VERSION=cuda-12.3 - export PATH=$PATH:/usr/local/$CUDA_VERSION/bin - export LD_LIBRARY_PATH=/usr/local/$CUDA_VERSION/lib64 - echo "">>~/.bashrc - echo "PATH=$PATH">>~/.bashrc - echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH">>~/.bashrc -} - -linux_install_ufw() { - sudo apt update - sudo apt install -y ufw - sudo ufw allow 22/tcp - sudo ufw allow 4444 -} - -linux_configure_ufw() { - echo "Please enter the port range for UFW (e.g., 2000-5000):" - read -p "Enter port range (start-end): " port_range - - if [[ $port_range =~ ^[0-9]+-[0-9]+$ ]]; then - start_port=$(echo $port_range | cut -d'-' -f1) - end_port=$(echo $port_range | cut -d'-' -f2) - - if [[ $start_port -lt $end_port ]]; then - sudo ufw allow $start_port:$end_port/tcp - sudo ufw enable - echo "UFW configured successfully with port range $port_range" - else - echo "Invalid port range. The start port should be less than the end port." - exit 1 - fi - else - echo "Invalid port range format. Please use the format: start-end (e.g., 2000-5000)" - exit 1 - fi -} - -# Do install. -OS="$(uname)" -if [[ "$OS" == "Linux" ]]; then - - which -s apt - if [[ $? == 0 ]] ; then - abort "This linux based install requires apt. To run with other distros (centos, arch, etc), you will need to manually install the requirements" - fi - echo """ - - ░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓████████▓▒░ -░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ -░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░ - ░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░ - ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░ - ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░ -░▒▓███████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓████████▓▒░ ░▒▓█▓▒░ - - - Bittensor; Mining a new element. - """ - ohai "This script will install:" - echo "git" - echo "curl" - echo "cmake" - echo "build-essential" - echo "python3" - echo "python3-pip" - echo "subtensor" - echo "bittensor" - echo "docker" - echo "nvidia docker support" - echo "pm2" - echo "compute-subnet" - echo "hashcat" - echo "nvidia drivers and cuda toolkit" - echo "ufw" - - wait_for_user - linux_install_pre - linux_install_subtensor - linux_install_python - linux_update_pip - linux_install_bittensor - linux_install_pm2 - linux_install_nvidia_docker - linux_install_compute_subnet - linux_install_hashcat - linux_install_nvidia_cuda - linux_install_ufw - linux_configure_ufw - - ohai "Would you like to increase the ulimit? This will allow your miner to run for a longer time" - wait_for_user - linux_increase_ulimit - echo "" - echo "" - echo "######################################################################" - echo "## ##" - echo "## BITTENSOR SN27 SETUP ##" - echo "## ##" - echo "######################################################################" - echo "" - echo "" - -elif [[ "$OS" == "Darwin" ]]; then - echo """ - -██████╗░██╗████████╗████████╗███████╗███╗░░██╗░██████╗░█████╗░██████╗░ -██╔══██╗██║╚══██╔══╝╚══██╔══╝██╔════╝████╗░██║██╔════╝██╔══██╗██╔══██╗ -██████╦╝██║░░░██║░░░░░░██║░░░█████╗░░██╔██╗██║╚█████╗░██║░░██║██████╔╝ -██╔══██╗██║░░░██║░░░░░░██║░░░██╔══╝░░██║╚████║░╚═══██╗██║░░██║██╔══██╗ -██████╦╝██║░░░██║░░░░░░██║░░░███████╗██║░╚███║██████╔╝╚█████╔╝██║░░██║ -╚═════╝░╚═╝░░░╚═╝░░░░░░╚═╝░░░╚══════╝╚═╝░░╚══╝╚═════╝░░╚════╝░╚═╝░░╚═╝ - - - Mining a new element. - """ - ohai "This script will install:" - echo "xcode" - echo "homebrew" - echo "git" - echo "cmake" - echo "python3" - echo "python3-pip" - echo "bittensor" - - wait_for_user - mac_install_brew - mac_install_cmake - mac_install_python - mac_update_pip - mac_install_bittensor - echo "" - echo "" - echo "######################################################################" - echo "## ##" - echo "## BITTENSOR SETUP ##" - echo "## ##" - echo "######################################################################" -else - abort "Bittensor is only supported on macOS and Linux" -fi - -# Use the shell's audible bell. -if [[ -t 1 ]]; then -printf "\a" -fi - -echo "" -echo "" -ohai "Welcome. Installation complete. Please reboot your machine for the changes to take effect:" -echo " $ sudo reboot" -echo "" -echo "- 1. Create a wallet pair" -echo " $ btcli w new_coldkey (for holding funds)" -echo " $ btcli w new_hotkey (for running miners)" -echo "" -echo "- 2. To run a miner on the Compute Subnetwork (SN27) you must first create a wallet pair and register to SN27. Visit ${tty_underline}https://docs.neuralinternet.ai/products/subnet-27-compute/bittensor-compute-subnet-miner-setup${tty_reset} for Instructions " -echo " pm2 start ./neurons/miner.py --name MINER --interpreter python3 -- --netuid 27 --subtensor.network local --wallet.name COLDKEYNAME --wallet.hotkey HOTKEYNAME --axon.port XXXX --logging.debug --miner.blacklist.force_validator_permit --auto_update yes " -echo "" -ohai "Extras:" -echo "" -echo "- Check your tao balance: " -echo " $ btcli wallet overview" -echo "" -echo "- Stake to your miners:" -echo " $ btcli stake add" -echo " $ btcli stake remove" -echo "" -echo "- Create/list/register wallets" -echo " $ btcli w new_coldkey" -echo " $ btcli w new_hotkey" -echo " $ btcli s list or $ btcli w list" -echo " $ btcli s register --subtensor.network finney --netuid 27" -echo "" -echo "- Use the Python API" -echo " $ python3" -echo " >> import bittensor" -echo "" -echo "- Join the discussion: " -echo " ${tty_underline}https://discord.gg/3rUr6EcvbB${tty_reset}" -echo "" -ohai "Installation complete. Please reboot your machine for the changes to take effect:" -echo " $ sudo reboot" diff --git a/scripts/installer/README.md b/scripts/installer/README.md new file mode 100644 index 00000000..4e263b16 --- /dev/null +++ b/scripts/installer/README.md @@ -0,0 +1,187 @@ +# Bittensor and Compute-Subnet Setup Guide + +## Overview +This guide provides comprehensive instructions for setting up Bittensor and Compute-Subnet on a Linux system. Follow these steps to prepare your machine for mining and earning rewards. + +## Prerequisites + +### Required Items +- **Weights & Biases (WANDB) API Key** + - Generate from WANDB Account Settings + - Required for logging mining statistics + +- **Bittensor Wallets** + - Coldkey and Hotkey required + - Must be registered on Bittensor network + - Can be created using `btcli new_coldkey` and `btcli new_hotkey` + +- **GPU Drivers** + - Ubuntu 22.04: Automatic NVIDIA driver installation + - Other distributions: Manual CUDA driver installation required + - Verify installation with `nvidia-smi` and `nvcc --version` + +## CUDA Installation +Visit NVIDIA's official CUDA download page to get the latest version: +https://developer.nvidia.com/cuda-downloads + +For Ubuntu 22.04 (update version numbers as needed): + +1. Download CUDA repository package: +```bash +wget https://developer.download.nvidia.com/compute/cuda/12.3.1/local_installers/cuda-repo-ubuntu2204-12-3-local_12.3.1-545.23.08-1_amd64.deb +``` + +2. Install repository package: +```bash +sudo dpkg -i cuda-repo-ubuntu2204-12-3-local_12.3.1-545.23.08-1_amd64.deb +``` + +3. Copy keyring: +```bash +sudo cp /var/cuda-repo-ubuntu2204-12-3-local/cuda-*-keyring.gpg /usr/share/keyrings/ +``` + +4. Update and install CUDA: +```bash +sudo apt-get update +sudo apt-get -y install cuda-toolkit-12-3 +sudo apt-get -y install -y cuda-drivers +``` + +5. Set up environment variables: +```bash +export CUDA_VERSION=cuda-12.3 +export PATH=$PATH:/usr/local/$CUDA_VERSION/bin +export LD_LIBRARY_PATH=/usr/local/$CUDA_VERSION/lib64 +``` + +6. Add to bashrc: +```bash +echo "">>~/.bashrc +echo "PATH=$PATH">>~/.bashrc +echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH">>~/.bashrc +source ~/.bashrc +``` + +7. Reboot system: +```bash +sudo reboot +``` + +### Verify CUDA Installation +Run `nvidia-smi`. Expected output should look like: +``` ++---------------------------------------------------------------------------------------+ +| NVIDIA-SMI 545.29.06 Driver Version: 545.29.06 CUDA Version: 12.3 | +|-----------------------------------------+----------------------+----------------------+ +| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC | +| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. | +| | | MIG M. | +|=========================================+======================+======================| +| 0 NVIDIA RTX Off | 00000000:05:00.0 Off | Off | +| 30% 34C P0 70W / 300W | 400MiB / 4914000MiB | 4% Default | +| | | N/A | ++-----------------------------------------+----------------------+----------------------+ +``` + +Run `nvcc --version`. Expected output: +``` +nvcc: NVIDIA (R) Cuda compiler driver +Copyright (c) 2005-2023 NVIDIA Corporation +Built on Fri_Nov__3_17:16:49_PDT_2023 +Cuda compilation tools, release 12.3, V12.3.103 +Build cuda_12.3.r12.3/compiler.33492891_0 +``` + +## Bittensor Installation + +### Step 1: Download and Prepare Installer +```bash +curl -sL https://raw.githubusercontent.com/neuralinternet/compute-subnet/main/scripts/installer/install_sn27.sh + -o SN27_installer.sh +chmod +x SN27_installer.sh +``` + +### Step 2: Run Installation Script +```bash +./SN27_installer.sh +``` + +The installer will: +- Set up Docker with NVIDIA support +- Configure PM2 and NodeJS +- Create Python 3.10 virtual environment +- Clone and set up Compute-Subnet repository +- Install Bittensor dependencies + +## Post-Installation Verification + +### Reboot your machine +```bash +sudo reboot +``` + +### Enter Virtual Environment +```bash +source /home/ubuntu/venv/bin/activate +``` + +### Docker Verification +```bash +docker --version +``` + +### Bittensor CLI Verification +```bash +btcli --version +``` + +### Directory Check +```bash +cd /home/ubuntu/Compute-Subnet +ls +``` + +## Running a Miner + +### Basic Miner Command +```bash +cd /home/ubuntu/Compute-Subnet +pm2 start ./neurons/miner.py --name MINER --interpreter python3 -- \ + --netuid 15 \ + --subtensor.network test \ + --wallet.name default \ + --wallet.hotkey default \ + --axon.port 8091 \ + --logging.debug \ + --miner.blacklist.force_validator_permit \ + --auto_update yes +``` + +## Troubleshooting + +### Common Issues and Solutions +1. **Missing WANDB Key** + - Edit `/home/ubuntu/Compute-Subnet/.env` + - Add your WANDB API key + +2. **Unregistered Wallet** + - Register your coldkey on Bittensor network first + - Use `btcli register` command + +3. **Docker Permissions** + ```bash + sudo usermod -aG docker $USER + ``` + Requires system relogin + +4. **Driver Issues** + - Manual installation required for non-Ubuntu 22.04 systems + - Verify with `nvidia-smi` + - Follow CUDA installation steps above if needed + +## Additional Resources +- [Weights & Biases Documentation](https://docs.wandb.ai/) +- [Bittensor Documentation](https://docs.bittensor.com/) +- [Compute-Subnet Documentation](https://docs.neuralinternet.ai) +- [NVIDIA CUDA Downloads](https://developer.nvidia.com/cuda-downloads) \ No newline at end of file diff --git a/scripts/installer/install_sn27.sh b/scripts/installer/install_sn27.sh new file mode 100644 index 00000000..6c379f17 --- /dev/null +++ b/scripts/installer/install_sn27.sh @@ -0,0 +1,485 @@ +#!/bin/bash +set -u + +# Enable command completion +set -o history -o histexpand + +python="python3" + +abort() { + printf "%s\n" "$1" + exit 1 +} + +exit_on_error() { + exit_code=$1 + last_command=${@:2} + if [ $exit_code -ne 0 ]; then + >&2 echo "\"${last_command}\" command failed with exit code ${exit_code}." + exit $exit_code + fi +} + +# Minimal logger +ohai() { + echo "==> $*" +} + +############################################## +# 1. Detect if it is AUTOMATED or manual mode +############################################## + +# AUTOMATED can be set as an environment variable, for example "true" +AUTOMATED="${AUTOMATED:-false}" + +# Or parse a --automated flag +if [[ "${1:-}" == "--automated" ]]; then + AUTOMATED="true" +fi + +WANDB_ENV="${WANDB_KEY:-}" + +ask_user_for_wandb_key() { + read -rp "Enter WANDB_API_KEY (leave blank if none): " WANDB_ENV +} + +########################################## +# Insert WANDB_API_KEY into .env +########################################## +inject_wandb_env() { + local env_example="/home/ubuntu/Compute-Subnet/.env.example" + local env_path="/home/ubuntu/Compute-Subnet/.env" + + ohai "Configuring .env for Compute-Subnet..." + + if [[ ! -f "$env_path" ]] && [[ -f "$env_example" ]]; then + ohai "Copying .env.example to .env" + sudo -u ubuntu cp "$env_example" "$env_path" + fi + + if [[ -n "$WANDB_ENV" ]]; then + ohai "Updating WANDB_API_KEY in .env" + sudo -u ubuntu sed -i "s|^WANDB_API_KEY=.*|WANDB_API_KEY=\"$WANDB_ENV\"|" "$env_path" + else + ohai "No WANDB_API_KEY provided. Skipping replacement in .env." + fi + + sudo chown ubuntu:ubuntu "$env_path" + ohai "Done configuring .env" +} + +getc() { + local save_state + save_state=$(/bin/stty -g) + /bin/stty raw -echo + IFS= read -r -n 1 -d '' "$@" + /bin/stty "$save_state" +} + +wait_for_user() { + local c + echo + echo "Press ENTER to continue or any other key to abort" + getc c + # we test for \r and \n because some stuff does \r instead + if ! [[ "$c" == $'\r' || "$c" == $'\n' ]]; then + exit 1 + fi +} + +################################################################################ +# PRE-INSTALL +################################################################################ +linux_install_pre() { + sudo apt-get update + sudo apt-get install --no-install-recommends --no-install-suggests -y apt-utils curl git cmake build-essential ca-certificates + + # Add Docker's official GPG key: + sudo install -m 0755 -d /etc/apt/keyrings + sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc + sudo chmod a+r /etc/apt/keyrings/docker.asc + + # Add Docker's repository: + echo \ + "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ + $(. /etc/os-release && echo "$VERSION_CODENAME") stable" \ + | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null + + sudo apt-get update + sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin + exit_on_error $? "docker-installation" + + # 4. Add the 'ubuntu' user to the 'docker' group, so they can run Docker without sudo + sudo usermod -aG docker ubuntu + + ohai "Docker installation complete. 'ubuntu' user added to 'docker' group." + ohai "IMPORTANT: A new shell session (or reboot) is required for 'ubuntu' to see the new group membership." +} + +################################################################################ +# SETUP VENV +################################################################################ +linux_setup_venv() { + ohai "Installing python3.10-venv (if not present)" + sudo apt-get install -y python3.10-venv + + ohai "Creating Python venv in /home/ubuntu/venv" + # Create the venv as the ubuntu user + sudo -u ubuntu -H python3 -m venv /home/ubuntu/venv + exit_on_error $? "venv-creation" + + # Upgrade pip inside the venv + ohai "Upgrading pip in the new venv" + sudo -u ubuntu -H /home/ubuntu/venv/bin/pip install --upgrade pip + exit_on_error $? "venv-pip-upgrade" + + if [[ "$AUTOMATED" == "true" ]]; then + ohai "Adding 'source /home/ubuntu/venv/bin/activate' to ~/.bashrc (automated mode)" + echo "source /home/ubuntu/venv/bin/activate" | sudo tee -a /home/ubuntu/.bashrc + sudo chown ubuntu:ubuntu /home/ubuntu/.bashrc + else + ohai "Skipping automatic venv activation in ~/.bashrc (manual mode)" + fi +} + +################################################################################ +# COMPUTE-SUBNET +################################################################################ +linux_install_compute_subnet() { + ohai "Cloning or updating Compute-Subnet into /home/ubuntu/Compute-Subnet" + sudo mkdir -p /home/ubuntu/Compute-Subnet + + if [ ! -d /home/ubuntu/Compute-Subnet/.git ]; then + # If not cloned, we clone it + sudo git clone https://github.com/neuralinternet/Compute-Subnet.git /home/ubuntu/Compute-Subnet/ + else + # If already cloned, we pull + cd /home/ubuntu/Compute-Subnet + sudo git pull --ff-only + fi + + # Ensure that "ubuntu" is the owner of the folder + sudo chown -R ubuntu:ubuntu /home/ubuntu/Compute-Subnet + + ohai "Installing Compute-Subnet dependencies (including correct Bittensor version)" + cd /home/ubuntu/Compute-Subnet + + # Install inside the venv + sudo -u ubuntu -H /home/ubuntu/venv/bin/pip install -r requirements.txt + sudo -u ubuntu -H /home/ubuntu/venv/bin/pip install --no-deps -r requirements-compute.txt + + # Editable installation of Compute-Subnet + sudo -u ubuntu -H /home/ubuntu/venv/bin/pip install -e . + exit_on_error $? "compute-subnet-installation" + + # Install extra libraries for OpenCL + sudo apt -y install ocl-icd-libopencl1 pocl-opencl-icd + + ohai "Starting Docker service, adding user to docker, installing 'at' package" + sudo groupadd docker 2>/dev/null || true + sudo usermod -aG docker ubuntu + sudo systemctl start docker + sudo apt install -y at + + cd /home/ubuntu +} + +################################################################################ +# PYTHON +################################################################################ +linux_install_python() { + if ! command -v "$python" >/dev/null 2>&1; then + ohai "Installing python" + sudo apt-get install --no-install-recommends --no-install-suggests -y "$python" + else + ohai "Upgrading python" + sudo apt-get install --only-upgrade "$python" + fi + exit_on_error $? "python-install" + + ohai "Installing python dev tools" + sudo apt-get install --no-install-recommends --no-install-suggests -y \ + "${python}-pip" "${python}-dev" + exit_on_error $? "python-dev" +} + +linux_update_pip() { + ohai "Upgrading pip (system-wide)" + "$python" -m pip install --upgrade pip + exit_on_error $? "pip-upgrade" +} + +################################################################################ +# PM2 +################################################################################ +linux_install_pm2() { + sudo apt-get update + sudo apt-get install -y npm + sudo npm install pm2 -g +} + +################################################################################ +# NVIDIA DOCKER +################################################################################ +linux_install_nvidia_docker() { + ohai "Installing NVIDIA Docker support" + local distribution=$(. /etc/os-release; echo $ID$VERSION_ID) + + curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add - + curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list \ + | sudo tee /etc/apt/sources.list.d/nvidia-docker.list + + sudo apt-get update -y + sudo apt-get install -y nvidia-container-toolkit nvidia-docker2 + + ohai "NVIDIA Docker installed" +} + +detect_ubuntu_version() { + source /etc/os-release 2>/dev/null || { + echo "Cannot detect /etc/os-release. Not an Ubuntu-based system?" + return 1 + } + + if [[ "$ID" == "ubuntu" && "$VERSION_ID" == "22.04" ]]; then + echo "ubuntu-22.04" + else + echo "unsupported" + fi +} + +################################################################################ +# CUDA INSTALLATION (NO removal of existing drivers) +################################################################################ +linux_install_nvidia_cuda() { + local distro=$(detect_ubuntu_version) + + if [[ "$distro" == "unsupported" ]]; then + ohai "Detected a distro/version that this script does not support for CUDA. Please install manually following NVIDIA docs." + return 0 + fi + + + if command -v nvidia-smi >/dev/null 2>&1 || command -v nvcc >/dev/null 2>&1; then + ohai "CUDA/NVIDIA drivers found; skipping re-installation." + return + fi + + ohai "CUDA/NVIDIA drivers not found. Installing for Ubuntu 22.04..." + + # STEPS pinned approach Ubuntu 22.04 + # 1. build-essential ... + sudo apt-get update + sudo apt-get install -y build-essential dkms linux-headers-$(uname -r) + + # 2. pinned file + wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin + sudo mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600 + + # 3. local .deb + wget https://developer.download.nvidia.com/compute/cuda/12.6.3/local_installers/cuda-repo-ubuntu2204-12-6-local_12.6.3-560.35.05-1_amd64.deb \ + -O /tmp/cuda-repo.deb + sudo dpkg -i /tmp/cuda-repo.deb + sudo cp /var/cuda-repo-ubuntu2204-12-6-local/cuda-*-keyring.gpg /usr/share/keyrings/ + sudo apt-get update + + # 4. Install toolkit + sudo apt-get -y install cuda-toolkit-12-6 + + # 5. Environment variables + ohai "Configuring environment variables for CUDA 12.6" + { + echo "" + echo "# Added by NVIDIA CUDA install script" + echo "export PATH=/usr/local/cuda-12.6/bin:\$PATH" + echo "export LD_LIBRARY_PATH=/usr/local/cuda-12.6/lib64:\$LD_LIBRARY_PATH" + } | sudo tee -a /home/ubuntu/.bashrc + + sudo chown ubuntu:ubuntu /home/ubuntu/.bashrc + source /home/ubuntu/.bashrc + + ohai "CUDA 12.6 installed successfully on Ubuntu 22.04!" +} + +############################################ +# Manual mode: request port range +############################################ +linux_configure_ufw() { + sudo apt-get update && sudo apt-get install -y ufw + + ohai "Allowing SSH (port 22) through UFW..." + sudo ufw allow 22/tcp + + # If AUTOMATED mode is true, skip user input and use defaults: + if [[ "$AUTOMATED" == "true" ]]; then + port_4444=4444 + port_8091=8091 + + ohai "AUTOMATED mode detected." + ohai "Opening port $port_4444 (validators/dashboard) and port $port_8091 (Axon)..." + sudo ufw allow "${port_4444}/tcp" + sudo ufw allow "${port_8091}/tcp" + else + # Otherwise, ask the user which ports to open: + echo "Enter the port number for validators/dashboard (Default: 4444)." + echo " 4444 is used by validators and dashboard to allocate the machine via SSH." + read -p "Port for validators/dashboard [4444]: " port_4444 + port_4444="${port_4444:-4444}" # If empty, default to 4444 + + echo "Enter the port number for Axon (Default: 8091)." + echo " 8091 is the typical port for Axon." + read -p "Port for Axon [8091]: " port_8091 + port_8091="${port_8091:-8091}" # If empty, default to 8091 + + ohai "Opening port $port_4444 (validators/dashboard) and port $port_8091 (Axon)..." + sudo ufw allow "${port_4444}/tcp" + sudo ufw allow "${port_8091}/tcp" + fi + + ohai "Enabling UFW..." + sudo ufw --force enable + + ohai "UFW configured. Open ports: 22 (SSH), $port_4444, $port_8091." +} + + + + + +################################################################################ +# ULIMIT (CONFIGURABLE) +################################################################################ +linux_increase_ulimit(){ + if [[ "$AUTOMATED" == "true" ]]; then + ohai "AUTOMATED mode: Increasing ulimit to 1,000,000" + prlimit --pid=$$ --nofile=1000000 + else + ohai "Current open-files limit (ulimit -n) is: $(ulimit -n)" + read -rp "Increase ulimit to 1,000,000? [y/N]: " do_ulimit + do_ulimit="${do_ulimit,,}" + if [[ "$do_ulimit" == "y" || "$do_ulimit" == "yes" ]]; then + ohai "Raising ulimit to 1,000,000..." + prlimit --pid=$$ --nofile=1000000 + else + ohai "Leaving ulimit as is." + fi + fi +} + + +################################################################################ +# MAIN INSTALL +################################################################################ +OS="$(uname)" +if [[ "$OS" == "Linux" ]]; then + + # Verify if apt is installed + if ! command -v apt >/dev/null 2>&1; then + abort "This Linux-based install requires apt. For other distros, install requirements manually." + fi + + echo """ + + ░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓████████▓▒░ +░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ +░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░ + ░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░ + ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░ + ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░ +░▒▓███████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓████████▓▒░ ░▒▓█▓▒░ + + - Bittensor; Mining a new element. + """ + if [[ "$AUTOMATED" == "true" ]]; then + ohai "Running in automated mode. Skipping interactive messages." + else + ohai "This script will install:" + echo "git" + echo "curl" + echo "cmake" + echo "build-essential" + echo "python3" + echo "python3-pip" + echo "subtensor" + echo "bittensor" + echo "docker" + echo "nvidia docker support" + echo "pm2" + echo "compute-subnet" + echo "hashcat" + echo "nvidia drivers and cuda toolkit" + echo "ufw" + + wait_for_user + fi + linux_install_pre + + # Step 1: Install python, pip + linux_install_python + linux_update_pip + + # Step 2: Create and configure venv in /home/ubuntu/venv + linux_setup_venv + + # Step 3: Install Compute-Subnet and Bittensor inside the venv + linux_install_compute_subnet + + # PM2 (NodeJS) + linux_install_pm2 + + # NVIDIA docker + linux_install_nvidia_docker + + # CUDA (without removing existing drivers) + linux_install_nvidia_cuda + + # UFW + linux_configure_ufw + + # ulimit + linux_increase_ulimit + + #wandb_key + if [[ "$AUTOMATED" == "false" ]]; then + ohai "Enter your wandb api key..." + ask_user_for_wandb_key + fi + + inject_wandb_env + + echo "" + echo "" + echo "" + echo "" + echo """ + +██████╗░██╗████████╗████████╗███████╗███╗░░██╗░██████╗░█████╗░██████╗░ +██╔══██╗██║╚══██╔══╝╚══██╔══╝██╔════╝████╗░██║██╔════╝██╔══██╗██╔══██╗ +██████╦╝██║░░░██║░░░░░░██║░░░█████╗░░██╔██╗██║╚█████╗░██║░░██║██████╔╝ +██╔══██╗██║░░░██║░░░░░░██║░░░██╔══╝░░██║╚████║░╚═══██╗██║░░██║██╔══██╗ +██████╦╝██║░░░██║░░░░░░██║░░░███████╗██║░╚███║██████╔╝╚█████╔╝██║░░██║ +╚═════╝░╚═╝░░░╚═╝░░░░░░╚═╝░░░╚══════╝╚═╝░░╚══╝╚═════╝░░╚════╝░╚═╝░░╚═╝ + + - Mining a new element. + """ + echo "######################################################################" + echo "## ##" + echo "## BITTENSOR SETUP ##" + echo "## ##" + echo "######################################################################" + +elif [[ "$OS" == "Darwin" ]]; then + abort "macOS installation is not implemented in this auto script." +else + abort "Bittensor is only supported on macOS and Linux" +fi + +# Final messages +echo "" +echo "Installation complete. Please reboot your machine for the changes to take effect:" +echo " sudo reboot" + +echo "" +echo "After reboot, you can create a wallet pair and run your miner on SN27." +echo "See docs: https://docs.neuralinternet.ai/products/subnet-27-compute/bittensor-compute-subnet-miner-setup" +echo "Done."