Build a Python environment in an Ubuntu virtual environment with Virtualbox / Vagrant

Build a Python environment in an Ubuntu virtual environment with Virtualbox / Vagrant

table of contents

  1. VirtualBox Build a virtual environment
  2. Build a Python environment using Pyenv
  3. Launch Jupyter Notebook in a virtual environment

VirtualBox Build a virtual environment

Install the latest version

Install the latest versions of VirtualBox and Vagrant respectively.

  • macOS BigSur 11.4 (host OS)
  • ubuntu18.04 (guest OS)
  • VirtualBox 6.1.22
  • Vagrant 2.2.16 64bit

After downloading and installing, open a terminal, create a folder (any name) for the virtual environment in any location on the mac local, and move it there. At that location, create a directory for the Linux (ubuntu18.04) OS to be added in the directory VagrantBox (here, the directory name is ubuntu64_008), move to that directory, and then create a directory for shared folders (here, here). (The directory name is workspace19) is created.

$ mkdir vvv
$ cd vvv
$ mkdir ubuntu64_008
$ cd ubuntu64_008
$ mkdir workspace19

Choose the right VagrantBox and version from VagrantCloud

VagrantCloud: https://app.vagrantup.com/ubuntu/boxes/bionic64

Creating a Vagrantfile

$ vagrant init ~ (VagrantBox name) will create a Vagrantfile. Here, I want to set the guest OS to Ubuntu 18.04.

$ vagrant init ubuntu / bionic

will do. Then edit the created Vagrantfile.

  • Shared folder settings for local and virtual environments

Folder created earlier: To make workspace19 a shared folder

config.vm.synced_folder “./workspace19”, “/home/vagrant/workspace19”

Will be added.

  • Port forwarding settings between guest OS and host OS

config.vm.network “private_network”, ip: “192.168.33.10”

By commenting out this line, for example, when building a Web application, in this case you can access the browser of the host OS from the guest OS with “192.168.33.10:8000 ~” in the virtual environment. It can be omitted in some cases.

  • The Vagrantfile should look like the one shown below.

 

Build an Ubuntu environment with VagrantBox

After editing the Vagrantfile, you can build the guest OS ubuntu 18.04 with VagrantBox with the following command.

$ vagrant box add ubuntu / bionic64 https://vagrantcloud.com/ubuntu/boxes/bionic64/versions/20210402.0.0/providers/virtualbox.box –force

Since ~ / 20210402.0.0 / ~ in the address indicates the version, you can specify the appropriate version from the Vagrant Cloud site.
VagrantCloud: https://app.vagrantup.com/ubuntu/boxes/bionic64
After the installation of Box is completed, execute the following command to build the virtual environment of ubuntu.

$ vagrant up => It takes time
$ vagrant status => running is OK
$ vagrant ssh => Log in to the virtual environment

Build a Python environment using Pyenv

Use the pyenv library to install Python and anaconda3.

Install the libraries required for Pyenv installation

Install the required libraries for pyenv with the following command, and finally git clone and install pyenv.

$ sudo apt install -y build-essential libssl-dev libffi-dev python-dev

$ sudo apt install build-essential libbz2-dev libdb-dev libreadline-dev libffi-dev libgdbm-dev liblzma-dev libncursesw5-dev libsqlite3-dev libssl-dev zlib1g-dev uuid-dev tk-dev

$ git clone https://github.com/pyenv/pyenv.git ~ / .pyenv

Pass the path of Pyenv

Pass the path of pyenv. For the guest OS ubuntu18.04 of the virtual environment, add the path to .profile.

$ echo’export PYENV_ROOT = “$ HOME / .pyenv”‘ >> ~ / .profile
$ echo’export PATH = “$ PYENV_ROOT / bin: $ PATH”‘ >> ~ / .profile
$ echo -e’if command -v pyenv 1> / dev / null 2> & 1; then \ n eval “$ (pyenv init –path)” \ nfi’ >> ~ / .profile
$ echo’eval “$ (pyenv init –path)”‘ >> ~ / .profile
$ echo’export PYTHONPATH =: “$ HOME / tegant / app”‘>> ~ / .profile

Finally, if there is no error with the following command, it is OK.

$ source ~ / .profile

Python・anaconda installation

Of the pyenv command

$ pyenv install –list

Select the version such as python / anaconda to install with.

 

After selecting, install with the following command. If you want to use machine learning etc., it is a good idea to select the version of anaconda.

$ pyenv install 3.8.4

An example command with $ pyenv install (version or list name) is when installing python 3.8.4. After the installation is complete, check the version of python used on your system with the following command:

$ pyenv versions

Since the version marked with * is specified for use in the system, for example, if you want to specify python3.8.4, do as follows.

$ pyenv global 3.8.4

After specifying, set the reading settings and reflect.

$ pyenv rehash

Finally, check the version of python, and if it is the specified version, it is successful.

$ python -V

Install the necessary libraries with pip install ~, and check the installed packages with pip list.

Launch Jupyter Notebook in a virtual environment

$ jupyter notebook –no-browser –ip = 0.0.0.0 –allow-root

In the case of a virtual environment, execute the above command and access the URL shown as a result to start the browser and use Jupyter notebook.

Leave a Reply

Your email address will not be published. Required fields are marked *