How to install PsychoPy 2022.2.4 on Ubuntu 22.04 using virtualenv

Here, I’ll show how to install PsychoPy in a virtual environment, so we can use python 3.8 instead of python 3.10 (which comes with Ubuntu 22.04), but in a “sandboxed” way, without touching python’s system install.

First, let’s install virtualenv, which will also install a bunch of other packages we’ll need along the way (to compile wxPython, for instance).

sudo apt install virtualenv

To install python 3.8 we’ll need to install the deadsnakes ppa repository.

sudo add-apt-repository ppa:deadsnakes/ppa

Installing python3.8-dev package installs also some dependencies we’ll need, but not all of them. Other packages are needed for different purposes that I’ll list below for documentation.

  • swig, libpulse-dev and libasound2-dev are needed to install pocketsphinx
  • libgtk-3-dev and libwebkit2gtk-4.0-dev are needed to build wxPython
  • git is required to save .psyexp file using Builder.
  • libxcb-xinerama0 libegl1-mesa are required to solve this error that prevents Builder from running experiments.

sudo apt install python3.8-dev swig libpulse-dev libasound2-dev libgtk-3-dev git libwebkit2gtk-4.0-dev libxcb-xinerama0 libegl1-mesa

Now, let’s create the virtual environment, with python 3.8, inside a directory I’ll call “Environments” - I like the ideia of organizing environments inside a single directory, so I don’t have many folders in my Home directory. However, feel free to change this as you like.

virtualenv --python /usr/bin/python3.8 ~/Environments/psychopy-py38

Now, go inside your Environments directory

cd Environments

And activate the environment we just created so we can install PsychoPy and its dependencies inside of it.

source psychopy-py38/bin/activate

If that works, you’ll see that (psychopy-py38) will appear before your prompt. In my case, my prompt reads: (psychopy-py38) flavio@flavio-notebook:~/Environments$

We have to reinstall python3.8-distutils to get pip to work inside the virtual environment - because of a bug reported here.

sudo apt install --reinstall python3.8-distutils

Upgrade pip

pip install pip -U

Install attrdict, which will be required latter on, to keep installing psychopy.

pip install attrdict

Now, we can install psychopy using pip

pip install psychopy==2022.2.4

wxPython will be compiled during the installation - this takes a long time to compile (around 30 minutes). But, look at the bright side: we only have to do it once :wink:

Now you should be able to run PsychoPy.
But let’s go back to the global environment first, so I can show you the two lines of code needed to start PsychoPy:

deactivate

To run PsychoPy Builder we first need to activate the virtual environment

source Environments/psychopy-py38/bin/activate

And then run PsychoPy Builder

psychopy --builder &

When you run it for the first time, a popup will tell you that psychtoolbox needs further configuration. The same steps are described here in case you miss it and want to find them again.

It is also recommended that you go to Properties → Audio and change “Audio library” to “ptb” (i.e. to use psychtoolbox).

Login to pavlovia from Builder is not working. It’s not a new issue, actually - there’s a thread on this problem here. Therefore, I’ve been using gitg to sync my projects with gitlab / pavlovia. If you want to know more about it, leave a comment here.

Builder still crashes when typing a dollar sign into a text component. It is an old bug that came back (it’s a tough bug…). To get rid of it, follow this instructions.

That’s it :slight_smile:

PS: I’d like to thank @arnon_weinberg and @kxKkPbRawwdwEu for their comments on my previous how to, which led to this more polished one (I’m hoping).

PS2: I’ve updated the how-to, since the installation process changed a bit using pip to get the current version of PsychoPy (2022.2.4).

4 Likes

Nicely done, thank you for compiling these instructions.

I haven’t used virtualenv before - does it have any advantage over python -m venv in this particular case? It’s not a huge difference but, I suspect, the installation steps could be reduced by using “/usr/bin/python3.8 -m venv ~/Environments/psychopy-py38”.

Also, I haven’t had any problems with pip in venvs; maybe because I use pyenv and it takes care of it? Not sure. After creating & activating the new venv, I just run “python -m pip install -U pip setuptools” and that usually works.

Starting from a clean 22.04.1LTS, I also had to apt install python3.8=tk and pip install attrdict.
Still having trouble with playing movies with an experiment made from builder.
I’m trying to get an existing experiment to run…

Hi @Andrew_Bangor

About the troubles with playing movies:

Just to make sure you have all the codecs installed, please, run:
sudo apt install ubuntu-restricted-extras

Also, there is a known bug related to playing videos on Ubuntu 22.04 using the preinstalled default app.
Luckily, the workaround is easy. Just remove gstreamer1.0-vaapi
sudo apt remove gstreamer1.0-vaapi

Please, let me know if that helps.

Cheers!

After folllowing all the steps above a Psychopy image does very briefly flash but then it exits with
13:24:15: Debug: “Shift+Tab” is not supported as a keyboard accelerator with GTK
Any suggestions?

Hi @simonrushton

  • Have you installed it on a clean install of Ubuntu 22.04.1?

  • Is the system up to date (all updates installed)? If not, try updating:
    sudo apt update
    sudo apt upgrade

  • I’ve been running Psychopy on Ubuntu using Xorg as window manager. Are you using Wayland (default) or Xorg? Please, try logging in using the “Ubuntu on Xorg” option.

  • What happens when you run:
    source Environments/psychopy-py38/bin/activate
    python
    import wx
    wx.version()

And, just to be sure: when you run Psychopy by typing
source Environments/psychopy-py38/bin/activate
psychopy --builder &
you must leave the terminal open. Closing the terminal also closes Psychopy Builder.