How to install PyPy3 + virtual environment in 30 seconds
TL;DR:
Run this
wget -qO- https://techoverflow.net/scripts/pypy3-installer.sh | bash
then run vpypy
every time you want to activate (you might need to restart). The script currently assumes you are running Linux x86_64
and have installed virtualenv (sudo apt install virtualenv
or similar if you don’t have it installed)
Full description:
PyPy is an alternate Python implementation that can be used to speed up many workloads. However, installing it is a somewhat cumbersome process, especially if you don’t have too much experience with virtual environments and related concepts.
We provide a script that automatically downloads PyPy3, installs it to ~/.pypy3
and creates a virtual environment in ~/.pypy3-virtualenv
. After that, it creates a shell alias vpypy
that aliases to source ~/.pypy3-virtualenv/bin/activate
and hence provides an easily memoizable way of activating the environment without requiring the user to memoize the directory.
Also, since both pypy3
itself and the virtual environment are installed in the user’s home directory, running this script does not require admin permissions.
After running the script using
wget -qO- https://techoverflow.net/scripts/pypy3-installer.sh | bash
you can activate the virtual environment using the vpypy
alias that is automatically added to ~/.bashrc
and ~/.zshrc
. Restart your shell for the alias definition to load, then run vpypy
:
uli@uli-laptop ~ % vpypy
(.pypy3-virtualenv) uli@uli-laptop ~ %
You can see that the prompt has changed. Now you can use pip
(which will install packages locally to the PyPy3 virtualenv), python
(which maps to pypy3
) and other related executables. In order to run a script using PyPy, just run python myscript.py
Full source code:
#!/bin/bash
# TechOverflow's 30-second Pypy3 virtual environment generator
# This script is released under CC0 1.0 Universal
DIRECTORY=~/.pypy3
VENV_DIRECTORY=~/.pypy3-virtualenv
VERSION=pypy3.6-v7.3.0-linux64
# Download (or use existing) pypy3
if [ -d "$DIRECTORY" ]; then
echo "Skipping PyPy download, already exists"
else
echo "Downloading PyPy to $DIRECTORY"
# Download & extract to DIRECTORY
wget https://techoverflow.net/downloads/${VERSION}.tar.bz2 -O /tmp/${VERSION}.tar.bz2
bash -c "cd /tmp && tar xjvf ${VERSION}.tar.bz2"
mv /tmp/${VERSION} $DIRECTORY
rm /tmp/${VERSION}.tar.bz2
fi
# Create virtualenv
if [ -d "$VENV_DIRECTORY" ]; then
echo "Skipping to create pypy3 virtualenv, already exists"
else
echo "Creating PyPy virtual environment in $VENV_DIRECTORY"
virtualenv -p ${DIRECTORY}/bin/pypy3 ${VENV_DIRECTORY}
fi
# Create "vpypy" shortcut
set -x
vpypy
result="$?"
set +x
if [ "$result" -ne 127 ]; then
echo "Skipping to create vpypy shortcut, already exists in current shell"
else
echo "Creating bash/zsh shortcut 'vpypy'"
if [ -f ~/.bashrc ]; then
echo -e "\n# TechOverflow PyPy installer\nalias vpypy='source ${VENV_DIRECTORY}/bin/activate'\n" >> ~/.bashrc
fi
if [ -f ~/.zshrc ]; then
echo -e "\n# TechOverflow PyPy installer\nalias vpypy='source ${VENV_DIRECTORY}/bin/activate'\n" >> ~/.zshrc
fi
# Activate shortcut in current shell (but do not automatically activate virtual environment)
alias vpypy='source ${VENV_DIRECTORY}/bin/activate'
fi
echo -e "\n\nPyPy installation finished. Restart your shell, then run 'vpypy' to activate the virtual environment"