Search code examples
pythonbashubuntuvagrantpython-venv

How can I activate a Python venv automatically when I ssh into a vagrant box?


In a Vagrant VM running Ubuntu 16.04, I've created a Python 3 virtual environment using:

python3 -m venv /home/vagrant/venv

When I vagrant ssh, I can activate it using either:

source /home/vagrant/venv/bin/activate

or

. /home/vagrant/venv/bin/activate

During provisioning, I'm creating file /etc/profile.d/login-bash.sh:

cd /vagrant
. /home/vagrant/venv/bin/activate

When I ssh in, that file is running because I'm starting in the /vagrant shared directory, but that second line doesn't appear to be doing anything.


Solution

  • Problem solved. ~/.profile will be run on each ssh login:

    config.vm.provision "shell", inline: 'echo ". /home/vagrant/venv/bin/activate" > ~/.profile', privileged: false