1. 10/5/2019 OpenCV_Linux_Installation_Guide - Google Docs
https://docs.google.com/document/d/1LC6XlasyLBa21YvGfipwcheRSzab3f_PqUBiRsFIB30/edit 1/9
Preparing Ubuntu 18.x-Based Linux
for Using OpenCV 4.1,
Python 3.7.x and PyCharm 2018.3.x
Professor Shohreh Kasaei
pkasaei@gmail.com
Written by Nader Karimi
Nader.karimi.b@gmail.com
Digital Image Processing Course
Spring, 2019
Sharif University of Technology
2. 10/5/2019 OpenCV_Linux_Installation_Guide - Google Docs
https://docs.google.com/document/d/1LC6XlasyLBa21YvGfipwcheRSzab3f_PqUBiRsFIB30/edit 2/9
Installing Python 3.7 on Ubuntu 18.x-based Linux
Installing python 3.7
Run the sequence of commands below:
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3.7
If those commands run successfully, you can run Python 3.7 by typing:
python3.7
If you want to use python3.7, be sure to type its name completely. If you just type python3, you
may start python 3.6.x instead.
Finding the absolute path to the python interpreter
You can find the path to the installed interpreter by typing:
which python3.7
Which in my case is:
/usr/bin/python3.7
Installing pip
You should install pip, which is a powerful package manager for python. Here is how to install
for python3:
sudo apt install python3-pip
2
3. 10/5/2019 OpenCV_Linux_Installation_Guide - Google Docs
https://docs.google.com/document/d/1LC6XlasyLBa21YvGfipwcheRSzab3f_PqUBiRsFIB30/edit 3/9
Installing and Using Virtual Environments for Python
Installing virtualenv
python3.7 -m pip install --user virtualenv
Creating a new virtualenv with python3.7 as the default python interpreter
First, create a directory for storing your virtual environments’ data. You can use your home
directory for example:
cd
mkdir myenvs
cd myenvs
Now you are in the right path to create a new virtualenv. Create a new one by typing:
python3.7 -m virtualenv env
Activating a virtualenv
First, change your working directory to the environments folder. In our case:
cd ~/myenvs
Then activate the environment you want, for example “env”, by typing:
source env/bin/activate
Your terminal should look like this after activating a virtualenv:
Note that we just typed “python” without version number and we could start python 3.7.
If you check python’s absolute path, you will notice that it is now pointing to a file in the specific
virtualenv directory that is activated:
/home/nader/env/bin/python
Sometime it will still point to the system-wide python interpreter. If that happened for you, it’s ok.
3
4. 10/5/2019 OpenCV_Linux_Installation_Guide - Google Docs
https://docs.google.com/document/d/1LC6XlasyLBa21YvGfipwcheRSzab3f_PqUBiRsFIB30/edit 4/9
Deactivating the virtualenv
Use this simple command to deactivate the activated virtualenv:
deactivate
4
5. 10/5/2019 OpenCV_Linux_Installation_Guide - Google Docs
https://docs.google.com/document/d/1LC6XlasyLBa21YvGfipwcheRSzab3f_PqUBiRsFIB30/edit 5/9
Installing and Using Anaconda for Scientific
Programming with Python
Downloading and Installing Anaconda
Download Anaconda for Linux from here. Don’t forget to choose Python 3.7 version.
Change your directory to the download folder. Then enter the command below (check to see if
the name of your file is different):
bash Anaconda3-5.2.0-Linux-x86_64.sh
and answer to the prompts till you finish installing it.
Creating a new virtualenv with python3.7.2 as the default python using conda
Conda is the package manager, somehow similar to pip, which comes with Anaconda. We can
create and manage virtual environments with it, too.
Here we create a new virtual environment using conda. We specify that we want the python to
be version 3.7.2, although we have not installed this version on our machine. Conda will handle
downloading and installing it for us! Also, we don’t need to think about where to store our
virtualenvs since conda has a default path for that.
Type these command to create a new virtualenv named “cenv” (which stands for conda env):
conda create -n cenv python=3.7.2
Finish the process by answering to the prompts it gives you. Note that we specify the name of
the virtualenv after “-n”.
Activating and deactivating a virtualenv using conda
We show how to activate and deactivate virtualenvs in the picture below. See how easy it is to
activate and deactivate a virtualenv using the command “source”. We don’t have to worry about
any system path while working with conda virtualenvs.
Note that instead of the command ‘source’, you can use the equivalent command ‘conda’. For
example, instead of using:
source activate
you can use:
conda activate
5
7. 10/5/2019 OpenCV_Linux_Installation_Guide - Google Docs
https://docs.google.com/document/d/1LC6XlasyLBa21YvGfipwcheRSzab3f_PqUBiRsFIB30/edit 7/9
Installing OpenCV 4.1 in an Activated Virtualenv
First, activate the virtualenv you want to use. Then type this command to install opencv version
4.x.x (the latest version will be installed):
pip install opencv-python
For installing other versions, find the appropriate command from this link.
You can check if it is installed correctly like this:
7
8. 10/5/2019 OpenCV_Linux_Installation_Guide - Google Docs
https://docs.google.com/document/d/1LC6XlasyLBa21YvGfipwcheRSzab3f_PqUBiRsFIB30/edit 8/9
Installing and Configuring PyCharm for Using a Virtual
Environment
First, download PyCharm from here. You may want to download the free, community edition file.
Next, unzip the tar.gz file using this command:
tar -xvzf pycharm_filename.tar.gz
Then go to PyCharm folder, and next to the “bin” folder and start pycharm like this:
Note that there is a file named “Install-Linux-tar.txt” in the PyCharm folder. You can check that
for further information about using PyCharm.
As we like to work with virtualenvs, we should instruct PyCharm to use our desired virtualenv.
You can use the pictures below for guidance. Start PyCharm and create a new project. Open
the Project Interpreter drop-down menu, choose existing interpreter and click on the button right
to it. Another windows will pop up. There is a button right to the Interpreter text field. Click on
that, and in the opened window, enter the path of the python interpreter of your virtualenv. You
can find that path by using “which” command, as explained before. Commit this changes and
finish making the new project.
8
9. 10/5/2019 OpenCV_Linux_Installation_Guide - Google Docs
https://docs.google.com/document/d/1LC6XlasyLBa21YvGfipwcheRSzab3f_PqUBiRsFIB30/edit 9/956
Finally, you can create a launcher for PyCharm, so that you won’t have to start it from
command-line each time. Start PyCharm, go to tools, and click on “Create Desktop Entry …”.
Enjoy!
9