Skip to content

Commit 73904a6

Browse files
committed
Speed-up pipx
1 parent be1a314 commit 73904a6

File tree

5 files changed

+63
-57
lines changed

5 files changed

+63
-57
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ your notebook instance enables
7070
survives instance reboot.
7171

7272
You can create a new custom conda environment as follows:
73-
`conda create --prefix /home/ec2-user/SageMaker/envs/MY_CUSTOM_ENV_NAME python=3.9 ipykernel`.
73+
`conda create --prefix /home/ec2-user/SageMaker/envs/MY_CUSTOM_ENV_NAME python=3.10 ipykernel`.
7474
Replace the environment name and python version with your choice. Please note that conda
7575
environment must have `ipykernel` package installed. Once the environment is created, you may
7676
need to [restart JupyterLab](#51-restart-jupyterlab) before you can see the environment
@@ -119,8 +119,6 @@ your notebook instance enables
119119

120120
- **\[Need sudo\]** Optionally mount one or more EFS.
121121

122-
- [Experimental tweaks](#53-experimental-tweaks) (off by default)
123-
124122
## 3. Installation
125123

126124
This step needs to be done **once** on a newly *created* notebook instance.
@@ -165,7 +163,7 @@ mounted as `/home/ec2-user/mnt/my_efs_01/`, while the second one `fs-456` will
165163
be mounted as `/home/ec2-user/mnt/my_efs_02/`.
166164

167165
After the installation step finishes, you should see a new directory created: `/home/ec2-user/SageMaker/initsmnb/`.
168-
Your next step is to jump to section [Usage](#14-usage).
166+
Your next step is to jump to section [Usage](#4-usage).
169167

170168
### 3.2. Installation from local source
171169

@@ -179,7 +177,7 @@ cd amazon-sagemaker-notebook-instance-customization/initsmnb
179177
```
180178

181179
After the installation step finishes, you should see a new directory created: `/home/ec2-user/SageMaker/initsmnb/`.
182-
Your next step is to jump to section [Usage](#14-usage).
180+
Your next step is to jump to section [Usage](#4-usage).
183181

184182
## 4. Usage
185183

initsmnb/TEMPLATE-setup-my-sagemaker.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ echo "c.FileCheckpoints.checkpoint_dir = '/tmp/.ipynb_checkpoints'" \
9090
echo "c.FileCheckpoints.checkpoint_dir = '/tmp/.ipynb_checkpoints'" \
9191
>> ~/.jupyter/jupyter_server_config.py
9292

93+
run_and_track_stat ${BIN_DIR}/install-pipx.sh &> ~/INITSMNB-install-pipx.txt &
9394
run_and_track_stat ${BIN_DIR}/upgrade-jupyter.sh &> ~/INITSMNB-upgrade-jupyter.txt &
9495
run_and_track_stat ${BIN_DIR}/install-cdk.sh &> ~/INITSMNB-install-cdk.txt &
9596
run_and_track_stat ${BIN_DIR}/install-code-server.sh &> ~/INITSMNB-install-code-server.txt &
@@ -119,11 +120,13 @@ fi
119120
# 030: Wrapping up...
120121
################################################################################
121122
# Wait for background jobs to complete.
122-
echo "
123+
COLOR_RED="\033[1;31m"
124+
COLOR_OFF="\033[0m"
125+
echo -e "
123126
Waiting for these jobs to complete...
124127
$(jobs)
125128
126-
Usually they take 4+ minutes to 7+ minutes. The high-side is typically the first
129+
Usually they take ${COLOR_RED}~4 minutes${COLOR_OFF} to ${COLOR_RED}5+ minutes${COLOR_OFF}. The high-side is typically the first
127130
time ever running initsmnb on a notebook instance, with more stuffs to install.
128131
The lower-side is for subsequent initsmnb runs (typically on notebook restart).
129132

initsmnb/install-cli.sh

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,62 +18,16 @@ echo "alias ncdu='ncdu --color dark'" | sudo tee /etc/profile.d/initsmnb-cli.sh
1818
rm ~/anaconda3/bin/nb{diff,diff-web,dime,merge,merge-web,show} ~/anaconda3/bin/git-nb* || true
1919
hash -r
2020

21-
# Pipx to install pre-commit. Otherwise, pre-commit is broken when installed
22-
# with /usr/bin/pip3 (alinux), but we don't want to use ~/anaconda/bin/pip3
23-
# either to minimize polluting its site packages.
24-
~/anaconda3/bin/pip3 install --no-cache-dir pipx
25-
26-
# Relocate pipx packages to ~/SageMaker to survive reboot
27-
export PIPX_HOME=~/SageMaker/.initsmnb.d/pipx
28-
export PIPX_BIN_DIR=~/SageMaker/.initsmnb.d/bin
29-
cat << EOF | sudo tee -a /etc/profile.d/initsmnb-cli.sh
30-
31-
# Add pipx binaries to PATH. In addition, add also ~/.local/bin so that its
32-
# commands are usable by Jupyter kernels (notable example: docker-compose for
33-
# SageMaker local mode).
34-
export PATH=\$PATH:$PIPX_BIN_DIR:/home/ec2-user/.local/bin
35-
export PIPX_HOME=$PIPX_HOME
36-
export PIPX_BIN_DIR=$PIPX_BIN_DIR
37-
EOF
38-
39-
# Stop 'pipx install' from warning about path
40-
export PATH=$PATH:$PIPX_BIN_DIR
41-
42-
declare -a PKG=(
43-
pre-commit
44-
ranger-fm
45-
cookiecutter
46-
jupytext
47-
s4cmd
48-
nvitop
49-
gpustat
50-
awslogs
51-
ruff
52-
#black
53-
#nbqa
54-
#isort
55-
#pyupgrade
56-
)
57-
58-
for i in "${PKG[@]}"; do
59-
~/anaconda3/bin/pipx install $i
60-
done
61-
62-
~/anaconda3/bin/pipx install nbdime
63-
# https://github.com/jupyter/nbdime/issues/621
64-
~/anaconda3/bin/pipx inject nbdime ipython_genutils
21+
# Use the good working nbdime
22+
ln -s ~/anaconda3/envs/JupyterSystemEnv/bin/nb{diff,diff-web,dime,merge,merge-web,show} ~/.local/bin/ || true
23+
ln -s ~/anaconda3/envs/JupyterSystemEnv/bin/git-nb* ~/.local/bin/ || true
24+
echo "nbdime path is $(which nbdime)"
6525
nbdime config-git --enable --global
6626

67-
~/anaconda3/bin/pipx upgrade-all
68-
6927
# pre-commit cache survives reboot (NOTE: can also set $PRE_COMMIT_HOME)
7028
mkdir -p ~/SageMaker/.initsmnb.d/.pre-commit.cache
7129
ln -s ~/SageMaker/.initsmnb.d/.pre-commit.cache ~/.cache/pre-commit || true
7230

73-
# ranger defaults to relative line number
74-
mkdir -p ~/.config/ranger/
75-
echo set line_numbers relative >> ~/.config/ranger/rc.conf
76-
7731
# Catch-up with awscliv2 which has nearly weekly releases.
7832
aria2c -x5 --dir /tmp -o awscli2.zip https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip
7933
cd /tmp && unzip -o -q /tmp/awscli2.zip

initsmnb/install-initsmnb.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ declare -a SCRIPTS=(
6868
fix-ipython.sh
6969
init-vim.sh
7070
install-cdk.sh
71+
install-pipx.sh
7172
QUICKSTART-CDK.md
7273
fix-pyspark-smnb.sh
7374
mount-efs-accesspoint.sh

initsmnb/install-pipx.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# Pipx to install pre-commit. Otherwise, pre-commit is broken when installed
6+
# with /usr/bin/pip3 (alinux), but we don't want to use ~/anaconda/bin/pip3
7+
# either to minimize polluting its site packages.
8+
~/anaconda3/bin/pip3 install --no-cache-dir pipx
9+
10+
# Relocate pipx packages to ~/SageMaker to survive reboot
11+
export PIPX_HOME=~/SageMaker/.initsmnb.d/pipx
12+
export PIPX_BIN_DIR=~/SageMaker/.initsmnb.d/bin
13+
cat << EOF | sudo tee /etc/profile.d/initsmnb-pipx.sh
14+
15+
# Add pipx binaries to PATH. In addition, add also ~/.local/bin so that its
16+
# commands are usable by Jupyter kernels (notable example: docker-compose for
17+
# SageMaker local mode).
18+
export PATH=\$PATH:$PIPX_BIN_DIR:/home/ec2-user/.local/bin
19+
export PIPX_HOME=$PIPX_HOME
20+
export PIPX_BIN_DIR=$PIPX_BIN_DIR
21+
EOF
22+
23+
# Stop 'pipx install' from warning about path
24+
export PATH=$PATH:$PIPX_BIN_DIR
25+
26+
declare -a PKG=(
27+
pre-commit
28+
ranger-fm
29+
cookiecutter
30+
jupytext
31+
s4cmd
32+
nvitop
33+
gpustat
34+
awslogs
35+
ruff
36+
#black
37+
#nbqa
38+
#isort
39+
#pyupgrade
40+
)
41+
42+
for i in "${PKG[@]}"; do
43+
~/anaconda3/bin/pipx install $i
44+
done
45+
46+
~/anaconda3/bin/pipx upgrade-all
47+
48+
# ranger defaults to relative line number
49+
mkdir -p ~/.config/ranger/
50+
echo set line_numbers relative >> ~/.config/ranger/rc.conf

0 commit comments

Comments
 (0)