How to upgrade virtualenv (virtual environment) to Python 3.7 on Ubuntu ( Ubuntu server 18.04 )

So I have a home assistant installed in a virtual environment. Just upgraded python to 3.7 and want to share my experience

First thing you need to install python 3.7

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3.7
sudo apt install python3.7-dev
sudo apt install python3.7-venv

Then update virtual environment
I am originally I installed virtual environment in /home/ha like this:

python3 -m venv homeassistant

So my virtual environment located in /home/ha/homeassistant You, of cause, will need to replace my path by yours.

Change directory to one level above your current virtual environment

cd /home/ha

Backup existing installation to be able to go back if something goes wrong

mv homeassistant homeassistant_bkp

Create a new virtual environment

python3.7 -m venv homeassistant
cd homeassistant
source bin/activate
pip install --upgrade pip
python3.7 -m pip install mysqlclient
python3.7 -m pip install homeassistant

You will need to install any additional packages you are using. In my example, as you can see, I am installing mysqlclient. If you donā€™t use mysql you donā€™t need to install it.

Finally, it is currently some bug causing missing frontend package. So, for now, you will need to install it manually:

pip install home-assistant-frontend

That is all. You can run HA as usually after this.

6 Likes

I myself went through this yesterday on a couple of Ubuntu systems.
On 16.04 you need deadsnake ppa, but on 18.04 you donā€™t have to use deadsnake as 3.7.3 is available on Ubuntuā€™s regular upstream.

Just for regular people. When you saying ā€œ3.7.3 is available on Ubuntuā€™s regular upstreamā€ you mean

sudo apt install python3.7

Will install python3.7.3 without adding deadsnake ppa? :slight_smile:

Yes (no ppa)! This is what I did on my 18.04 yesterday:

$ sudo apt install python3.7
$ sudo apt install python3.7-venv

and from my virtual environment:

python3 --version
Python 3.7.3

Ok. Good to know. Thanks.

Did you get the missing front-end part ?

Yes unfortunately :frowning: . but found a way to get it to work :slight_smile:
Run (within the venv) pip install home-assistant-frontend

1 Like

That was the bit I was missing, last python upgrade i just started a new container and moved my configs over.
:joy:

I was on 3.6, but wanted to upgrade to v0.101. I said, ā€˜Screw itā€™, and went right to 3.8.

Thanks for this. Having the same missing front-end issue on FreeNAS. Installing it manually gets everything working again!

Thank you very much for this. Looks like Iā€™m going to need to upgrade as well. As comfortable as I generally feel in HA, I donā€™t feel as comfortable in unix. I can install, download updates, follow instructions, etc.

I did my install about a year ago using these instructions
venv
(as another user)

So, if I generally follow your instructions I will have to do some steps as the Home Assistant User, right?

EDIT: Couple of questions
So If I did this

sudo -u homeassistant -H -s
cd /srv/homeassistant
python3 -m venv .
source bin/activate

when I did my initial install, what folder do I need to backup (mv) to be safe?
/srv/homeassistant ?

Also, I think that this is what Iā€™d do to upgrade if Iā€™m running VENV as another user

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3.7
sudo apt install python3.7-dev
sudo apt install python3.7-venv

cd /srv
sudo mkdir homeassistant (might not be needed, depends on path to previous install?)
sudo chown homeassistant:homeassistant homeassistant (might not be needed, depends on path to previous install?)

sudo -u homeassistant -H -s
cd /srv/homeassistant
python3.7 -m venv .
source bin/activate
pip install --upgrade pip (not sure if needed??)
python3.7 -m pip install homeassistant
pip install home-assistant-frontend

Does that look right?

1 Like

As I mentioned in my earlier post, if you have Ubuntu 18.x you can skip deadsnakes (lines 1 - 3).

I recommend you first update you system in general:
sudo apt update
sudo apt upgrade
If you want to use deadsnakes do lines 2, 3 and then 1.

The rest looks about right. Just note that once you have run source bin/activate, then your command line interface has been setup to automatically use python3.7 meaning you donā€™t have to specify 3.7 in any more of your commands (unless you run deactivate), so you would only need to enter pip install homeassistant.

1 Like

All,

Thanks for this. I am using HA on ubuntu 18.04 installed in a virtual environment as user ā€˜homeassistantā€™. my config files are in /home/homeassistant/.homeassistant and i use an external mySQL database.

Below are the exact steps I took to install (I can take no credit for it as it is all the bits in the posts above put together!):

systemctl stop home-assistant@homeassistant

apt update

apt install python3.7

apt install python3.7-dev

apt install python3.7-venv

cd /srv

#cp homeassistant homeassistant_old ##edited below as it should be a mv

mv homeassistant homeassistant_old

mkdir homeassistant

chown homeassistant:homeassistant homeassistant

sudo -u homeassistant -H -s

cd /srv/homeassistant

python3.7 -m venv .

source bin/activate

pip install --upgrade pip

python3.7 -m pip install mysqlclient

python3.7 -m pip install homeassistant

pip install home-assistant-frontend

exit

systemctl start home-assistant@homeassistant

journalctl -f -u home-assistant@homeassistant

I do log on as root so no sudo required.

Thanks for all the posts above that got me to this and I hope this helps anyone else with a standard Ubuntu install as user homeassistant in a virtenv

mb

4 Likes

Just one Quick question, did you do a
cp homeassistant homeassistant_old

or

mv homeassistant homeassistant_old

mv homeassistant homeassistant_old

I agree that is what I would expect, I was just asking because in @markbajaj steps he listed cp vs mv. Figured if it was a typo he could fix his post to save others from messing up. or maybe he did do a cp, although, Iā€™d expect that not to work. haha

1 Like

You are correct, I did a mv not a copy :slight_smile:

Iā€™ll edit the post now :+1:

Thanks

mb

1 Like

Thanks for this great tutorial. Does anyone have similar instructions for CentOS or a similar rpm-based linux? I previously posted about the problem with deprecating Python 3.6 for CentOS, as there are no officially supported 3.7+ packages available yet, but no one has commented yet (see Python support on CentOS after version 3.6 deprecation).

Maybe check out this thread and this comment in particular which give you some ideas how to build from scratch:

I was dreading the upgrade and thanks to your hard work it was a breeze. Thank you.

1 Like