{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating persistent conda environments on Studio’s EFS\n", "\n", "This walkthrough should take around 10 minutes and only requires the console.\n", "\n", "For this notebook, please use the Data Science image and Python 3 kernel." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From within SageMaker Studio, click on the Home button and then on Open Launcher.\n", "\n", "![](../img/open-launcher.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Within the Launcher, locate the Notebooks and compute resources section. In this section, check that SageMaker image selected is a conda supported first party kernel image such as “Data Science”. Then select the Open Image Terminal option to open a terminal window with a new kernel – you will see a message saying “Starting image terminal…” and after a few moments the new terminal will open in a new tab.\n", "\n", "![](../img/open-image-terminal.png)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Within the terminal, run the following commands:\n", "\n", "```\n", "mkdir -p ~/.conda/envs\n", "conda create --yes -p ~/.conda/envs/custom\n", "conda activate ~/.conda/envs/custom\n", "conda install -y ipykernel\n", "conda config --add envs_dirs ~/.conda/envs\n", "```\n", "These commands will take about 3 minutes to run and will:\n", "1. Create a directory on the EFS volume to store the conda environments\n", "2. Create the new conda environment\n", "3. Activate the conda environment\n", "4. Install the ipykernel dependencies (without the ipykernel dependency this solution will not work)\n", "5. Create a conda configuration file (.condarc) which contains the reference to the new conda environment directory.\n", "\n", "As this is a new conda environment, there will be no additional dependencies installed, if you would like to install other dependencies you can modify the conda install line or wait for the above commands to finish and then install any additional dependencies whilst inside the conda environment.\n", "\n", "For this example, the numpy library will be installed by running the following command in the terminal window:\n", "\n", "```\n", "conda install -y numpy\n", "python -c \"import numpy; print(numpy.version.version)\"\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that the conda environment is created and the dependencies installed, you can create a notebook which uses this conda environment persisted on Amazon EFS. Go back to the launcher window and select the Create notebook option with the “Data Science” SageMaker image.\n", "\n", "![](../img/create-notebook.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From the new notebook, click the switch kernel button in the top right-hand corner which should say “Python 3 (Data Science)”:\n", "\n", "![](../img/switch-kernel-button.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From the Set up notebook environment popup, select the Kernel dropdown which will include an option for the newly created conda environment. If at first there is no option for the new conda environment, this could be because it takes a few minutes to propagate.\n", "\n", "![](../img/setup-notebook-environment.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Select the new conda environment for the kernel and then click the Select button to change the kernel. Back within the notebook, the kernel name will have changed in the top right-hand corner and within a cell you can test that the dependencies installed are available.\n", "\n", "![](../img/conda-environment-working.png)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Clean up the Conda Environment\n", "\n", "As the conda environment is stored on Amazon EFS, there will be an ongoing charge for the storage of this environment.\n", "\n", "From within the image terminal, this command can be run to tidy up the conda environment:\n", "```\n", "conda deactivate\n", "conda env remove -n custom\n", "conda config --remove envs_dirs ~/.conda/envs\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Resources\n", "Check out the following links for more information:\n", "- [Managing conda environments](https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html)\n", "- [Install External Libraries and Kernels in Amazon SageMaker Studio](https://docs.aws.amazon.com/sagemaker/latest/dg/studio-notebooks-add-external.html)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "instance_type": "ml.t3.medium", "kernelspec": { "display_name": "Python 3.10.6 64-bit", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" }, "vscode": { "interpreter": { "hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e" } } }, "nbformat": 4, "nbformat_minor": 4 }